Week 3 - Solving second order ODEs
Aim:To slove the Ordinary Differential Equation(ODE'S) by using python programming language,In which the simple pendulum is taken as reference.Here we create its animation and stimulate the behaviour of the simple pendulum of the motion.
Theory:For sloving the ordinary differential equation,we use the differential equation:
d^2/dt^2 + b(d(theta))/m(d(t))+g(sin(theta))/L=0,where
g=acceleration due to gravity(m/s^2)
L=length of the pendulum(m)
m=mass of the ball(kg)
b=damping co-efficient
theta=angle made by simple pendulum
In this we will break the equation into a first order differential equaion,so that the equation to slove becomes easier to slove.
Procedure:The required program to slove the second order differential equations is:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
#function that returns dz/dt
def model(theta,t,b,g,l,m):
theta1=theta[0]
theta2=theta[1]
dtheta1_dt=theta2
dtheta2_dt=-(b/m)*theta2-(g/l)*math.sin(theta1)
dtheta_dt=[dtheta1_dt,dtheta2_dt]
return dtheta_dt
b=0.03
g=9.81
l=2
m=0.3
#intial condition
theta_0=[math.pi/2,0]
#time points
t=np.linspace(0,10,150)
#slove ODE
theta=odeint(model,theta_0,t,args=(b,g,l,m))
#plot results
plt.plot(t,theta[:,0],'b-',label=r'$\frac{d\theta_1}{dt}=\theta2$')
plt.plot(t,theta[:,1],'b-',label=r'$\frac{d\theta_2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{L}sin\theta_1$')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()
#animation step
ct=1
x0=5
y0=5
for i in range (len(t)):
x1=x0+1*math.sin(theta[i,0])
y1=y0-1*math.cos(theta[i,0])
filename='pendulum'+str(ct)+'png'
plt.figure()
plt.plot([4,6],[5,5],'k-')
plt.plot([x0,x1],[y0,y1])
plt.plot(x1,y1,marker=0,markersize='10')
plt.savefig(filename)
ct=ct+1
Result:The require graph for the second order differential equation,considering simple pendulum as reference and the animation is shown below.

Conculsion:The simulation of the simple pendulum is done by solving a second order differential equation.The animation is simulated.