All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping. In the above equation, g = gravity in m/s2, L = length of the pendulum in m, m = mass of the ball in kg, b=damping coefficient. 2. Write a program in Python that will simulate the pendulum…
Prakash Shukla
updated on 19 Dec 2022
AIM:
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
2. Write a program in Python that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
THEORY:
A pendulum is a body suspended from a fixed support so that it swings freely back and forth under the influence of gravity. 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.
Damping suggests that the angular amplitude decreases exponentially over time. This means that the amplitude of the pendulum will go on decreasing with time and eventually the pendulum will stop.
In Engineering, ODE is used to describe the transient behaviour of a system. A simple example is a pendulum. The way in which pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.
In this project we will simulate the motion of the simple pendulum with damping by solving the second order ODE which represents the equation of motion of a simple pendulum with damping.
In this project we solve the second order ODE by considering the following:
θ=θ1
dθdx=dθ1dx=θ2
d2θdt2=d2θ1dt2=dθ2dt
d2θ1dt2+bm(dθ1dt)+glsinθ1=0
dθ2dt+bm(θ2)+glsinθ1=0
dθ2dt=−bm(θ2)−glsinθ1
ddx[θ1θ2]=[θ2−bm⋅θ2−glsinθ1]
This is a system of ODEs with two first order ODE in it. We use this to get our solution.
PROGRAM:
Programming language used is Python.
Code to simulate the transient behaviour of a simple pendulum and to create an animation of it's motion using second order Ordinary Differential Equation in Python.
# 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
# Defining ODE function
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=1
theta_0=[0,3]
t = np.linspace(0,20,150)
# Solve ODE
theta = odeint(model,theta_0,t, args = (b,g,l,m))
# Plotting the graph
plt.plot(t,theta[:,0],'b-',label=r'$\frac{d\theta_1}{dt}=\theta2$')
plt.plot(t,theta[:,1],'r--',label=r'$\frac{d\theta2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{l}sin\theta_1$')
plt.ylabel('Plot')
plt.xlabel('Time(second)')
plt.title('Damped Harmonic motion of a Simple Pendulum')
plt.legend(loc='best')
plt.show()
# Iteration using for loop to create animation of motion of pendulum
p=theta[:,0]
ct=1
x0=0
y0=0
for i in p:
x1= x0 + (l* math.sin(i))
y1= y0 - (l* math.cos(i))
ct =ct+1
filename = '%05d.png' % ct
plt.figure()
# Plotting the rigid support through which string of pendulum is attached
plt.plot([-.5,.5],[0,0],'b-', linewidth = 10,color= 'brown')
plt.plot([x0,x1],[y0,y1],linewidth=4) # Plotting String of pendulum
plt.plot(x1,y1,'o',markersize = 25) # Plotting Bob of pendulum
plt.xlim([-2,2])
plt.ylim([-2,1])
plt.title('Simple Pendulum')
plt.savefig(filename)
EXPLANATION OF THE PROGRAM:
def ode_function(theta,t,b,g,l,m):
Above function consists of theta, time, damping co-effcient, length of pendulum, mass of bob, acceleration due to gravity. As explained in calculation part, Θ1 is displacement function and Θ2 is angular velocity function.
l = 1m, m = 1kg, b = 0.05, g = 9.81 m/s^2.
theta = odeint(ode_function,theta_0,t_span,args = (b,g,l,m))
Above function made use of odeint function of scipy module.
Therefore, dispacement results are plotted as;
plt.plot(t, theta[:,0]) = it implies theta should consider first column and all rows. It will give response of displacement curve w.r.t time with red color.
plt.plot(t, theta[:,1]) = it implies thata should consider second row and all rows. It will give response of velocity curve w.r.t time with green dashed line.
Legend function used will display annotations of curve at "best" suited location.
After this position of the pendulum is plotted at particular time in the time span and captured frame by frame to make an animation.
Pendulum movement against time :-
Simulation of the pendulum motion.
GIF. file :-
Youtube link for video file of motion of pendulum :-
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...
Project - Analysis of a practical automotive wiring circuit
1. Identify each of the major elements in the above automotive wiring diagram. Ans. a) Battery b) Ignition Switch c) Starter d) Fuel on off switch e) …
27 Nov 2023 06:15 PM IST
Week 5 - Curve fitting
1. What does popt and pcov mean? (Watch the video to get some context) The curve_fit() function returns two items, which we call popt and pcov. popt- The popt argument are the best-fit parameters (p optimal) for a and b. An array of optimal values for the parameters which minimizes the sum of squares…
21 Dec 2022 09:18 AM IST
Week 3 - Solving second order ODEs
AIM: Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping. In the above equation, g = gravity in m/s2, L = length of the pendulum in m, m = mass of the ball in kg, b=damping coefficient. 2. Write a program in Python that will simulate the pendulum…
19 Dec 2022 01:06 PM IST
Week 2 Air standard Cycle
Based on the concepts you have learnt during the forward kinematics routine, go ahead and write code that can solve an otto cycle and make plots for it. Here are the requirements 1. Your code should create a PV diagram 2. You should output the thermal efficiency of the engine. There are 4 process in otto cycle :-…
18 Dec 2022 05:24 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.