All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Objective: To solve the following ODE of a simple pendulum with damping and then plot the displacement and velocity values on a graph d2θdt2+bmdθdt+glsinθ=0 where g = gravity in ms2, …
Abhishek Baruah
updated on 02 Mar 2021
Objective:
d2θdt2+bmdθdt+glsinθ=0
where
g = gravity in ms2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Problem Statement:
How does displacement and velocity vary with time and how can these values be used to simulate the pendulum
Solution Procedure:
As we want the angular displacement and angular velocity values stating from the initial position of 0 degrees and 3radians respectively, we have initialised those conditions also. And finally using the linspace command, created an array of 200 values between 0 and 20 corresponding to each time element we will be plotting the values in.
Now, by the use of the integration function from the scipy.integrate module, I have calculated each and every value of angular displacement and angular velocity up to 20 seconds and stored them in 2 columns of an array
Lastly, to complete the 1st part of the objective, I have used the plotting commands to plot the angular displacement vs time and angular velocity vs time graphs
Here is the detailed code for this:
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,m,g,l):
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)
#damping coefficient
b=0.05
#gravity in m/s^2
g=9.81
#length of the pendulum in m
l=1
mass of the ball in kg
m=1
#counter variable used for naming later
ct=1
#initial condition
theta_0=[0,3]
#creating 200 time value array between 0 and 20 seconds
t=np.linspace(0,20,200)
#integrate the equation
theta=odeint(model,theta_0,t,args=(b,m,g,l))
#plotting the graphs
plt.figure()
plt.plot(t,theta[:,0],label='Angular displacement')
plt.plot(t,theta[:,1],label='Angular Velocity')
plt.legend(loc='best')
plt.xlabel('Time in seconds')
plt.show()
Now for Part 2 of the objective, I have used trigonometry to calculate the positions of the pendulum for each point of time, used the marker size command to create the sphere and finally compiled 200 images corresponding t each position of the pendulum. And using imagemagick, stitched the images together to form the animation of the pendulum. Following is the detailed code for part 2:
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,m,g,l):
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)
#damping coefficient
b=0.05
#gravity in m/s^2
g=9.81
#length of the pendulum in m
l=1
mass of the ball in kg
m=1
#counter variable used for naming later
ct=1
#initial condition
theta_0=[0,3]
#creating 200 time value array between 0 and 20 seconds
t=np.linspace(0,20,200)
#integrate the equation
theta=odeint(model,theta_0,t,args=(b,m,g,l))
#for creating images of the pendulum corresponding to each position
for v in theta[:,0]:
posx=0-l*math.sin(v)
posy=1-l*math.cos(v)
ct=ct+1
filename= 'output%05d.png'%ct
plt.figure()
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.plot([0,posx],[1,posy])
plt.plot([-2,2],[1,1],'-y',linewidth=10)
plt.plot(posx,posy,'o',markersize=30)
plt.savefig(filename)
Errors Faced:
The error was occuring as I was using the array directly inside the sin/cos function which is not possible. Also, the posx and posy are calculated wrong which I figured out soon and changed before compiling.
Solution: Used a for loop instead where the local variable contained each value of θ in int format for every iteration
Results and Discussion:
From the graph, it is clear that both the angular displacement and velocity follows the oscillatory trajectory which can also be seen on the actual animation of the pendulum as shown below.
The animation for the pendulum can be found below:
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...
File Parsing and data analysis using python
Objective: To create a data visualiser using python, perform a compatibility check of the file and then calculating some basic performance parameters Problem Statement: To visualise the complex data in the file in a sequencial and a more understandable way. Also, how can SublimeREPL adds a more two way relationship in…
30 Mar 2021 06:17 PM IST
Curve fitting using Python
Objective: To find a good fitting function for showing the relationship between temperature and specific heat capacity at constant pressure using the practical values from the data file Procedure: First, we define a function f(x) which will be will give the relationship between the temperature (x) and Cp (y) values Then,…
27 Mar 2021 03:37 AM IST
Analysing ice breaking pressure using a air cushion vehicle by the use of Newton-Raphson method
Objective: Euler equation: (1−β2)p3+(0.4hβ2−σh2r2)p2+(σ2h43r4)p−(σh23r2)3=0 -Equation 1 where p denotes the cushion pressure, h the thickness of the ice field, r the size of the air cushion, σ the tensile strength of the…
07 Mar 2021 05:17 PM IST
Solving the ODE for simple pendulum followed by its simulation
Objective: To solve the following ODE of a simple pendulum with damping and then plot the displacement and velocity values on a graph d2θdt2+bmdθdt+glsinθ=0 where g = gravity in ms2, …
02 Mar 2021 01:38 AM 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.