b = 0.05; % Damping Coefficient
g = 9.81; % Gravity in m/s2
l = 1; % Length of Pendulum in m
m = 1; % Mass of the ball
% 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)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
title(\'Motion of a Simple Damped Pendulum\')
ylabel(\'Plot\')
xlabel(\'Time\')
legend(\'Displacement(rad)\',\'Velocity(rad/s)\')
hold off
% Pendulum Animation
figure(2)
O = [0 0];
axis(gca,\'equal\'); % Aspect Ration of The Plot
axis([-1 1.2 -1.5 0.2]); % Limits of the Plot
grid on
ct=1; % Loop for Animation
for i=1:length(t_span)
%Mass point
P = l*[sin(results(i,1)) -cos(results(i,1))];
%Circle of origin
O_circ = viscircles(O,0.01);
%Pendulum
pend = line([O(1) P(1)],[O(2) P(2)]);
%Ball
ball = viscircles(P,0.05);
%Time intervals at which the plots should be updated
pause(0.001);
M(ct) = getframe(gcf);
ct = ct+1;
%Deletes previous object if they are not in the final loop
if i<length(t_span)
delete(pend);
delete(ball);
delete(O_circ);
end
end
% Video of the animation
movie(M)
videofile = VideoWriter(\'Pendulum.AVI\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)