Week 3 - Solving second order ODEs
https://docs.google.com/document/d/15xWj-EOLbhuU3MI0QTIemlnZmyaeZ8LUDG39-1ojUV4/edit?usp=sharing
clear all; close all; clc;
%inputs
b=0.5;
g=9.81;
l=1;
m=1;
ct=1;
%initial conditions
t_theta=[0;3];
%time points
t_span=linspace(0,20,1000);
%solver ODE
[t,results]= ode45(@(t,theta) ode_func(theta,b,g,l,m),t_span,t_theta);
%plotting
figure(1)
hold on
subplot(2,1,1)
plot(t,results(:,1));
plot(t,results(:,2));
hold on
xlabel('Time')
ylabel('Plot')
for i=1:length (results(:,1))
x0=0;
y0=0;
x1=1*sin(results(i,1));
y1=-1*cos(results(i,1));
subplot(2,1,2)
plot([-1 1],[0 0],[x0 x1],[y0 y1],LineWidth=2);
grid on
pause (0.0002)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
%creating movie
movie(M)
videofile=VideoWriter('2nd Order ODE','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
function [dtheta_dt]=ode_func(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