pendulum animation
%Calling a function pendulum
function [dtheta_dt] = pendulum_ode(t, theta,g,l,m)
w = g/(l*m);
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -w*w*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
% main program
clear all
close all
clc
% inputs
g = 9.81;
l = 1.5;
m = 0.5;
%Initial conditions
theta0 = [0;6.5];
t_span = linspace(0,1,500);
%Solving the ODE
[t, results] = ode45(@(t,theta) pendulum_ode(t, theta,g,l,m), t_span, theta0);
angular_disp = results(:,1);
%Animation
ct = 1;
for i = 1:length(t_span);
THETA = angular_disp(i);
%Coordinates for pendulum
x0 = 0;
y0 = 0;
x1 = 1.5*sin(THETA);
y1 = -1.5*cos(THETA);
%Plotting for animation
plot([-0.5 0.5],[0 0],[-0.5 -0.5],[0 0.08],[-0.5 0.5],[0.08 0.08],[0.5 0.5],[0.08 0], 'color','b','linewidth',3);
hold on
plot([x0 x1],[y0 y1], 'color', 'k', 'linewidth', 5)
plot(x1,y1,'-o','markers',23,'markerfacecolor','b','color','y')
hold off
axis([-2 2 -3 0.5])
P(ct) = getframe(gcf);
ct = ct + 1;
end
%Movie
movie(P);
videofile = VideoWriter('iSIMPLE_PENDULUM.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile, P);
close(videofile);