Motion of Pendulum using ODE
clc
clear all
close all
b = 0.5;
g = 9.81;
l = 1;
m = 1;
theta_0 = [0;3];
t_span = linspace(0,20,200);
[t , results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
ct = 1;
for i = 1:length(results(:,1))
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
plot([x0,x1],[y0,y1],\'linewidth\',2)
axis([-2 2 -1.5 0.5]);
rectangle(\'Position\',[-1 0 2 0.2],\'FaceColor\',[0 0.1 0.1]);
viscircles([x1 y1],0.20);
grid on;
grid minor;
M(ct) = getframe(gcf);
ct = ct + 1;
end
movie(M)
videofile = VideoWriter(\'Pendulum_animated\', \'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
figure(2)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'plot\')
xlabel(\'time\')
title(\'Plot showing variation of distance and velocity of pendulum wrt time\')
grid on
grid minor
legend(\'Displacement\',\'Velocity\')
FUNCTION:-
function [dtheta_dt] = odependulum(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
YOUTUBE ANUMATION LINK:-
https://youtu.be/U4OPbJwXcDk