Project

Simulation of transient behaviour of a simple pendulum and creating an animation of its motion

AIM: Simulation of the transient behaviour of a simple pendulum and creating an animation of its motion.

GOVERNING EQUATION:

A simple pendulum consists of a mass m hanging from a string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with the periodic motion. When the motion of a pendulum reduces due to an external force, the pendulum and its motion are damped. These periodic motions of gradually decreasing amplitude are damped simple harmonic motion.

By applying Newton's second law for rotational systems, the equation of motion for the pendulum can be obtained:

θ = anglular displacement

t = time

b = damping coefficient, 0.05

m = mass ; 1 kg

l = length of pendulum; 1 m

g = gravity; 9.81 m/s^2

time domain= (0,20); in seconds

Initial conditions; at t=0

θ =0 and ώ=3 rad/sec

OBJECTIVE:

  1. To solve second order ODE representing pendulum motion and plotting velocity, displacement vs time.
  2. To create an animation of the pendulum motion.

To solve the second-order ODE in PYTHON, we first convert it into two first-order ODE as shown below:

Ode solver in python takes the above matrix as input and provide the solution to second-order ODE.

PROGRAM:

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

#Solving 2nd order ODE to simulate pendulum behaviour

#pendulum constants
b=0.05
m=1
g=9.81
l=1
#defining initial conditions
theta_0=[0,3]
#time domain
t=np.linspace(0,20,150)

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

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

ang_disp=theta[:,0]
velocity=theta[:,1]

#plotting velocity and ang displacement graph
plt.plot(t,ang_disp,label='Angular Displacement')
plt.plot(t,velocity,label='Velocity')
plt.legend()
plt.xlabel('time (sec)')
plt.grid()
plt.xlim([0,20])
plt.show()

#Creating pendulum motion
ct=1
for p in ang_disp:
	x_bob=l*math.sin(p)
	y_bob=-l*math.cos(p)
	plt.figure()
	#rod
	plt.plot([0,x_bob],[0,y_bob],linewidth=8)
	#bob
	plt.plot(x_bob,y_bob,'o',markersize=30)
	#horizintal reference
	plt.plot([-0.4,0.4],[0,0],linewidth=5)
	#hinge
	plt.plot(0,0,'o',markersize=3)
	#vertical reference
	plt.plot([0,0],[0,-1],'--',linewidth=2)
	plt.plot
	plt.xlim([-1,1])
	plt.ylim([-1.2,0.2])
	plt.title('Pendulum Simulation')
	filename=str(ct)+'.png'
	plt.savefig(filename)
	ct=ct+1


STEPS:

1. Given pendulum constants(mass, length, damping coefficient and g) were defined.

2. theta_0 array created to define initial conditions.

3. t array was created using np.linespace command to create 150 equally spaced time intervals from 0 to 20 sec.

4. model function was created in which two first-order ODE as derived above were provided in a matrix form.

5. Using odeint solver, calling model and providing arguments, second-order ODE is solved and solutions(disp, velocity) along with time are saved in the matrix theta.

6. Angular displacement and velocity obtained were plotted against time.

7. To create the animation of pendulum motion, for loop was executed.

8. under for loop, the endpoint of pendulum rod was defined and line consisting of origin and endpoint was plotted for each time interval.

9. All the figures were saved sequentially using plt.savefig command.

10. All the figures were combined together to form animation of swinging pendulum.

ERROR FACED: No error encountered

OUTPUT:

The plot of displacement and velocity clearly states the motion of a pendulum reduces due to the damping effect. There is a gradual decrease in the amplitude of angular displacement and velocity.

Animation of pendulum motion: https://youtu.be/thxOzXkw8PU

CONCLUSION:

To simulate the transient behaviour of simple pendulum, second-order ODE was solved using PYTHON and animation of pendulum motion was created to describe the damping effect.

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.