All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Objective: Write a program to solve an ODE which represents the equation of motion of a simple pendulum with damping and animate a simple pendulum in motion. Simple Pendulum: A simple pendulum consists of a ball (point-mass) m hanging from a (massless) string of length L and fixed at a pivot…
Giridhar Lingutla
updated on 24 Feb 2021
Objective:
Write a program to solve an ODE which represents the equation of motion of a simple pendulum with damping and animate a simple pendulum in motion.
Simple Pendulum:
A simple pendulum consists of a ball (point-mass) m hanging from a (massless) string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. By applying Newton's secont law for rotational systems, the equation of motion for the pendulum may be obtained
τ=Iα=-mgsinθL=mL2d2θdtτ=Iα=−mgsinθL=mL2d2θdt
and rearranged as
d2θdt2+gLsinθ=0d2θdt2+gLsinθ=0
and if a damper is introduced in the system the above equation becomes
d2θdt2+bmdθdt+gLsinθ=0d2θdt2+bmdθdt+gLsinθ=0
In the above equation,
g = gravity in m/s^2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Governing Equations:
To solve the above equation in Python, we need to transform the given second order ODE into first order ODE:
suppose θ=θ1θ=θ1
dθdt=dθ1dt=θ2dθdt=dθ1dt=θ2 .(I)
d2θdt2=d2θ1dt2=ddt(dθ1dt)=ddt(θ2)d2θdt2=d2θ1dt2=ddt(dθ1dt)=ddt(θ2) .(II)
ODE: d2θdt2+bmdθdt+gLsinθ=0d2θdt2+bmdθdt+gLsinθ=0
Replacing θwithθ1θwithθ1
d2θ1dt2+bmdθ1dt+gLsinθ1=0d2θ1dt2+bmdθ1dt+gLsinθ1=0
by replacing the values with equation II
dθ2dt+bmθ2+gLsinθ1=0dθ2dt+bmθ2+gLsinθ1=0
dθ2dt=-bmθ2-gLsinθ1dθ2dt=−bmθ2−gLsinθ1
so we get two first order ODE's, dθ1dt=θ2dθ1dt=θ2,dθ2dt=-bmθ2-gLsinθ1dθ2dt=−bmθ2−gLsinθ1
Python Code:
"""
Simple Pendulum animation and ODE solution by Giridhar
"""
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
# 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
# Inputs
# damping coefficient
b = 0.05
# gravity(m/s^2)
g = 9.81
# length of the pendulum (m)
l = 1
# mass of ball (kg)
m = 1
# Initial Conditions
theta_0 = [0,3]
# time points
t = np.linspace(0,20,150)
# ode solution
theta = odeint(model,theta_0,t,args = (b,g,l,m))
# plotting the results
plt.plot(t,theta[:,0],'b-',Label = r'$frac{dtheta1}{dt}=theta2$(Displacement)')
plt.plot(t,theta[:,1],'r--',Label = r'$frac{dtheta2}{dt}=-frac{b}{m}theta2-frac{g}{l}sintheta1$(Velocity)')
plt.xlabel('Time (s)')
plt.ylabel('Plot')
plt.title('Harmonic motion of a Pendulum_Damped')
plt.legend(loc='best')
plt.savefig('Pendulum_Damped')
plt.show()
# Animation of Pendulum motion
ct =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)
filename = str(ct) + '.png'
ct = ct + 1
plt.figure()
# string
plt.plot([x_start,x_end],[y_start,y_end],linewidth=3)
# pendulum ball
plt.plot(x_end,y_end,'o',markersize=15,color='y')
# support
plt.plot([-1.25,1.25],[0,0],linewidth=5,color='k')
plt.plot([0,0],[0,-1.5],'--',color='r')
plt.xlim([-1.25,1.25])
plt.ylim([-1.5,1.5])
plt.title('Animation of a Pendulum')
plt.savefig(filename)
Plot:
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 decreases the amplitude and the velocity of motion.
Pendulum Motion Animation:
Errors:
Python Code Explanation:
Simple Pendulum Animation:
Conclusion:
A program in Python is created and succesfully executed to solve Ordinary Differential Equations of a damped pendulum and animated a simple pendulum in motion.
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...
Week 6 - Data analysis
https://skill-lync.com/projects/data-analysis-using-python-48
02 Mar 2021 07:35 PM IST
Data Analysis using Python
Objective: Extract a plot between any two quantities from the given data file automatically. Compatibility check: Code should exit gracefully, if a non-compatible file is provided as an input. Calculate the area under P-V diagram. Calculate the power output of an engine running at 1500 RPM. Calculate the specific fuel…
02 Mar 2021 07:35 PM IST
Curve Fitting using Python
Objective: Write a Python Program to curve fit a linear and cubic polynomial for the given Cp data and explain the results. Curve Fitting: Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to…
01 Mar 2021 12:41 PM IST
Find Minimum Cushion Pressure with Newton-Raphson method of an Air Cushion Vehicle to break Ice in Python
Objective: Write a Python program to find minimum cushion pressure of an Air Cushion Vehicle to Break Ice with Newton-Raphson method. Governing Equation: The factors involved are: p - cushion pressure h - thickness of the ice field r - size of the air cushion σ - tensile strength of the ice β -…
27 Feb 2021 05:20 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.