Python -Transient behavior of a simple pendulum
Transient behaviour of a simple pendulum
Objective
To write a program in python to simulate the transient behaviour of a simple pendulum and to create an animation of it's motion using the ODE for the motion of a pendulum.
Procedure
Formulae:
g = gravity in m/s^2
L = length of the pendulum in m
m = mass of the ball in kg
b=damping coefficient
Values used in the program
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s^2.
By importing the odeint module from scipy.integrate library we are able to calculate the complex differential equations using python. However the ODE for a simple pendulum is of the second order and second order ODE must be simplified into the first order form before computing it in python
`theta = theta1`
`d theta/ dt = d theta1/dt = theta2` Eq.1
`d^2 theta/dt^2 = d theta2/dt`
`d theta2/dt = -(b/m) theta 2 - (g/L)sintheta1`` Eq.2
`theta 1` = angular displacement , `theta2` = angular velocity
We then use the main code along with conditions,
theta = odeint(model,theta_0,t,args = (b,g,l,m))
theta_0 = [0,3]
t = np.linspace(0,20,150)
where ,
model: is the function containing Eq.1 and Eq.2 and will define the ODE with theta1 in the 0th column and theta2 in the 1st column
theta_0': this array is used to denote the initial conditions at t=0 to pass on to the function model. in this case we have given zero angular displacement and 3m/s angulat velocity
t: is used to pass the time conditions , in this particular program we are using t =0 to t=20 secs with 150 intervals between them
Python program to simulate the motion of a pendulum:
"""
Simple Pendulum ODE
"""
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
b = 0.05
g = 9.81
l = 1 #metre
m = 1 #kg
#initial condition
theta_0 = [0,3]
#timepoints
t = np.linspace(0,20,150)
# function returning 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
#solve ODE
theta = odeint(model,theta_0,t,args = (b,g,l,m))
x0 = 0
y0 = 0
ct = 1
for i in range(100):
x1 = l*math.sin(theta[i,0])
y1 = -l*math.cos(theta[i,0])
plt.figure()
plt.plot([-1,1],[0,0], color = 'red', linewidth = 1 )
plt.scatter(x1,y1,color = 'blue', s = 300)
plt.plot([x0,x1],[y0,y1], color = 'black')
plt.xlim([-2,2])
plt.ylim([-2,2])
filename = 'test%05d.png'%ct
plt.savefig(filename)
ct = ct + 1
Simulating motion of the pendulum:
Initiate conditions for the pivot point at co-ordinates (0,0)
x0 = 0
y0 = 0
we use a for loop and increment for a certain value 'i' to get the position of the pendulum for each ofthe values stored in the first column which contains all the angular displacement values
The co-ordinates of the bob are ,
`x1 = l*sintheta`
`y1 = l*costheta`
Fig: Image from the simulation
Conclusion
The ODE for the motion of pendulum was successfully made into a python program and the animation images for the motion were obtained.