Week 3 - Solving second order ODEs
clear
clc
%% Inputs
b=0.1%%damping coeff
g=9.81%%gravitational acceleration
l=1;%% length of pendulum
m=1%% Mass of the object
%% Intial displacement is 0 & initial angular velocity is 3
Initial_theta=[0;3];
t_span=linspace(0,20,300)
[t results]=ode45(@(t,theta) odefunc(t,theta,b,g,l,m),t_span,Initial_theta);
figure(1)
plot(t, results(:,1));
hold on
plot(t,results(:,2))
%% Plotting the pendulum'
for i=1:length(results)
theta=results(i,1)
y=-l*cos(theta)
x=l*sin(theta)
figure(2)
plot([0 x],[0 y],"linewidth",3);
hold on
plot(x,y,'O','MarkerSize',40,'MarkerFaceColor','red')
axis([-1 1 -1.5 0]);
daspect([1 1 1]);
anim(i)=getframe;
hold off
end
movie(anim)
videofile= VideoWriter('Pendulum.avi')
%%Creating a video file& adding the movie in it
open(videofile)
writeVideo(videofile,anim)
close(videofile)