Week 3 - Solving second order ODEs
write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping:-
Writing a program in Matlab that will simulate the pendulum motion
useing,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
in this above equation
b=dampping co-efficent
m=mass of the ball (kg)
g=gravity (m/s^2)
L=length of the pendulam (m)
NEWTON'S SECOND LAW:-
"The amount of the acceleration of the body is propostional to the net force , and inversely propostional to the mass of the body". is called newtons second law.
F=m*a
where
F=net force
m= mass of the body
a= acceleration.
simple pendulam:-
A mass suspended from a light string that can oscillate when displaced from its rest position.
(d^2θ/dt^2)+dθ/dt+c=0 is a second order ode
(d^2θ/dt^2)+(b/m)dθ/dt+(g/L)sinθ=0 ----------------------------------------------(1)
where θ=θ1;
dθ/dt=dθ1/dt -------------------------------------------------------------------------------(2)
now we assume dθ1/dt=θ2 ------------------------------------------------(3)
d^2θ/dt^2=d^2θ1/dt^2
d^2θ1/dt^2=d/dt(dθ1/dt)==>dθ2/dt
d^2θ2/dt^2=dθ2/dt ----------------------------------------------------------------------(4)
now we can substitute the (2) and (4)eguations in eg.(1)
d^2θ2/dt+(b/m)dθ1/dt+(g/L)sinθ=0 ------------------------------------------------------(5)
from equqtion (3) we have
dθ1/dt=θ2 ; -------------------------------------------------------(3)
dθ2/dt=-(b/m)θ2-(g/L)sinθ1 -----------------------------------------------------------(6)
from the given data we have
angular dispalcement =0
angular velocity =3 m/s
time= 0
theta_0=[0:3];
t_span=linspace(0,20,300)
now we get 't' and output by using
function [dtheta_dt]=ode_func_m(t,theta,b,g,L,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/L)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
main program is
%inputs
b=0.05;
g=9.81;
L=1;
m=0.1;
%initial condition
theta_0=[0;3];
%time plots
t_span=linspace(0,20,300);
%solving ode
[t,results]=ode45(@(t,theta) ode_func_m(t,theta,b,g,L,m),t_span,theta_0);
figure(1)
plot(t,results(:,1));
hold on
plot(t,results(:,2))
hold off
xlabel('time')
ylabel('velocity')
figure(2)
plot(results(:,1),results(:,2))
theta2=results(:,1)
ct=1
for i=1:length(theta2)
THETA=theta2(i)
x0=0
y0=0
x1=L*sin(THETA)
y1=-L*cos(THETA)
figure(3)
plot([-0.5 0.5],[0 0],'linewidth',3,'color','b')
hold on
%plotting
plot([x0 x1],[y0 y1],'linewidth',3,'color','b')
plot(x1,y1,'o','markersize',20,'color','b','MarkerFaceColor','r')
hold off
axis([-2 2 -1.5 1.5])
M(ct)=getframe(gcf);
pause(0.02)
ct=ct+1
end
movie(M)
videofile=VideoWriter('second_order_ode.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
YOUTUBE LINK:https: //youtu.be/pbh4vd9hhHo