Simple Pendulum Animation- without damping
link to the video: https://youtu.be/r6PAKdZ4GMI
% Solving ODE for the pendulum and creating an animation
close all
clear all
clc
% constants and inputs g= m/s^2, length(l)=m, mass(m)= kg
g= 9.81;
l= 1.5;
m= 0.1;
% initial conditions
theta_0= [0;20];
% time points
t_span= linspace(0,10,60);
% solve ODE
[t, results]= ode45(@(t,theta) ODE_pendulum_func(t,theta,g,l,m), t_span, theta_0);
% plotting results
figure(1)
plot(t_span,results(:,1),\'color\',\'b\')
hold on
grid on
plot(t_span,results(:,2),\'color\',\'r\')
hold off
figure(2)
plot(results(:,1), results(:,2))
xlabel(\'position-radians\');
ylabel(\'angular velocity\');
% animation
ct=1;
for i= 1:length(results(:,1))
x0=0;
y0=0;
x1= l*sin(results(i,1));
y1= -l*cos(results(i,1));
figure(3);
plot([-1 1],[0 0],\'linewidth\',2,\'color\',\'k\');
hold on
line([x0 x1],[y0 y1],\'linewidth\',2,\'color\',\'b\');
plot(x1,y1,\'-o\',\'markers\',20,\'markerfacecolor\',\'r\');
axis([-2 2 -2 2])
pause(0.1)
M(ct) = getframe(gcf);
ct=ct+1;
end
movie(M)
videofile = VideoWriter(\'pendulum_challenge.avi\', \'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
*********Function used in the program************
function [dtheta_dt]= ODE_pendulum_func(t, theta, g,l,m)
theta1= theta(1);
theta2= theta(2);
w= g/(l*m);
dtheta1_dt= theta2;
dtheta2_dt= -(w^2)*sin(theta1);
dtheta_dt= [dtheta1_dt; dtheta2_dt];
end