Week 3 - Solving second order ODEs
Aim: To solve the second order ODEs for the simple pendulum by describing the transient behaviour of the system.
Objective:
To write a program in Python that will simulate the pendulum motion,
Given data:
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
Program code:
import numpy as np
from scipy.integrate import ode int
import math
import matplotlib.pyplot as plt
#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.02
g = 9.81
l = 1
m = 0.1
#initial condition
theta_0 = [math.pi/2,0]
#time points
t = np.linspace(0,10,150)
#solve 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],'r--',label=r'$\frac{d\theta2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{l}sin\theta_1$')
plt.xlabel('time')
plt.ylabel('plot')
plt.legend(loc='best')
plt.show()
Explanation:
Inorder to work on this 2nd order ODE, installation of the numpy and scipy has to be done.
Import both numpy and scipy ,
Import math and matplotlib,
Defining the functions for the given equation is done.
First the equation is reduced as first order ODE and second order ODE and the first ODE is the displacement whereas the second ODE is for the velocity of the system.
Length of the string is set 1m, mass of the system is 1kg, damping coefficient is set as 0.02 and g is 9.81.
Initial condition is declared in terms of theta with radians.
Time points for the same is stated t =np.linspace(0,10,150) it is carried out through numpy inorder to intergrate the values from 0 -150.
Solving the ODE with int command for the given input variables, here it works through scipy which integrates the ODE at each instant.
Finally plotting for the same is carried out.
Screenshot:

Result/Output:
The program for solving the second order ODE of the simple pendulum is written and the plots for the same is obtained.
The animation video of the simple pendulum is created using the MATLAB and the same is uploaded in the you tube.