Project

Simulating the transient behavior of a simple pendulum using Python Programming

TITLE: Simulating the transient behavior of a simple pendulum using Python Programming.

SUMMARY: In this project, using the second order differential equation of damped oscillatory motion we have visualize its displacement and velocity followed by creating the animation of simple pendulum.

INTRODUCTION: We know that in Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum. A damped oscillation means an oscillation that fades away with time. It can also be compared as a simple harmonic motion, where the object moves to and fro. 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.

Damped oscillatory motion & Ordinary Differential Equation

Above ODE describes the phenomenon of motion of a simple pendulum or a mass, spring & damped system. By solving these equations we will be able to simulate any of the systems.

From above equation,

g = gravity in m/s^2

l = Length of pendulum in m

m = mass of ball in Kg

b = damped co-efficient.

Now, in order to solve given ODE and achived final results its important to break those secon order differential equation in first order and solve them therefore we have as follows

also, we get

Using above obtained equations we code as follows,

  • Python programming for Simple Pendulum
import numpy as np 
from scipy.integrate import odeint
import matplotlib.pyplot as plt 
import math

#function that return dz/dt
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

b = 0.02
g = 9.81
l = 1
m = 0.1
#initial condition
theta_0 = [math.pi/2,0]
#time point
t = np.linspace(0,10,150)
#solve ODE
theta = odeint(model, theta_0, t, args = (b, g ,l, m))
#plot results
plt.plot(t, theta[:,0], 'b-', label = r'$\frac{d\theta_1}{dt}=\theta2$')
plt.plot(t, theta[:,1], 'r--', label = r'$\frac{d\theta_2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{l}sin\theta_1$')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()

#animation
ct = 1
x0 = 5
y0 = 5

for i in range(len(t)):
	x1 = x0+l*math.sin(theta[i,0])
	y1 = y0-l*math.cos(theta[i,0])
	filename = 'simple_pendulum' + str(ct) + '.png'

	#Animaton plotting
	plt.figure()
	plt.plot([4,6],[5,5], 'k-')
	plt.plot([x0,x1],[y0,y1])
	plt.plot(x1,y1,marker='o', markersize= '10')
	plt.axis([3.5,6.5,3,6])
	plt.savefig(filename)
	ct = ct+1

A Brief explanation

We begin with Numpy as well as matplot libraries follow by installing the scipy module for integration purpose. We also define certain functions as 'theta, t, b, g, l, m' inside the model and we call once they are defined.Values of theta are assigned as theta_1 for theta(0) followed by theta_2 for theta(1). Then the final equation is broken down and is equated as per required.

Later we assigned the values for b, g, l, m and provide the initial condition if theta_0 as [math.pi/2,0] followed by the timeploting and last the equations are solved using odeint 'command' . Now we plot the obtained result of graph and its parameters are also defined as per the required, which were done by plotting the apppropriate command. Ultimately we create an animation using a counter variable for 1 and assignong the X0 & Y0 values for 5. The generated .png files are saved and are converted into a GIF animation.

RESULTS:

OUTPUT

Animation can be visualize as follows,

CONCLUSION:

  1. theta_0 ranges between 0 to 3 m/s^2
  2. ODE has been solved using the odeint command which were installed as per scipy module
  3. Function model has been created with required arguments inside which, they are broken down and solved.
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.