2nd order simulation of simple pendulum by python
Aim: to simulate the transient behaviour of a simple pendulum and to create animation.
introduction:
In an engineering the ordinary diffenstial equation are used to describe transient behaviour of a system. the simple pendulum is one of the ODE.
by Newton second law the pendulum defines its motion, the secod order ODE which describes the position of ball of an pendulum with respect to time.
above equation describes hundreds of ODEs, in above equation g= gravity in m/se2
l=length of pendulum in m
m=mass of the ball in kg
b=damping coefficient
program to get frequency graph for pendulum
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
#function that returns dz/dt
def model(theta,time,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.02
g=9.81
l=1
m=0.1
#initial condition
theta_0=[0,3]
#time inputs
time=np.linspace(0,20,100)
#solve ODE
theta=odeint(model,theta_0,time,args=(b,g,l,m))
# plot results
plt.figure()
plt.plot(time,theta[:,0],\'b-\',label=r\'$\\frac{d\\theta_1}{dtime}=\\theta2$\')
plt.plot(time,theta[:,1],\'r--\',label=r\'$\\frac{d\\theta2}{dtime}=-\\frac{b}{m}\\theta_2-\\frac{g}{L}sin\\theta_1$\')
plt.ylabel(\'plot\')
plt.xlabel(\'time\')
plt.legend(loc=\'best\')
plt.show()
Explianation
1] program code written according to given data for velocity, damping coefficent etc.
2] time priode is taken 0 to 3 for 20secs
3] by scipy above shown equation is integrated from 0 to 20 with 100 values
4] finally ploting gragh time vs order divation for both first and second order
5] dotted= first order, blue=second order
program to create images for animation
ct=1
for t in theta[:,0]:
x0=0
y0=0
x1=(l*math.sin(t))
y1=-(l*math.cos(t))
gam=str(ct)+\'.png\'
ct=ct+1
plt.figure()
plt.plot([-0.5,0.5],[0,0],\'black\',linewidth=5)
plt.plot([x0,x1],[y0,y1],linewidth=3,color=\'r\')
plt.plot(x0,y0,\'o\',markersize=5)
plt.plot(x1,y1,\'o\',markersize=30,color=\'blue\')
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.5])
plt.savefig(gam)
Explianation
1] to count images ct command used and plus 1 for ever next increment.
2] intial cordinates are give for fixed pendulum
3]similary end cordinators are given as x1,y1
4] images are saved in form of pngs.
5] fixed line with width 5m given in black color
6] pendulum with width 5 in color red defined
7] ball weigth 30 in blue color.
8] for above 5,6,7 marker command used to create physical pendulum.
9] finally animated video create by using pngs.
https://www.youtube.com/watch?v=1ttkM467Fyg
above link for video of simple pendulum uploded in youtube in the name of hari reddy.
following errors faced
instead of open bracket placed curved bracket later changed.
faced diffculty in marking physical pendulum and limits[-4,4] out the order defind later given correct order and no variation in the position of pendulum so changed as iven in code.
conculsion
simulation for simple pendulum program done sucessfully.