Week 3 - Solving second order ODEs
AIM:
to write a program to simulate the transient behaviour of a simple pendulam and creat an animation of its motion
PROCEDURE:
1. importing math ,matplot, numpy
import numpy as np
from scipy.integrate import odeint
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
# input
b = 0.05
g = 9.81
l = 1
m = 1
# initial condition
theta_0 = [0,3]
# time points
t = np.linspace(0,20,200)
# solve ODE
theta = odeint(model,theta_0,t,args = (b,g,l,m))
# ploting 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/theta_2}{dt}= -\frac{b}{m}\theta_2-\frac{g}{L}sin\theta_1$')
plt.xlabel('time')
plt.ylabel('plot')
plt.legend(loc='best')
plt.show()
# making animation
THETA3 = []
for theta3 in theta[:,0]:
THETA3.append(theta3)
ct = 1
x0 = 0
y0 = 0
for THETA4 in THETA3:
x1 = l*math.sin(THETA4)
y1 = -l*math.cos(THETA4)
filename= str(ct) +'.png'
ct = ct+1
plt.figure()
plt.plot([-1.5,1.5],[0,0],linewidth=8)
plt.plot([x0,x1],[y0,y1],linewidth=3)
plt.plot(x1,y1,'o',markersize = 15)
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1])
plt.savefig(filename)
2. input values are,
damping coefficient,
b = 0.05
gravity,
g = 9.81
length of pendulam ,
l = 1
mass of ball,
m = 1
3. defining the function ,
def model(theta,t,b,g,l,m)
when the second oder equation can split into 2 parts ,
dtheta1_dt = theta2
dtheta2_dt = -(b/m)*theta2 - (g/l)*math.sin(theta1)
4. at the initial condition theta_0 can change from 0 to 3
- considering the time is 0 to 20 sec ,an d time can split into equal 200 parts using np.linspace
5. when solving the ode to calculate the values for theta by using odeint function,
theta = odeint(model,theta_0,t,args = (b,g,l,m))
6.ploting the plots by using the values in displacement and time
the output plot is,

7. creating th animation when considering theta3 and theta4
-for creating the loop for theta3 at theta[:,0]
- making another loop in theta3 by theta4
-at initialy x0,y0 is zero
- at perticular point in
x1 = l*math.sin(THETA4)
y1 = -l*math.cos(THETA4)
- making each plots seperatly by using plt.plot command
8. each plots can covert into a single folder
- command prompt is used to make a animation of all plot
the animation link is;
https://drive.google.com/file/d/1X1PcZmNj4O19sSIYnppSpAHlirGPVOsS/view?usp=sharing
CONCLUSION:
can be write a program to simulate the simple pendulam and create an animation of pendulam