Week 3 - Solving second order ODEs
AIM:- Write a program in Matlab\Octave that will simulate the pendulum motion by using ODE function .
DESCRIPTION:-
In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum
The way the 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" w.r.t time.
simple pendulum has a small-diameter bob and a string that has a very small mass but is strong enough not to stretch appreciably. The linear displacement from equilibrium is s, the length of the arc. Also shown are the forces on the bob, which result in a net force of −mg sinθ toward the equilibrium position—that is, a restoring force.
GOVERNING EQUATIONS:-
Parameters of the system will be defined by
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2
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)/l)
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=01
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
result:- In this challenge we are successfully simulate simple pendulum by using Second order ODE by the help of python function and through which we create an simulation video .