Simulating the transient behaviour of a simple pendulum using Python
AIM: To simulate the transient behaviour of a simple pendulum using Python
OBJECTIVE:
- To generate a plot describing the transient behaviour of the simple pendulum
- To create a GIF describing the animated motion of the simple pendulum
INTRODUCTION
A simple pendulum is defined to have an object that has a small mass, also known as the pendulum bob, which is suspended from a light wire or string.
It is an idealized mathematical model of a pendulum. This is a weight (or bob) on the end of a massless cord suspended from a pivot, without friction. When given an initial push, it will swing back and forth at a constant amplitude. Real pendulums are subject to friction and air drag, so the amplitude of their swing’s declines.
APPLICATIONS
- Clock
- Car Shock Absorbers
- Musical Instruments
- Bungee Jumping
- Diving Board
- The Process of Hearing
- Earthquake-proof buildings
- Metronome
FORMULA
`[(d^2theta)/dt^2] + [b/m][(d theta)/dt]+[g/L]sintheta = 0`
where
- b - damping coefficient
- l - Length of the string
- g - acceleration due to gravity
- m - the mass of the bob
The above equation is a second-order differential equation(ODE). So, it must be divided into two first-order differential equations.
They are as follows:-
- `(d theta_1)/dt = theta_2`
- `(d theta_2)/dt = -[b/m]theta_2 - (g/l)sin theta_1`
The following conditions are assumed during the conversion:-
- `theta = theta_1`
- `(d theta)/dt = (d theta_1)/dt = theta_2`
- `(d^2 theta)/dt^2 = d/dt[(d theta_1)/dt] = (d theta_2)/dt`
PROGRAM CODE
User-defined function
def model(theta,t,b,g,l,m): #diving the 2nd order ODE into two 1st order ODE's
theta1=theta[0]
theta2=theta[1]
dtheta1_dt=theta2 #first order ODE
dtheta2_dt=-(b/m)*theta2-(g/l)*math.sin(theta1) #first order ODE
dtheta_dt = [dtheta1_dt,dtheta2_dt]
return dtheta_dt
Main program code
#importing modules
import math
import numpy as npy
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#Inputs
b=0.05 #damping coeff
g=9.81 #accel due to gravity
l=1 #length of the pendulum
m=1 #mass of the pendulum
#Initial condition(For displacement)
theta0=[math.pi/2,0]
#Time array
t=npy.linspace(0,10,150)
#Solve ODE
theta=odeint(model,theta0,t,args = (b,g,l,m))
#Plotting
plt.plot(t,theta[:,0],".",label="Displacement",color="Red")
plt.plot(t,theta[:,1],"--",label="Velocity",color="Black")
plt.legend()
plt.title("Transient behaviour of a simple pendulum with damping")
plt.ylabel("Plot")
plt.xlabel("Time(sec)")
plt.show()
#Animation
ct=1
x0=5
y0=5
for i in range(len(t)):
x1=x0+l*math.sin(theta[i,0])
y1=y0-l*math.cos(theta[i,0])
filename='Pend'+str(ct)+'.png'
plt.figure()
plt.plot([4,6],[5,5],'black') #fixed link
plt.plot([x0,x1],[y0,y1],"green") #pendulum string
plt.plot(x1,y1,"red",marker="o",markersize='60') #pendulum bob
plt.axis([3.5,6.5,3,6])
plt.savefig(filename)
ct=ct+1
Procedure
- Import neccesary modules for using built in callable functions for solving the ODE and plotting the graph.
- Divide higher-order ODE into the first-order ODE and define in the function.
- Define the initial conditions and angular velocity and displacement. The simulation time is taken in the range of 0-20 seconds.
- The ODE is solved and the graph is plotted.
- An animated video depicting the motion of the simple pendulum is generated.
RESULTS AND OBSERVATIONS
CONCLUSION
Using the code above, we were able to succesfully generate a plot depicting the transient nature of the simple pendulum and also able to generate an animated video depicting the motion of the pendulum