Week 3 - Solving second order ODEs
OBJECTIVE
- To write a program in python to simulate the transient behavior of a Simple pendulum.
- To plot graph Time vs Displacement.
- To create an animation of its motion.
THEORY ABOUT SIMPLE PENDULUM
The pendulum is a simple mechanical system that follows a differential equation. The pendulum is initially at rest in a vertical position. When the pendulum is displaced by an angle θ and released, the force of gravity pulls it back towards its resting position.
A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth. The time for one complete cycle, a left swing and a right swing, is called the period. The period depends on the length of the pendulum and also to a slight degree on the amplitude, the width of the pendulum's swing.

GOVERNING EQUATION
Second order differential equation for simple pendulum is
d2θdt2+bmdθdt+glsinθ=0d2θdt2+bmdθdt+glsinθ=0
Where,
g=gravity (ms2ms2)
l=Length of the pendulum (m)
m=mass of the ball (kg)
b=damping coefficient
INPUT VALUES
g=9.81 ms2ms2
l=1 m
b=0.05 m
m=1 kg
PROCEDURE
- Initially we are going to import the “matplotlib.pyplot” , “math” , “scipy” and “numpy” library for the calculations.
- Scipy library provides us a equation of the second order ODE to solve the theta value.
- Then just input the given parameters are g, b, l and m to calculate the other parameters.
- Using the calling function command we are going to call theta values.
- And then using “numpy” command we provide the steps in the time.
- Then plot the Time against Displacement graph.
- For the motion of simple pendulum, first mention the co-ordinates of the pendulum at the start(x0, y0) and end(x1, y1).
- Using the string and counter command we’ll give different png names and save all the png files.
- For the Animation we use some other software to create the animation.
PROGRAM
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
from PIL import Image
import glob
#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 = 0.5
#initial condition
theta_0 = [0,3]
#time points
t = np.linspace(0,15,300)
#solve ODE
theta = odeint(model,theta_0,t,args = (b,g,l,m))
#print(theta)
#plot results
plt.plot(t,theta[:,0],'b-', label = r'$frac {dtheta_1}{dt}=theta2$')
plt.plot(t,theta[:,1],'r--',label = r'$frac{dtheta2}{dt}= -frac{b}{m}theta_2-frac{g}{l}sintheta_1$')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend(loc = 'best')
#plt.savefig('Graph_disvelo')
plt.show()
#plotting simple pendulum on graph at each angle.
x = theta[0,1]
y=theta[:,0]
ct=1
for ang in y :
x1=0;y1=0
x2=l*math.sin(ang)
y2=-l*math.cos(ang)
plt.figure()
plt.plot([-2.0,2.0],[0,0],'y',linewidth=5)
plt.plot([x1,x2],[y1,y2],linewidth=3)
plt.plot(x2,y2,'o',markersize=20,markerfacecolor='w')
plt.plot(x1,y1,'o',markersize=6,markerfacecolor='r')
plt.xlim([-2.0,2.0])
plt.ylim([-2.0,0.5])
filename = 'test%05d.png'%ct
plt.savefig(filename)
ct=ct+1
#creating 'gif' from the images saved
frames = []
imgs = glob.glob("*.png")
for i in imgs:
new_frame = Image.open(i)
frames.append(new_frame)
# Save into a GIF file that loops forever
frames[0].save('png_to_gif.gif', format='GIF',
append_images=frames[1:],
save_all=True,
duration=60, loop=0)
RESULTS
Plot

Animation Video
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

CONCLUSION
- Simulation of transient behavior of a Simple pendulum motion by solving second order differential equation.
- Animation of the Simple pendulum is simulated.
REFFERENCES