Pendu
function [dtheta_dt] = ode_funct(t ,theta,b,g,l,m)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/(l*m))^2*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
clear all
close all
clc
b=0.5;
l = 1;
m = 1;
g = 9.81;
%initial conditions
theta_0 = [0;3];
%time points
t_span = linspace(0,10,100);
%solve ode
[t,results] = ode45(@(t,theta) ode_funct(t,theta,b,g,l,m),t_span,theta_0);
ang_disp=results(:,1);
ang_vel=results(:,2);
figure(1)
hold on
plot(t,ang_disp,\'r\')
plot(t,ang_vel,\'b\')
x0=0;
y0=0;
ct=1;
for i=1:length(t)
figure(2)
origin= [0 0];
p=[(l*sin(ang_disp(i))),(-l*cos(ang_disp(i)))];
pause(0.2)
plot([origin(1) p(1)],[origin(2) p(2)]);
axis([-1 1 -1.4 0.2]);
bob=viscircles(p,0.05);
M(ct)= getframe(gcf);
ct= ct+1;
end
movie(M);
videofile= VideoWriter(\'pendulum_animation.avi\',\'uncompressed AVI\');
open(videofile);
writeVideo(videofile,M);
close(videofile);
The youtube link for the video is: https://youtu.be/zFJM68yutEg