Project

PYTHON program to simulate the transient behaviour of a simple pendulum and to create an animation of it's motion.

AIM :

To simulate the transient behavior of a simple pendulum and to create an animation of its motion.

GOVERNING EQUATIONS :

To solve the given equation, we need to transform the given second order ODE into first order ODE as,

⇒ θ=θ1,

⇒d(θ)dt=d(θ1)dt=θ2,

⇒d2θdt=d2θ1dt=d(θ2)dt,

⇒d2θdt+(bm)⋅d(θ)dt+(gL)⋅sinθ=0,

⇒d2θ1dt+(bm)⋅d(θ1)dt+(gL)⋅sinθ1=0,

⇒d(θ2)dt+(bm)⋅θ2+(gL)⋅sinθ1,

∴,d(θ1)dt=θ2

and d(θ2)dt=−(bm)⋅θ1−(gL)⋅sinθ1

OBJECTIVE :

  • To write a program that solves the second order ODE corresponding to motion of a simple pendulum and to plot its angular displacement and angular velocity wrt time.
  • To create the animation of the motion of the pendulum.

THEORY: A simple pendulum is defined to have an object that has a small mass, also known as the pendulum bob, which is suspended from a light wire or string, such as shown in Figure.

When the bob of pendulum is displaced from it's mean position to either extreme position and is released, the pendulum will swing back and forth with periodic motion until the motion damps. By applying Newton's second law for rotational systems, the equation of motion for the pendulum may be obtained as

τ=I⋅α

⇒ −mg⋅sinθ⋅L = mL2⋅d2θdt2

and rearranged as d2θdt2+(gL)⋅sinθ=0

and if a damper is introduced in the system the above equations changes slightly as

d2θdt+(bm)⋅d(θ)dt+(gL)⋅sinθ=0

BODY OF THE CONTENT :

  1. Math, matplotlib, numpy and scipy modules were imported for their various functions.
  2. Initial inputs such as b, g, l, m are provided according to data in the question.
  3. An array of initial theta was created to define initial angular displacement and initial angular velocity.
  4. An array of a time span was created to store 100 equally spaced intervals between time 0 to 20 seconds.
  5. To solve the second order ODE, a function named ode_function which stores the above matrix data in it.
  6. The given second-order ODE was solved using 'odeint' command followed by the proper syntax of the calling function and the result obtained was used to plot the graph of variation of angular displacement and angular velocity wrt time.
  7. Now for creating the animation of the obtained harmonic motion of the pendulum, for loop was used and a loop counter variables were initiated, the values of angular position are stored in a separate variable called 'tmp' and are iterated in for loop. In this execution, the one end of the string of pendulum was fixed and the other end moves according to the solution of the ODE solved and was plotted for each time interval of the time span.

CODE:

''' A program for solving second-order ODEs and simulate its motion'''
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint

def ode_function(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 
# damping coefficient
b = 0.05
# acceleration due to gravity(m/s^2)
g = 9.81  
# length of string (m)                                                               
l = 1  
# mass of bob (kg)                                                                  
m = 1      

# Initial Conditions
theta_0 = [0,3] 
# time points
t_span = np.linspace(0,20,100)

# Solve ODE

theta = odeint(ode_function,theta_0,t_span,args = (b,g,l,m))

# Plotting the graph
plt.plot(t_span,theta[:,0],'b-',label=r'$frac{dtheta1}{dt}=theta2$')
plt.plot(t_span,theta[:,1],'r--',label=r'$frac{dtheta2}{dt}=-frac{b}{m}theta2-frac{g}{l}sintheta1$')
plt.xlabel('Time (second)')
plt.ylabel('Plot')
plt.title('Damped Harmonic motion of a Simple Pendulum')
plt.legend(loc='best')
plt.grid()
plt.show()

# Iteration using for loop to create an animation of the motion of pendulum

ct =1
tmp = theta[:,0]
for i in tmp:
	x_start = 0                                                           
	y_start = 0
	x_end =  l*math.sin(i)                                          
	y_end = -l*math.cos(i)
	# Plotting for animation
	plt.figure()
	# Plotting String of pendulum
	plt.plot([x_start,x_end],[y_start,y_end],linewidth=3)
	# Plotting Bob of pendulum
	plt.plot(x_end,y_end,'o',markersize=20,color='r')
	# Plotting the rigid support through which string of pendulum is attached
	plt.plot([-0.5,0.5],[0,0],linewidth=8,color='k')
	# Plotting hatched vertical line for refernce
	plt.plot([0,0],[0,-1.5],'--',color='y')
	plt.xlim([-1.25,1.25])
	plt.ylim([-2,1])
	plt.title('Animation of transient behaviour of a Simple Pendulum')
	filename = 'test%05d.png' % ct
	plt.savefig(filename)
	ct = ct + 1

OUTPUT:

Animation link:

https://drive.google.com/file/d/1F5jJJhaiSAszoXhiANPAPdMAkd9gFVOg/view?usp=sharing

CONCLUSION :

Using the ordinary differential equation of simple pendulum and assuming angular velocity and displacement, an animation of simple pendulum was created.

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.