Week 4.1 - Solving second order ODEs
https://screenrec.com/share/YdmOqrhL5B
clear
close
clc
% inputs
m=1
b=0.05
l=1
g=9.81
% initial condition
theta_0=[0,3]
% time points
t_span=linspace(0,20,300)
% solve ode
[t,results]=ode45(@(t,theta)ode_fun(t,theta,b,g,l,m),t_span,theta_0)
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('angular velocity')
% animation
x0=0;
y0=0;
ct=1;
for i=1:length(t_span);
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
figure(2)
plot([-0.5 0.5],[0 0],'LineWidth',10,'Color','k');
axis([-1.5 1 -1.5 1]);
line([x0 x1],[y0 y1],'linewidth',3,'color','r');
hold on
plot(x0,y0,'o','MarkerSize',6,'MarkerFaceColor','b')
plot(x1,y1,'o','MarkerSize',25,'MarkerFaceColor','g')
grid on
hold off
M(ct)=getframe(gcf)
ct=ct+1;
end
movie(M)
videofile=VideoWriter('Simple_motion.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile);