Project

Solving Second order ODE using Python - Simple Pendulum Animation

In mathematics, Ordinary Differential Equation (ODE) is a differential equation containing one or more functions of one independent variable and the derivatives of those functions.

In Engineering, ODE is used to describe the Transient Behaviour of system.

Here, motion of Simple Pendulum is also governing by Second Order ODE as below :-

#Python Program to Simulate Pendulum Motion by solving Second Order ODE :-

#Solving Second Order ODE - Simple Pendulum Animation using Python
#By DIPESH SALUNKE

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

#Defining Function that returns \'dtheta_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
b = 0.05
g = 9.81
l = 1.0
m = 1.0

#Initial Condition
theta_0 = [0,3]

#Time Points
t = np.linspace(0,20,200)

#Solving ODE
theta = odeint(model,theta_0,t,args = (b,g,l,m))

#Plotting Results 

#Plotting Angular Displacement
plt.plot(t,theta[:,0],\'b-\',label=r\'$\\frac{d\\theta_1}{dt}=\\theta_2$\')

#Plotting Angular Velocity
plt.plot(t,theta[:,1],\'r--\',label=r\'$\\frac{d\\theta_2}{dt}=-\\frac{b}{m}\\theta_2-\\frac{g}{L}sin\\theta_1$\')

#Limiting X-axis
plt.xlim([0,20])
#Limiting Y-axis
plt.ylim([-3,3])

plt.legend(loc=\'best\')
plt.xlabel(\'Time\')
plt.ylabel(\'Plot\')
plt.show()


ct=1                                #Counter Variable
for Theta in theta[:,0]:
	
	#Co-ordinates of Fixed Point of String
	x0 = 0
	y0 = 0
	#Co-ordinates of End Point of String
	x1 = l*math.sin(Theta)
	y1 = -l*math.cos(Theta)

	filename = str(ct) + \'.png\'
	ct = ct + 1

	plt.figure()
	#Plotting Horizontal Line
	plt.plot([-0.5,0.5],[0,0],\'g\',linewidth=5)
	#Plotting String of Pendulum
	plt.plot([x0,x1],[y0,y1],\'r\')
	#Plotting Ball of Pendulum
	plt.plot(x1,y1,\'o\',markersize=30)
	#Limiting X-axis
	plt.xlim([-1.5,1.5])
	#Limiting Y-axis
	plt.ylim([-1.5,1.5])
	#Title
	plt.title(\'Animation of Simple Pendulum using Python\')
	#Saving Each Figure with its file name
	plt.savefig(filename)


Code Explanation :-

  • Import numpy module, scipy module, matplotlib.pyplot module & math module.
  • Defining a function which is the system of ordinary differential equations.
  • Divide Second order ODE into two First order ODEs.
  • Assigning a given values to inputs.(b,g,l,m)
  • Assigning an initial condition at time(t=0) where initial angular displacement is 0 and initial angular velocity is 3 rad/s.
  • Time span is 0 to 20 sec. So, Assigning 200 equally spaced values from 0 to 20 in variable \" t \".
  • \"odeint\" command is in the scipy module. \"odeint\" is basically a function that integrate system of ordinary differential equations. For that defined a system(\'model\' function) above which is the system of two first order ODEs.
  • Assigning a function , initial conditions , time and input arguments to the \"odeint\".
  • Plotting Angular Displacement & Angular Velocity.
  • Used legend to describe the graph.
  • Used xlim & ylim to limit the X-axis & Y-axis.
  • Used xlabel & ylabel to indicate both axes.
  • Assigning value \'1\' to Counter Variable \'ct\'.
  • theta[:,0] is the set of values of Angular Displacement.
  • Assigning a co-ordinates to fixed point & end point of string of pendulum by using simple trigonometry.
  • ct is the counter variable which is used to assign a number to the filename(with one increment) with every iteration.
  • Plotting Fixed Horizontal Line , String & Ball of Pendulum.
  • Used \'xlim\' & \'ylim\' to limit the X-axis & Y-axis.
  • Used \'title\' to indicate this animation.
  • savefig(filename) is used to save each position of pendulum in seperate figure with its filename.

Result :-

Animation :-

https://youtu.be/NyvkLhW4RUo

=============#=============#==============#==============

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.