Project

Program to simulate the transient behaviour of a simple pendulum using python

Aim: Program to simulate the transient behaviour of a simple pendulum and to create an animation of it\'s motion.

Introduction:

A simple pendulum consists of a ball (point-mass) m hanging from a (massless) 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 periodic motion. By applying Newton\'s secont law for rotational systems, the equation of motion for the pendulum may be obtained.

This ODE represents the equation of motion of a simple pendulum with damping

In the above equation,

g = gravity in m/s^2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

using,

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s^2.

\"Sample

Program:

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


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



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

# intial condition

th = [0,3]

t = np.linspace(0,20,250)

theta = odeint(model,th,t,args=(b,g,l,m))

plt.plot(t,theta[:,0])
plt.plot(t,theta[:,1])
plt.show()


solv_od = theta[:,0]

count = 1
x0 = 0
y0 = 0
L = 3

for i in t:
	for s in solv_od:
	


		pos_x = x0+L*math.sin(math.radians(180)+s)
		pos_y = y0+L*math.cos(math.radians(180)+s)

		filename =\'0.5%d.png\' % count
		count = count+1
		plt.figure()
		plt.plot([-0.25,0.25],[0,0],linewidth=10)
		plt.plot(x0,y0,marker = \'*\', markersize = 8)
		plt.plot([x0,pos_x],[y0,pos_y],linestyle =\'--\')
		plt.plot(pos_x,pos_y,\'o\',markersize=20)
		plt.xlim([-4,4])
		plt.ylim([-5,1])
		plt.grid(True)
		plt.title(\'Simple pendulum\')
		plt.savefig(filename)

Explanation Of Code:

  • \"scipy\",\"math\",\"matplotlib.pyplot,\"odeint\",\"numpy\", modules are used to run the neccesary functions.
  • Giving initial conditions value to the function.
  • \"odeint\" command used to perform the ode solver function and store the value in theta in the form of array .
  • Using np.linspace command to equall space the interval for 250 iteration.
  • For defining the position of the hinge, support and bob are placed using the x0,y0 and x1,y1 with their values.

Errors faced while programming:

solved by putting colon operator

Output:

Animation:

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.