Week 3 - Solving second order ODEs
https://drive.google.com/drive/folders/16ERKcFqdGH9AuUsWjOaaaLZK8cVCykOi?usp=sharing
% inputs for pendulum %
b=0.05;
g=9.81;
l=1;
m=1;
theta_0=[0;3];
t_span=linspace(0,20,200);
[t, results] = ode45 (@(t,theta) ode_function(t,theta,b,g,l,m) , t_span, theta_0);
% plot %
plot(t, results (:,1))
hold on
plot(t, results (:,2))
xlabel('time')
ylabel('plots')
legend ('displacement', 'velocity') ;
hold off
ct=1;
B=(results (: ,1)) ;
% Animation %
for i=1: length(B)
B1=results(i,1);
X0=0;
Y0=0;
Y1=-1*cos(B1);
X1=1*sin(B1) ;
plot ( [X0 X1], [Y0 Y1], 'linewidth',8)
hold on
plot (X1, Y1,ct ,20, 'r');
plot ([-0.2 0.2], [0 0], 'linewidth',7)
axis ([-0.6 1.2 -1.5 0.11]);
title('OSCILATION OF SIMPLE PENDULUM' )
legend (' connecting tread(rod) ', 'mass', 'fixed position' ) ;
xlabel (' X-axis')
ylabel('Y-axis')
hold off
% Making video %
M(ct)= getframe(gcf);
ct=ct+1;
end
movie (M)
videofile=VideoWriter('simple pendulum.avi', 'Uncompressed AVI');
open (videofile);
writeVideo (videofile,M) ;
close(videofile);
function [dtheta_dt]= ode_function(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