Animation of a simple pendulum using matlab .
Aim - To write a program in matlab to stimulate the pendulum motion
THEORY - A simple pendulum consist of mass(m) hanging from a string of length (L) and fixed at pivot point .When displaced to an initial angle and released ,thr pendulum will swing back and forth with periodic moyion .
This motion can be described in mathematicall form and code can be written in MATLAB to get the representation.
MATHEMATICAL EQUATION :-
g = 9.81 ,acceleration due to gravity
L =`1 , length of pendulum in (m)
m =1 , mass of the ball in(kg)
b = 0.05 , damping coefficient
PROCEDURE -
1 Firstly the second order 0DE is converted into first order ode which can be used in function .
2 call the function using the inbuilt ode45
3 input the initial data such as time,angular displacement, and angular velocity along with parameter L,g,b and m .
The result ares tored in 2 coloumn in the first coloumn angular displacement value and in 2nd coloumn angular velocity values .
MATLAB CODE -
clear all
close all
clc
% inputs
l =1;
m =1;
g = 9.81;
b=0.05;
ct = 1;
% initia; condition
theta_0 = [0,3];
t_span = linspace(0,20,500);
% 0de solving
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
hold on
plot(t,results(:,1));
plot(t,results(:,2));
xlabel ('time')
ylabel('angular velocity or angular displacement')
legend('angular displacement(radians)','angular(radian/sec)')
grid on
theta2 = results(:,1)
for i = 1:length(theta2);
theta_x = theta2(i);
x0 = 0;
y0 = 0;
x1 = l*sin(theta_x);
y1 = -l*cos(theta_x);
% plotting the pendulum
figure(2)
plot([-0.5 0.5],[0 0],'color','b','linewidth',5)
hold on
plot([x0 x1],[y0 y1],'color','r','linewidth',3)
hold on
plot(x1 ,y1 ,'o','markers',15,'markerfacecolor','g')
hold off
grid on
axis([ -1.5 1.5 -1.5 0.5]);
pause(0.002)
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
ODE_FUNCTION CODE
function [dtheta_dt] = ode_func(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
RESULTS :
PLOT -1
VARIATION OF ANGULAR DISPLACEMNT OR ANGULAR VELOCITY W.R.T TIME .

PLOT -2
ANIMATION VIDEO FOR THE SIMPLE PENDULUM (proceed the link for this )