Solution of second order ODE and animation of motion of a pendulum by PYTHON programming
Aim:-
To solve second order ODE and animate the motion of the pendulum by PYTHON programming
Governing equation:-
Equation that play main role to solve second order ODE:-
dtheta/dt2+b/m d2theta/dt+g/L sintheta=0
where,
m=mass of the pendulum,
l=length of the pendulum
g=gravitational acceleration
b=damping coefficient
Now,
Let, theta=theta1 where theta=angular displacement
or, d2theta/dt=dtheta1/dt=theta2(say)
or, dtheta2/dt=-b/m* theta2+g/l * sin(theta1)
Objective:-
The main objective of this project is to solve second order ODE for a simple pendulum to animate its motion and plot its graph.
Brief Theory:-
Simple pendulum:- It is nothing but a small ball of mass m suspended by a string (massless) of length L.
Acceleration of pendulum:- It is given by the equation a = -g sin_theta
Time period:- It is the time taken by a simple pendulum to complete one oscillation (full) and is given by T = 2π/omega
Second order differential equation for simple pendulum:- It is given by the governing equation
d2theta/dt2+b/m dtheta/dt+g/l sintheta=0
If theta=theta1 , then d2theta/dt=d2theta1/dt2=d/dt (dtheta1/dt) = d/dt (theta2)
Equation becomes
dtheta2/dt + b/m theta2 + g/l sintheta1=0
Workflow:-
1) Inputs are assumed
2) Initial conditions are given and differential equation is solved by a function that later defined.
3)Plots and animations are created as usual.
Brief content of this project:-
This project has been done by PYTHON programming. The code and the function have been attached below.
Program for the plot:-
import numpy as np
import math
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#taking function
def model(T,t,b,l,g,m):
T1= T[0]
T2 = T[1]
dT1_dt = T2
dT2_dt = -(b/m)*T2 -(g/l)*math.sin(T1)
dTh_dt = [dT1_dt, dT2_dt]
return dTh_dt
# taking inputs
# damping co-efficient
b = 0.05
# length of pendulum(m)
l = 1
# gravity ( m/s^2)
g = 9.81
# mass of ball (kg)
m = 1
#taking initial conditions
T_x = [0,3]
# taking time points
t = np.linspace(0,20,150)
# solving ode
T = odeint(model,T_x,t,args=(b,l,g,m))
# plotting of graph
plt.plot(t,T[:,0],'-',label = 'displacement')
plt.plot(t,T[:,1],'--',label= 'Velocity')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend()
plt.show()
Program for animation:-
import numpy as np
import math
from scipy.integrate import odeint
import matplotlib.pyplot as plt
# taking function
def model(T,t,b,l,g,m):
T1= T[0]
T2 = T[1]
dT1_dt = T2
dT2_dt = -(b/m)*T2 -(g/l)*math.sin(T1)
dTh_dt = [dT1_dt, dT2_dt]
return dTh_dt
# taking inputs
# damping co-efficient
b = 0.05
# length of pendulum(m)
l = 1
# gravity( m/s^2)
g = 9.81
# mass of ball (kg)
m = 1
# taking initial conditions
T_x = [0,3]
# taking time points
t = np.linspace(0,20,150)
# solving ode
T = odeint(model,T_x,t,args=(b,l,g,m))
# plotting
X = T[:,0]
ct = 1
for theta in X:
x1 = 0;y1 = 0
x2 = l*math.sin(theta)
y2 = -(l*math.cos(theta))
file = 'test%05d.png'%ct
plt.figure()
plt.plot([-1.5,1.5],[0,0],linewidth = 5)
plt.plot([x1,x2],[y1,y2],linewidth = 4)
plt.plot(x2,y2,'o',markersize =12)
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.5])
plt.savefig(file)
ct = ct+1
Algorithm of the program for the plot:-
1)Function is taken
2)Inputs are taken
3)plotting is done
4)plot command is used to plot graph
5)xlim ylim command is used for the range of the
6)using savefig command figure is saved
7)loop increment condition is taken
Algorithm of the program for the animation:-
1)Similar to the algorithm of the plot
2)xlabel,ylabel command is used to put x-axis, -axis properties
3)animation is formed and shown.
Output Plot:-
Animation:-
Below the YouTube link of the animation:-
Conclusion:-
Through this whole program, e have learned how to solve second order ode problems, plot and the animation of pendulum by python programming which is useful for industry works.