Solving second order ODEs
CODE :
b=0.5;
l=1;
m=1;
g=9.81;
theta_0=[0;4];
t_span=linspace(0,20,400);
[t,result]=ode45(@(t,theta)ode_function(b,g,l,m,t,theta),t_span,theta_0);
figure(1)
hold on
plot(t,result(:,1),\'color\',\'r\');
plot(t,result(:,2),\'color\',\'b\');
xlabel(\'Time\')
ylabel(\'Displacement / velocity\')
legend(\'Displacement\',\'velocity\')
grid on
ct=1;
for i=1:length(result(:,1))
x0=0;
y0=0;
x1=1*sin(result(i,1));
y1=-1*cos(result(i,1));
figure(2)
plot([-2 2],[0 0],\'linewidth\',3,\'color\',\'r\');
line([x0 x1],[y0 y1],\'linewidth\',4);
axis([-2 2 -2 2])
hold on
plot(x1,y1,\'o\',\'markers\',20,\'markerfacecolor\',\'g\');
hold off
grid on
M(ct) = getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter(\'Second_order_ODE.avi\',\'Uncompresse avi\')
open(videofile)
writeVideo(videofile,M)
close(videofile)
function[dtheta_dt]=ode_function(b,g,l,m,t,theta)
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
OUT PUTS: