Project

Pendulum animation using 2nd order ode

In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum

The way the 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.

Similarly, there are hundreds of ODEs that describe key phenomena and if you have the ability to solve these equations then you will be able to simulate any of theses systems.

This ODE represents the equation of motion of a simple pendulum with damping,

`((d^2theta)/(dt^2))+(b/m)*((d*theta)/dt)+(g/l)*sin(theta)=0`

where:

b - damping coefficient

l - Length of the string

g - acceleration due to gravity

m - the mass of the bob

The equation of simple pendulum using damping is a second-order differential equation(ODE). So, it must be divided into two lower-order differential equations as

(d theta_1)/dt = theta_2

((d theta_2)/(dt))=-(b/m)*(theta_2)-(g/l)*sin(theta_1)

Import numpy, matplotlib, math modules for array, plotting and math operations respectively.

  • To solve the ODE call the odeint module and given the define variables. Then plot graph between the time vs displacement and time vs angular velocity.
  • Create a loop to obtain animation of the motion of the simple pendulum bob and convert the obtained images into gif file by using the 'imgflip'.

Python Script:

import numpy as np 
from scipy.integrate import odeint
import math 
import matplotlib.pyplot as plt 
"""
damping: effects that reduce the amplitude of the vibrations
"""

def ode_func(theta,t,b,g,l,m):

	theta1=theta[0]
	theta2=theta[1]
	#first ode
	dtheta1_dt=theta2
	#second ode
	dtheta2_dt=-((b/m)*(theta2))-((g/l)*math.sin(theta1));
	dtheta_dt=[dtheta1_dt,dtheta2_dt]
	return dtheta_dt
b=0.05 # damping coeffient(material property that indicate the material to bounce back)
# material with high damping coefficient used as the shock absorbers
g=9.81 # acceleration due to the gravity
l=1 #length of the string
m=1 #mass of the bob

#intial conditions
theta_0=[0,3]
# time plot
t=np.linspace(0,20,200)
#T = len(t)
# solvimg the ode
theta = odeint(ode_func,theta_0,t,args=(b,g,l,m))
plt.figure(1)
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.xlabel('time(s)')
plt.ylabel('plot')
plt.legend(loc='best')
plt.show()
ite = 1
# Create a loop for the animation
for t in theta[:,0]:
	x0=0
	y0=0
	x1=(l*math.sin(t))
	y1 = -(l*math.cos(t))
	filename = str(ite)+'.png'
	ite=ite+1

	#plotting
	plt.figure()
	plt.plot([-0.5,0.5],[0,0],'black',linewidth=5) # plotting the base line of the pendulum
	plt.plot([x0,x1],[y0,y1],linewidth=3,color='r') 
	plt.plot(x0,y0,'o',markersize=5) # highlighting the point
	plt.plot(x1,y1,'o',markersize=30,color='blue') # highlighting the point
	plt.xlim([-1.5,1.5]) #defining the x limit.
	plt.ylim([-1.5,1.5]) # defining the y limit.
	plt.title('Animation of of Simple Pendulum')
	plt.savefig(filename)

The plot below represents the variation of the velocity and displacement for different amplitude and time.

GIF:-

https://drive.google.com/file/d/1DdiFgQCxAvS7m8n_wfi4keuru-kUuQxt/view?usp=sharing

Youtube link

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.