Week 3 - Solving second order ODEs
OBJECTIVE:
- Writing a program in python to simulate the transient behaviour of a Simple pendulum.
- Plotting graph Time vs Velocity, Acceleration.
- To create an animation of simple pendulum 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
`(d^2θ)/dt^2 + (b/m) * (dθ)/dt +(g/l) *sinθ = 0`
Where,
g=gravity (m/s^2)
l=Length of the pendulum (m)
m=mass of the ball (kg)
b=damping coefficient
Given INPUT VALUES
g=9.81m/s^2
l=1m
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 an 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 (x1, y1) and end (x2, y2).
- Using the string and counter command we’ll give different png names and save all the png files.
- To create animation, me used “ffmpeg” software which work through CMD.
Programme:
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
import scipy.optimize
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 # Damping Co-efficient
g = 9.81 # gravity in m/s^2
l = 1 # Length of pendulum rod in meter
m = 1 # Weight of the pendulum
# initial condition
theta_0 = [0,3]
#time points
t = np.linspace(0,20,250)
# solve ODE
theta =odeint(model,theta_0,t,args = (b,g,l,m))
#plot results
plt.figure()
plt.plot(t,theta[:,0],'b-',label =r'$\frac{d\theta_1}{dt}=\theta2$')
plt.plot(t,theta[:,1],'r--',label =r'$\frac{d\theta_2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{L}sin\theta_1$')
plt.ylabel('Plot')
plt.xlabel('Time in sec')
plt.legend(loc='best')
#plt.show()
plt.savefig('Simple_pendulam_oscilation.PNG')
# Plotting simple pendulam 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)
filename = str(ct) +'.png'
ct = ct+1
plt.figure()
plt.plot([-2.0,2.0],[0,0], 'y',linewidth =10)
plt.plot([x1,x2],[y1,y2],linewidth=5)
plt.plot(x1,y1,'o',markersize = 8,markerfacecolor ='b')
plt.plot(x2,y2,'o',markersize = 20,markerfacecolor ='g')
plt.xlim([-2,2])
plt.ylim([-2,0.5])
plt.title('Simple Pendulum')
plt.savefig(filename)
The above program is run for 20secs with 250 values. When I run more than 250(300) values the programme is showing error
RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`). Consider using `matplotlib.pyplot.close()
Output:
FFmpeg is used to generate animation file and that file also uploaded in YouTube. To watch the video, follow the link: https://youtu.be/dPxISDV3PWE
Save the plotted files of simple pendulum to a certain folder and open that folder in command prompt by right clicking on the folder or by CMD commands.
Than use this command “ffmpeg -i %d.png simple_pendulum.avi/ ffmpeg -i %d.png simple_pendulum.gif ” to create the animation of .png files.

