Week 3 - Solving second order ODEs
# Program
clear all
close all
clc
% Inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% Initial condition
theta_0 = [0;3];
% Time points
t_span = linspace(0,20,500);
% Solve ODE
[t , results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
% Plotting
figure (1)
hold on
title(' Damping analysis of a Pendulum')
xlabel('Time [s]')
ylabel('Plot')
plot(t,results(:,1))
plot(t,results(:,2))
legend('Angular Displacement','Angular Velocity')
grid on
% Using 'for' loop for pendulum animation
ct = 1;
for i = 1:length(results(:,1))
start_x = 0;
start_y = 0;
end_x = l*sin(results(i,1));
end_y = -l*cos(results(i,1));
figure (2)
grid on
plot([-1 1], [0 0],'linewidth',3)
hold on
plot([start_x end_x], [start_y end_y],'linewidth',3)
hold on
plot(end_x,end_y,'o','linewidth',20,'color','y')
title('Pendulum animation')
axis([-1.5 1.5 -1.5 1])
pause(0.03)
hold off
M(ct) = getframe(gcf);
ct = ct+1;
end
% Movie Animation
movie(M)
videofile = Videowriter('Pendulum Animation.avi','Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
# Output Video Link
https://drive.google.com/file/d/1RsFzCgvsBnvgDqdMgV-rFiZ61cNvEYSu/view?usp=sharing
(Link takes you to a zipped folder file. Kindly download the same for viewing the video.)
# All explaination and errors faced have been attached in PDF file.