Week 3 - Solving second order ODEs
CODE :
import math
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
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.1
g = 9.81
l = 1
m = 0.1
ct = 1
theta0 = [0,3]
t = np.linspace(0,20,200)
theta = odeint(model,theta0,t,args=(b,g,l,m))
angles = theta[:,1]
plt.figure(1)
plt.plot(t,theta[:,0],label='Angular displacement')
plt.plot(t,theta[:,1],label='Angular velocity')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend()
plt.savefig('graph.png')
plt.show()
for angle in angles:
x1 = 5
y1 = 5
x2 = x1 + l*math.sin(angle)
y2 = y1 - l*math.cos(angle)
filename =str(ct)+'.png'
ct = ct +1
s1 = str(t[ct-2])
plt.figure(2)
plt.plot([x1,x2],[y1,y2])
plt.plot(x2,y2,marker='o',markersize=10)
plt.text(5.8,6,'Time = '+s1[0:4]+'s')
plt.xlim([3.5,6.5])
plt.ylim([3.5,6.5])
plt.savefig(filename+'.png')
plt.clf()
#plt.show()
OUTPUT:

PENDULUM ANIMATION LINK :
https://drive.google.com/file/d/1ViM15NOdsh7ZWCTR3M1HSyuEuIdN4F_M/view?usp=sharing