Week 3 - Solving second order ODEs
objective :
To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Input Values
L=1 metre,
m=0.1 kg,
b=0.05.
g=9.81 m/s2.
Step 1 : Define the function
def model(theta,t,b,g,L,m)
theta1 = theta[0]
theta2 = theta[1]
dtheta1_dt = theta2
dtheta2_dt = -(b/m)*theta2 - (g/1)*math.sin(theta1)
dtheta_dt = [dtheta_dt,dtheta2_df]
return dtheta_dt
Program :
import math
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
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.05
g=9.81
l=1
m=1
theta_0=[math.pi/2
t=np.linspace(0,50,100)
theta=odeint(model,theta_0,t,args= (b,g,l,m))
print(theta)
plt.plot(t,theta[:,0])
plt.plot(t,theta[:,1])
plt.ylabel('plot')
plt.xlabel('time')
plt.savefig('plt')
plt.show()