Week 3 - Solving second order ODEs
Aim:
To Write a program in Python that will simulate the pendulum motion, just like the one shown in the start of this challenge.
Theory:
To simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.When you do this you will get the position of the pendulum as a function of time. Use this information to animate the motion of the pendulum.
L=1 metre,
m=1 kg,
b=0.05,
g=9.81 m/s2.
Explanation of coding logic:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
#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.05
g = 9.81
l = 1
m = 1
#initial condition
theta_0 = [math.pi/2, 0]
#time points
t = np.linspace(0,20,500)
#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('plot')
plt.ylabel('time')
plt.show()
#Animation for simple pendulum
ct=1
for i in theta[:,0]:
x0=0
y0=0
x1=(l*math.sin(i))
y1=-(l*math.cos(i))
filename = 'test%05d.png' % ct
ct=ct+1
#Plotting
plt.figure()
#Plotting the base line of the pendulum
plt.plot([-0.5 ,0.5],[0,0],linewidth =4,color ='blue')
plt.plot([x0,x1],[y0,y1],linewidth=2,color = 'black')
plt.plot(x1,y1,'o',markersize =15,color ='green')
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.5])
plt.title('Animation of Simple pendulum')
plt.savefig(filename)
plt.show()
Animation vedio link: https://youtu.be/4AVsSVQiyU4
Result:
ODE is used to describe the transient behavior of a system. A simple example is a pendulum. The way the pendulum moves depends on the Newtons second law.
Conclusion: The above program in Python that will simulate the pendulum motion.
Resultant of the simple pendulum :

Resultant ODE from the problem:
