All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: To simulate the transient behavior of a simple pendulum and to create an animation of its motion. OBJECTIVE : To write a program in Python that solves the second-order ODE corresponding to the motion of a simple pendulum and to plot its angular displacement and angular velocity wrt time. To create…
Shubhranshu Mishra
updated on 03 Jul 2020
AIM: To simulate the transient behavior of a simple pendulum and to create an animation of its motion.
OBJECTIVE :
THEORY: 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, such as shown in Figure 1.
Figure 1
When the bob of the pendulum is displaced from it's mean position to either extreme position and is released, the pendulum will swing back and forth with periodic motion until the motion damps. By applying Newton's second law for rotational systems, the equation of motion for the pendulum may be obtained as
τ=I⋅ατ=I⋅α ⇒⇒ −mg⋅sinθ⋅L-mg⋅sinθ⋅L = mL2⋅d2θdt2mL2⋅d2θdt2
and rearranged as d2θdt2+(gL)⋅sinθ=0d2θdt2+(gL)⋅sinθ=0
and if a damper is introduced in the system the above equations changes slightly as
d2θdt+(bm)⋅d(θ)dt+(gL)⋅sinθ=0d2θdt+(bm)⋅d(θ)dt+(gL)⋅sinθ=0
GOVERNING EQUATIONS :
To solve the above equation in Python, we need to transform the given second order ODE into first order ODE as,
⇒⇒ θ=θ1θ=θ1 ,
⇒⇒d(θ)dt=d(θ1)dt=θ2d(θ)dt=d(θ1)dt=θ2 ,
⇒⇒d2θdt=d2θ1dt=d(θ2)dtd2θdt=d2θ1dt=d(θ2)dt ,
⇒d2θdt+(bm)⋅d(θ)dt+(gL)⋅sinθ=0⇒d2θdt+(bm)⋅d(θ)dt+(gL)⋅sinθ=0 ,
⇒d2θ1dt+(bm)⋅d(θ1)dt+(gL)⋅sinθ1=0⇒d2θ1dt+(bm)⋅d(θ1)dt+(gL)⋅sinθ1=0 ,
⇒d(θ2)dt+(bm)⋅θ2+(gL)⋅sinθ1⇒d(θ2)dt+(bm)⋅θ2+(gL)⋅sinθ1 ,
∴,d(θ1)dt=θ2∴,d(θ1)dt=θ2 and d(θ2)dt=−(bm)⋅θ1−(gL)⋅sinθ1d(θ2)dt=-(bm)⋅θ1-(gL)⋅sinθ1
Represent them in matrix form,
⇒ddx[θ1θ2]=[θ2−(bm)⋅θ2−(gL)⋅sinθ1]⇒ddx[θ1θ2]=[θ2-(bm)⋅θ2-(gL)⋅sinθ1] ,
BODY OF THE CONTENT :
PYTHON CODE FOR SOLVING ODEs AND ANIMATION :
''' A program for solving second order ODEs and simulate its motion'''
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
def ode_function(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
# Inputs
# damping coefficient
b = 0.05
# acceleration due to gravity(m/s^2)
g = 9.81
# length of string (m)
l = 1
# mass of bob (kg)
m = 1
# Initial Conditions
theta_0 = [0,3]
# time points
t_span = np.linspace(0,20,100)
# Solve ODE
theta = odeint(ode_function,theta_0,t_span,args = (b,g,l,m))
# Plotting the graph
plt.plot(t_span,theta[:,0],'b-',label=r'$frac{dtheta1}{dt}=theta2$')
plt.plot(t_span,theta[:,1],'r--',label=r'$frac{dtheta2}{dt}=-frac{b}{m}theta2-frac{g}{l}sintheta1$')
plt.xlabel('Time (second)')
plt.ylabel('Plot')
plt.title('Damped Harmonic motion of a Simple Pendulum')
plt.legend(loc='best')
plt.grid()
plt.show()
# Iteration using for loop to create animation of motion of pendulum
loop_counter =1
tmp = theta[:,0]
for i in tmp:
x_start = 0
y_start = 0
x_end = l*math.sin(i)
y_end = -l*math.cos(i)
# Plotting for animation
plt.figure()
# Plotting String of pendulum
plt.plot([x_start,x_end],[y_start,y_end],linewidth=3)
# Plotting Bob of pendulum
plt.plot(x_end,y_end,'o',markersize=20,color='r')
# Plotting the rigid support through which string of pendulum is attached
plt.plot([-0.5,0.5],[0,0],linewidth=8,color='k')
# Plotting hatched vertical line for refernce
plt.plot([0,0],[0,-1.5],'--',color='y')
plt.xlim([-1.25,1.25])
plt.ylim([-2,1])
plt.title('Animation of transient behaviour of a Simple Pendulum')
filename = 'test%05d.png' % loop_counter
plt.savefig(filename)
loop_counter = loop_counter + 1
NOTE:- Python throws an error for indentation mismatching. Use the code in the format as posted here.
ERRORS: No major errors were encountered while writing and solving the code.
RESULTS :
The above plot of angular displacement and angular velocity shows the pendulum's motion to be gradually decreasing. This is due to the damping effect which exponentially decreases the amplitude and the velocity of motion.
CONCLUSION: In this way, we can understand that any second-order or any higher-order ODE can easily be solved using Python, and simulation of the transient behavior of simple pendulum was made using it to explain the effect of damping on its motion.
NOTATIONS :
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Frequency Analysis of a rotating shaft (Finite Element Analysis using SolidWorks)
Aim- The aim of this project is to perform a frequency analysis on a rotating shaft, from there we need to determine the critical frequencies and the mode shapes. 5 Mode Shapes were simulated and analyzed. Introduction:- Frequency is the number of occurrences of a repeating event per unit of time. The formula…
06 Jul 2020 03:57 PM IST
Project - Rankine cycle Simulator (MATLAB)
AIM: To create a basic 'RANKINE CYCLE SIMULATOR'. THEORY: The Rankine cycle is the fundamental operating cycle of all power plants where an operating fluid is continuously evaporated and condensed. The selection of operating fluid depends mainly on the available temperature range. The above figure shows us the basic rankine…
03 Jul 2020 10:43 AM IST
Curve fitting (MATLAB)
AIM: To write a program to fit a linear and cubic polynomial for the specific heat data set then calculate the goodness of fit using different parameters and different ways to improve the fit in MATLAB THEORY: Curve fitting is the process of constructing a curve or mathematical function that best fits the data points,…
03 Jul 2020 10:24 AM IST
Solving second order ODEs (MATLAB)
Aim: To solves the ODE which represents the equation of motion of a simple pendulum with damping. Objective: To write a program that solves the following ODE which represents the equation of motion of a simple pendulum with damping and create an animated video of output obtains by solving this ODE. Theory:…
03 Jul 2020 10:20 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.