Project

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

OBJECTIVE: Write a program to simulate the transient behavior of a simple pendulum and to create an animation of its motion.

What is ODE?

The number of the highest derivative in a differential equation. A differential equation of order 1 is called first order, order 2-second order, etc. Example: The differential equation y\" + xy\' – x3y = sin x is second-order since the highest derivative is y\" or the second derivative

ODE is important in engineering they provide lots of information and solving those ODE can solve many examples

An example is a Simple Pendulum

PROCEDURE :

In above fig, there is formula given which is in second-order we need to convert it into first order ode, first convert that into first order

after solving we get

dtheta1_dt=theta2
dtheta2_dt= -(b/m)*theta2 - (g/l)*sin(theta1)

where,

b= Damping coefficient

g= gravity

L = Length of pendulum

m= mass

If we provide the initial condition for displacement and initial velocity

initial condition [ 0;3] ....given

and need to define the time for how many time we need the ode to work

Time is given 0 to 20 second

FUNCTION CODE:

# function that returns 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

we have a function for this program so that we can change the value of damping coefficient, theta, gravity, etc to solve

MAIN CODE:

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

# function that returns 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

#intial condition 
theta_0 =[math.pi/4,0]

#time points
t = np.linspace(0,10,150)

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

#plot results
(1)
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()


ct = 1
for i in theta[:,0]:
	x0 =0
	y0 =0
	x1=  l*math.sin(i)
	y1 = -l*math.cos(i)

	plt.figure()
	plt.plot([-0.5,0.5],[0,0],\'black\',linewidth=15)
	plt.plot([x0,x1],[y0,y1],\'red\',linewidth=3)
	plt.plot(x1,y1,\'o\',markersize = 30)
	plt.xlim([-1.5,1.5])
	plt.ylim([-1.5,0.5])
	plt.grid()
	filename = str(ct)+\'.png\'
	plt.savefig(filename)
	ct =ct+1

As per the code, we are using a syntax called odeint to solve the ode problems

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

with a time span of 0 to 20 sec in this syntax there are two conditions used by odeint ie. one dependent and another independent.

t= time is independent and theta changes with respect to time so it is dependent

We get the results for displacement and velocity from initial to final.

We get the values for displacement in the first column and velocity column.

As we need to plot the ode for pendulum we need to plot first the plot for displacement and velocity with respect to time.

Using for loop consider the first column as it is for the displacement result.

The initial and final condition of displacement of the pendulum and fix the axis for creating a pendulum we need to use marker command and give the size of the marker and set the speed

when theta values are changed the plot gets changed,in the above figure

end the for loop

and create a movie

Images are created we need to convert them into a gif file.

There are lots of apps which create images into gif file or it is done online also.

CONCLUSION :

  • The plot of displacement vs time
  • Created function to define the theta values
  • By using ODEint solved ode function
  • Showed the pendulum behavior with respect to time
  • Created a gif file to show change of position of the pendulum
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.