Week 3 - Solving second order ODEs
b=0.05
g=9.81
l=1
m=1
%initial conditions
the_0=[0,3]
%time points
t_span=linspace(0,20,500)
%solve ode
[t,result]=ode45(@(t,the) ode_f(t,the,b,g,l,m), t_span,the_0)
figure(1)
hold on
plot(t,result(:,1))
plot(t,result(:,2))
hold off
frame=1
figure(2)
for i=1:length(t_span)
mac=result(i,1);
x_cord=l*sin(mac);
y_cord=-l*cos(mac);
plot([0,x_cord],[0,y_cord])
line([-0.5,0.5],[0,0])
ball=viscircles([x_cord,y_cord],0.05);
grid on
axis([-2 2 -2 1])
pause(0.00001)
M(frame)=getframe(gcf);
frame=frame+1;
end
movie(M)
videofile=VideoWriter('Simple_Pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
% function code
function [dthe_dt,a]=ode_f(t,the,b,g,l,m)
the1=the(1)
the2=the(2)
dthe1_dt=the2
dthe2_dt=-(b/m)*the2-(g/l)*sin(the1)
dthe_dt=[dthe1_dt;dthe2_dt]
a=length(the)
end
