Project

Simulation of Transient Behaviour of Simple Pendulum by Animation

Simulation of Transient Behaviour of Simple Pendulum by Animation

Objective:

  • simulate the transient behaviour of pendulum in given timeframe by solving ODE in python.
  • create an animation of the results obtained from simulation.

Description:

  • The motion of pendulum is described by the following second order ODE :

  • where
    • b=damping coeffcient
    • m=mass of pendulum
    • g=gravity
    • l=length of pendulum

To solve this in python we need to convert it into two single order ODEs:

let θ=θ1 and

dθ1/dt=θ2 ----> 1

by substituting θ=θ1,we get

and finally

------> 2

we have now 2 single order ODEs which can be solved in python.

programming in python:

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

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

def pendulum(theta,t):
	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

initial=[0,3]
timeFrame=np.linspace(0,20,150)
simulation_values=odeint(pendulum,initial,timeFrame)

x0,y0,f=0,0,0
x2,y2=5,4
for i in simulation[:,0]:
	x1= l*math.sin(i)
	y1=-l*math.cos(i)
	plt.figure();plt.grid();plt.plot([-1,1],[0,0]);plt.xlim(-3,3);plt.ylim(-2,1);plt.plot((x0,x1),(y0,y1))
	plt.plot(x1,y1,\'o\',markersize=15)
	f=f+1
	plt.savefig(\"C:\\\\Users\\\\Admin\\\\Pictures\\\\gif2\\\\pen\"+str(f)+\".png\")

plt.figure()
plt.plot(timeFrame,simulation_values[:,0],\'b-\')
plt.plot(timeFrame,simulation_values[:,1],\'r--\')
plt.xlabel(\"time frame\")
plt.legend([\"angular displacement\",\"angular velocity\"])
plt.savefig(\"C:\\\\Users\\\\Admin\\\\Pictures\"+\".png\")

  • In order to solve ODEs, scipy.integrate module and for plotting, matplotlib.pyplot and for arrays, numpy modules are necessary
  • Initialization of variables are done and ODEs are defined in function named pendulum(theta,t)
  • To study the simulation of transient behaviour of pendulum we must specify the initial conditions and timeframe in which we want to study.
  • List named initial is initial conditions and array named timeFrame is time frame specified
  • odeint is a function of scipy.integrate which is used to solve ODEs it takes three arguments
    • 1- ODEs which are written in a function (here-function pendulum(theta,t))
    • 2-initial conditions for ODEs(here- list-initial)
    • 3-time frame in which we want to study the behaviour (here- list-timeframe)
  • Then solutions are obtained in simulation_values as list where it consists of two values simualtion_values[0] = angular displacement values and simulation_values[1] = velocity values
  • Then the results are plotted using matplotlib module
  • Here we plotted two plot
    • 1-the motion of pendulum, which can be plotted by using angular displacement (simulation_values[0] ) of pendulum and length of pendulum(l)
    • 2-angular displacement vs time and angular velocity vs time two comparisions in one plot.

commands used in program:

Performing Animation:

  • To perform animation here we used magick tool.
  • Go to command prompt change directory to location where these pictures are saved
  • Go to command prompt

>>> cd C:\\Users\\Admin\\Pictures\\gif2

>>> magick *.png output.gif

  • Then you will get a gif file named output in the same loction.

Results:

The following are the outputs of the code:

Conclusion:

  • Simulation of simple pendulum\'s transient behviour has been done sucessfully with scipy module in python.
  • Animation of the pendulum has been done with magick tool from the obtained plots.
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.