Week 3 - Solving second order ODEs
Theory-In Engineering,ODE is used to describe the transient behaviour of a system.a simple example is a pendulum
The way pendulum moves depends on the newtons second law.when this law is written down ,we get a second order ordinary differential equation that describes the position of the ball with respect to time.
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
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))
plt.plot(t,disvelo[:,0],'-',label='displacement')
plt.plot(t,disvelo[:,1],'--',label='velocity')
plt.legend()
plt.xlabel('time')
plt.ylabel('displacementvelocity')
plt.show()
y=disvelo[:,0]
ct=1
for ang in y :
x1=0;y1=0
x2=1*math.sin(ang)
y2=1*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.xlime([-1.5,1.5])
plt.ylime([-1.5,1.5])
plt.savefig(file)
ct=ct+1