Week 3 - Solving second order ODEs
AIM: To write a program for the motion of simpe pendulum using ODE.
THEORY: A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position.
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.
PYTHON PROGRAMME:
import math
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import numpy as np
#function program
def model(theta,t,b,g,m,l):
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
#input parameters
b= 0.05
g= 9.81
l= 1
m= 1
theta_0= [math.pi/2,0]
t= np.linspace(0,20,200)
#Solving the ordinary diff. equation
theta= odeint(model,theta_0,t,args=(b,g,l,m))
#Plotting the values
plt.figure()
plt.plot(t,theta[:,0],'g',label=r'Angular Displacement')
plt.plot(t,theta[:,1],'r',label=r'Angular velocity')
plt.xlabel('plot')
plt.ylabel('Time')
plt.legend(loc='best')
#plt.show()
#Animating the outputs
ct=1
x0= 5
y0= 5
for i in range(len(t)):
x1= x0+l*math.sin(theta[i,0])
y1= y0-l*math.cos(theta[i,0])
filename= 'pendulum motion'+str(ct)+'.png'
plt.figure()
plt.plot([4,6],[5,5],'k-')
plt.plot([x0,x1],[y0,y1])
plt.plot(x1,y1,marker='o',markersize='10')
plt.axis([3.5,6.5,3,6])
plt.savefig(filename)
ct= ct+1
EXPLANATION OF CODES:
- Import the required math libraries.
- Function module is created in which required arguments have been inserted to calculate the ODE.
- Inputs like g,mass,length,etc have been defined.
- Theta_0 calculated from range 0-90.
- Differential equation has been calculated using odeint command.
- After that plots have been generated for angular displacemant and angular velocity.
- After that animation is created using the for loop using the length of t viz .20.
- Finally, images are being saved using savefif command.
OUTPUT PLOTS:
ERRORS FACED: No error encountered.
ANIMATION FILE:
REFERENCES: