Challenge

Week 3 - Solving second order ODEs

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

Governing Equation :

second order differential equation

`(d^2θ)/dt^2+b/m(dθ)/dt+g/l(sinθ)=0`

where,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

Objective :

  1. write a program that solves the simple pendulam ODE.
  2. plot the animation of simple pendulum.

Steps of code :

  1. import the module required using import command
  2. function model is created with arguments
  3. define the input parameters for the equation
  4. define the condition for displacement and velocity
  5. odeint cammand is use to solve ode function
  6. plot the graph by ploting command
  7. to obtain the pendulum motion use the for loop . define the loop to get different value
  8. image are saved using savefig command

Python code:

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

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

# initial condition
theta_0 = [0,3]

# TIME POINT 
t = np.linspace(0,20,200)

# 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\theta2}{dt}=-\frac{b}{m}\frac{g}{L}sin\theta_1$')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()


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

	ode = str(ct) + '.png'
	ct = ct + 1

	plt.figure()
	# horizontal support
	plt.plot([-1,1],[0,0])
	# pendulum string
	plt.plot([x0,x1],[y0,y1])
	# bob
	plt.plot(x1,y1,'o',markersize=10)
	#limiting xaxis and yaxis
	plt.xlim([-2,2])
	plt.ylim([-2,1])
	plt.savefig(ode)

results :

Plot

Animation youtube link :

https://www.youtube.com/watch?v=bk0JvvGpFYo

Conclusion:

written program to simulate the behavior of simple pendulum amd created the animation . from the graph it can clearly seen that the value of angular displacement and angular velocity with time the effect is known as damping effect.

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.