Pendulum simulation in Matlab
clear all
close all
clc
l=1;
m=1;
b=0.5;
g=9.81;
theta_0=[0;3];
t_span=linspace(0,20,500);
[t,results]=ode45(@(t,theta) odeFunc(t,theta,b,g,l,m),t_span,theta_0)
% plot(t,results(:,1))
% hold on
% plot(t,results(:,2))
p = results(:,1)
c=1;
for i=1:length(t)
x0=0;
y0=0;
x1=l*cos((1.5*pi)+p(i));
y1=l*sin((1.5*pi)+p(i));
plot([x0 x1],[y0 y1],\'marker\',\'.\')
axis([-2 2 -2 2])
pause(0.01)
M(c)=getframe(gcf);
%hold on
c=c+1;
end
movie(M)
videofile=VideoWriter(\'Pendulam.avi\')
open(videofile)
writeVideo(videofile,M)
close(videofile)
function [dtheta_dt]=odeFunc(t,theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2;
dtheta2_dt=-b*theta2/m-g*sin(theta1)/l;
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
Link for the video: https://youtu.be/6kkAg8rssjw