Simulation of Transient behaviour of a Simple Pendulum

OBJECTIVE – To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of its motion.



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. 

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.

Assumptions made for calculation purposes,

L=1 metre,

m=1 kg,


g=9.81 m/s2.



  • The required modules like numpy, matplotlib.pyplot and scipy modules are imported.
  • The governing equation of motion for the simple pendulum are utilized to create the state variable and defined under a function.
  • Input variables like length of string, damping coefficient ,acceleration due to gravity, mass of ball are initiated.
  • The initial conditioned and time span are defined.
  • Odeint command is called and inputs are specified.
  • A plot of angular displacement and angular velocity are created as functions of time.
  • For loop which iterates through the values of angular displacement and angular velocity is defined.
  • Vector decomposition is used to determine the location of the moving pendulum.
  • Counter is setup in the loop,a filename is devised to take on the name of the count for ever subsequent o=plot of the pendulum position that is generated.
  • The plt.savefig() command is used to save the plots in series under the filename and corresponding count.
  • The plot images are stitched together using ImageMagick to generate an animation of the simple pendulum.
#code for simulation of a simple pendulum

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

def pendulum(theta, t, b, g, l, m):
			theta2 = theta[0]
			omega =theta[1]
			theta_dot = [omega, -(b/m)*omega -(g/l)*math.sin(theta2)]

b =0.05
g = 9.81
l = 1
m = 1

IC = [0,3]
t = np.linspace(0,20,250)

theta = odeint(pendulum,IC,t,args =(b,g,l,m))
theta1 = theta[:,0]
omega1 = theta[:,1]

plt.plot(t,theta1,'b-',Label='Angular Displacement')
plt.plot(t,omega1,'r--',Label='Angular Velocity')
plt.ylabel('Anugular Variables')

ct = 1
for angle in theta1:
 			x0 = 0
 			y0 = 0
 			x1 = l*math.sin(angle)
 			y1 = -l*math.cos(angle)
 			filename = 'swing%05d.png'%ct
 			ct = ct + 1
 			plt.plot([-1.5,-1.5],[0,0],linewidth = 7)
 			plt.plot([x0,x1],[y0,y1],linewidth = 1)
 			plt.plot(x1,y1,'o',markersize = 20)



The youtube link to the animation file is given below,



CONCLUSION – Thus the program is written to simulate the transient behaviour of a simple pendulum and animation file is created.

Projects by Habishek Umapathy

AIM – To run a pipe flow simulation with an inlet Reynolds number of 100, 1000 and 10000. For each of these cases do the following Place the line probes at 95%, 90%, 85%of the pipe length. Compare the normalized velocity profile at each of these locations. N Read more

OBJECTIVE – We have to write the script which should take column numbers as the input and plot the respective columns as separate images. The plot labels should be extracted from the file. If there is a request for a plot between column 1(crank angle) and column 8 Read more

  OBJECTIVE – To write a program to perform curve fitting.   Curve Fitting is the process of constructing a curve or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically constr Read more

The Newton Raphson method is an iterative technique that is used to find out the roots of a real valued function. It uses the idea that a continuous and differentiable function can be approximated by a straight-line tangent to it. In this case Newton Raphson method is Read more

OBJECTIVE - To write a code for P_V diagram of an Otto cycle and calculate thermal efficiency




  • Initially state variables p1,t1,t3 and gamma a Read more

OBJECTIVE - 1. Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the data file. Calculate the molecular weight of each species and display it in the command window. Plot the Cp, Ent Read more

OBJECTIVE - Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function. PROCEDURE- The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural sele Read more

OBJECTIVE – To write code to fit a linear and cubic polynomial for the Cp data and plot the linear and cubic fit curves along with the raw data points.   PROCEDURE – Curve Fitting is the process of constructing a curve or mathematical functions, whic Read more


The End