Challenge

Simple Pendulum - Solving ODE & Animation

SIMPLE PENDULUM - ODE

Aim :

To create a program that solves an ODE which represents the equation of motion of a simple pendulum with damping and simulate the pendulum motion with the help of animation.

Description :

In Engineering, ODE is used to describe the transient behaviour 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.

In this project, I have taken the scenario for a simple pendulum with damping. The below mentioned 2nd Order Differential equation 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

The following values for these parameters are taken in this project,

L = 1 metre, m = 1 kg, b = 0.05, g = 9.81 m/s2.

This 2nd ODE is simplified into 2 single ODE and the equations are solved with the help of odeint function.

Program Code :

"""
Program to solve a ODE which represents the equation of motion 
 of a simple pendulum with damping and simulate the 
 pendulum motion with the help of animationfor 0-20 secs
"""

import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

#function definition
def model(start,t,b,l,g,m):
	t1 = start[0]
	t2 = start[1]
	dt1_dt = t2
	dt2_dt = (-b*t2/m)-(g*math.sin(t1)/l)
	dts = [dt1_dt,dt2_dt]
	return dts

#inputs
b=0.05	  #damping coefficient
l=1	  #length of pendulum
g=9.81	  #gravity
m=1	  #mass of ball

#initial condition
start = [0,3]

#time points
t = np.linspace(0,20,250)

#solving ODE
value = odeint(model,start,t,args=(b,l,g,m))

#plotting
plt.plot(t,value[:,0],'-',label='Displacement')
plt.plot(t,value[:,1],'--',label='Velocity')
plt.legend(loc='best')
plt.xlabel('Time')
plt.ylabel('Displacement, Velocity')
plt.show()

#Image creation for animation
count = 1
x1 = 0
y1 = 0

temp = value[:,0]

for i in temp:
	x2=l*math.sin(math.pi-i)
	y2=l*math.cos(math.pi-i)

	filename = str(count) + '.png'
	#incrementing the variable for the next iteration
	count = count + 1
	
	plt.figure()
	plt.xlim([-1.5,1.5])
	plt.ylim([-1.5,1.5])
	plt.plot([-1.5,1.5],[0,0],linewidth=6)
	plt.plot([0,0],[-1.5,1.5],'--')
	plt.plot([x1,x2],[y1,y2],linewidth=4)
	plt.plot(x2,y2,'o',markersize=15)
	plt.savefig(filename)
	#saving the file with a new name each iteration

Output :

Plot:

Animation :

( Various no. of images are merged into a single gif le for an animation )

Conclusion :

From the plot of time vs Displacement,Velocity, it can be observed that the graphs of velocity & displacement are dying down as time increases which is due to the damping of simple pendulum.

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.