Challenge

Week 3 - Solving second order ODEs

Aim :- To Write a program in python that will simulate the pendulum motion

Objectives :-

> program to solve second order ODE

> making animation of pendulum motion

Theory :-

ODE-ordinary differential equations

second order ODE of pendulum motion

b=damping coefficient

m=mass of ball in kg

g=gravity m/s2

L=length of pendulum in m

conversion :-

θ=θ1

(dθ/dt)=(dθ1/dt)=θ2

`frac{d^2theta}{dt^2}`=`frac{d^2theta1}{dt^2}`=`frac{d theta2}{dt}`

`frac{d^2theta}{dt^2}+(frac{b}{m})(frac{d theta}{dt})+(frac{g}{l})sintheta=0`

`frac{d^2theta1}{dt^2}+(frac{b}{m})(frac{d theta1}{dt})+(frac{g}{l})sintheta1=0`

`frac{d theta2}{dt}+frac{b}{m}theta2+frac{g}{l}sintheta1=0`

`frac{d theta2}{dt}=-frac{b}{m}theta2-frac{g}{l}sintheta1`

Input prameters:-

length(L)=1 m

mass(m)=1kg

Damping coiefficient (b)=0.05

Acceleration due to gravity(g)=9.81m/s2

sample code:-

#code for simple pendulum
import math
import numpy as n1
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def model(initial,t,b,l,g,m):
	t1=initial[0]
	t2=initial[1]
	dt1=t2
	dt2=(-b*t2/m)-(g*math.sin(t1)/1)
	dt=[dt1,dt2]
	return dt
#inputs
b=0.05
l=1
g=9.81
m=1

initial=[0,3]
t=n1.linspace(0,20,200)
disvelo=odeint(model,initial,t,args=(b,l,g,m))

#ploting time v/s displacement&velocity
plt.plot(t,disvelo[:,0],'-',label='displacement')
plt.plot(t,disvelo[:,1],'--',label='velocity')
plt.legend()
plt.xlabel('time')
plt.ylabel('displacement&velocity')
plt.show()
#creating plots  pendulum positions

y=disvelo[:,0]
ct=1
for ang in y:
	x1=0;y1=0
	x2=l*math.sin(ang)
	y2=-l*math.cos(ang)
	file='test%05d.png'%ct
	plt.figure()
	plt.plot([-1.5,1.5],[0,0],linewidth=8)
	plt.plot([x1,x2],[y1,y2],linewidth=3)
	plt.plot(x2,y2,'o',markersize=15)
	plt.xlim([-1.5,1.5])
	plt.ylim([-1.5,1.5])
	plt.savefig(file)
	ct=ct+1

code explanation:-

I have used odeint command to solve the two first order ODE equation in the fuction. The command have common syntax.it solve

the two first order ODE equation and givens the resuluts.The result are plotted. for animation of simple pendulum, we have separate the

displacement value from the solution .For different displacement value the string position are saved as aimage file.All images are stored in

separate files .All images changed into animation using image magik software

Output graph:-

output vedio link:-https://www.youtube.com/watch?v=0TSYraIZmKc

conclusion:-

time v/s displacement&velocity graph is obtained .

animation of pendulum is created.

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.