Project

program that solves the following ODE This ODE represents the equation of motion of a simple pendulum with damping

objective = To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

Input Values

L=1 metre,

m=0.1 kg,

b=0.02.

g=9.81 m/s2.

program in Python that will simulate the pendulum motion

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

# 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

# Inital and Final condition
# pi/2 to 0

theta_0=[math.pi/2,0]

# Time points
t=np.linspace(0,50,500)

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

#Plot result
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.savefig(\"Plot\")
plt.show()


#Simple Pendulam Animation

p3=[0,0]
p4=[]
theta_new=theta[:,0]

ct=1
for i in theta_new:
	x=l*math.sin(i)
	y=-l*math.cos(i)
	p1=[-1,0]
	p2=[1,0]
	X=[p1[0],p2[0]]
	Y=[p1[1],p2[1]]
	X1=[p3[0],x]
	Y1=[p3[1],y]
	filename=str(ct)+\".png\"
	ct=ct+1
	
	
	plt.figure()
	plt.plot(X,Y,linewidth=10,marker=\'o\',markersize=5)
	plt.plot(X1,Y1,marker=\'o\',markersize=20)
	plt.xlim(-2,2)
	plt.ylim(-2,0)
	plt.savefig(filename)



	









Graph

Blue waves shown angular displacement

Red waves shown angular velocity

Animation of Simple pendulam

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.