Challenge

Week 3 - Solving second order ODEs

Objective: To write a program that solves the ODE for the motion of a simple pendulum with damping.

Solution: For solving the ODE for simple pendulum with damping, we have to consider following parameters,

Pendulum will start its motion initially from angular displacement of 0 and with angular velocity of 3 rad/sec.

`theta` = Angular displacement

`(d theta )/ dt` = Angular velocity

b = damping coefficient

m = mass

g = gravity

L = length

To solve the ODE, follow the below mentioned code with equation mentioned in it.

Code:

#Solving 2nd order ODE for motion of pendulum
#By Dishank Bhavsar

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

#definition of ODE function
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

#defining parameters
b = 0.05
g = 9.81
l = 1
m = 1

#defining angular displacement & angular velocity 
theta_0 = [0,3]

#for time 0 to 20 and dividing it into 200 different positions for future plot creating purpose
t = np.linspace(0,20,200)

#using odeint function for ploting the graph
theta = odeint(model,theta_0,t,args = (b,g,l,m))

#ploting graph
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()

#For generating animation for the motion of pendulum, first we have to generate figures for time 0 to 20 seconds

c = 1
#using for loop for generating figures at every displacement between given period of time
for theta_c in theta[:,0]:
	x0 = 0
	y0 = 0
	x1 = l*math.sin(theta_c)
	y1 = -l*math.cos(theta_c)

	filename = 'pendulum' + str(c) + '.png'
	print(filename)

	c = c + 1
	plt.figure()

	plt.plot([-0.5,0.5],[0,0],'g',linewidth=5)

	plt.plot([x0,x1],[y0,y1],'r')

	plt.plot(x1,y1,'o',markersize=30)

	plt.xlim([-1.5,1.5])
	plt.ylim([-1.5,1.5])
	plt.title('Animation of simple pedulum using python')
	plt.savefig(filename)

Graph for Angular displacement and Angular velocity w.r.t. time:

Animation of Pedulum:

Discussion:

For generating graph after solving 2nd order ODE for pendulum with damping, the numpy module is used for generating plots and figures and scipy module has been used to solving ODE in python.

Then the time period is divided into 200 parts by linspace function between 0 to 20 seconds and with respect to that, change in angular displacement and velocity is considered.

For generating animation of pendulum movement, every single figure (total 200) is saved by using for loop and generating plot at every theta value. After that, animation is generated by merging all the figures with the help of online GIF maker.

Results:

We can observe that, damping causes the pendulum to slow down by looking at the graph.

Angular displacement and velocity decreases with time due to damping coefficient.

With increase in damping coefficient, the pendulum will stop its motion earlier.

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.