simulation of pendulum using Matlab
1. Pendulum Explanation-
Figure 1.1 Forces on Pendulum
1st Step-
According to the newton’s second law of motion-
F tangential = m*a tangential
F tangential = - (m*g*sin (θ))
damping force = (b/m*L)*`(d theta)/dt`
a tangential =R*alpha
alpha = `(d^2theta)/dt^2`
So mass on string of length L, tangential acceleration is a tangential = L* ((d^2theta)/dt^2) (θ(theta) is in radians)’
In case of pendulum with damping the damping force will also play its role, hence equation becomes –
- (m*g*sin (θ)) – b*`(d theta)/dt` = m*L*(d^2theta)/dt^2
((d^2theta)/dt^2) + (b/m*L)*`(d theta)/dt`+ (g/L)*sin (θ) = 0 (ODE to be solved)
k=(b/m*L) ; (g/L) = r
2nd Step-
Convert second order ODE to first order ODE –
X(1) = θ
X(2) =((d theta)/dt)
`(d(X(1)))/dt`= ((d theta)/dt) = X(2) (1)
`(d(x(2)))/dt`` (d^2theta)/dt^2 = -k*X(2) - r*sin (θ) (2)
Above two equations are solved in Matlab.
Matlab Code –
%system parameters
m=1;L=1;b=0.05;g=9.81;
k=(b/(m*L));
r=g/L;
t = [0 20];
x0 = [0 3];
%ODE using anonymous function
[t, x] = ode45(@(t,x)[x(2);-k*x(2)-r*sin(x(1))],t,x0);
plot(t,x(:,1));
grid on;
hold on;
plot(t,x(:,2),\'r\');
xlabel(\'Time(s)\');
ylabel(\'Amplitude\');
legend({\'Angle(rad)\',\'Angular_speed(rad/sec)\'});
Results –
%system parameters
m=1;L=1;b=0.05;g=9.81;
k=(b/(m*L));
r=g/L;
t = [0 20];
x0 = [0 3];
%ODE using anonymous function
[t, x] = ode45(@(t,x)[x(2);-k*x(2)-r*sin(x(1))],t,x0);
plot(t,x(:,1));
grid on;
hold on;
plot(t,x(:,2),\'r\');
xlabel(\'Time(s)\');
ylabel(\'Amplitude\');
legend({\'Angle(rad)\',\'Angular_speed(rad/sec)\'});
%system parameters
m=1;L=1;b=0.05;g=9.81;
k=(b/(m*L));
r=g/L;
t = [0 20];
x0 = [0 3];
%ODE using anonymous function
[t, x] = ode45(@(t,x)[x(2);-k*x(2)-r*sin(x(1))],t,x0);
%animation
%origin
O=[0,0];
axis(gca,\'equal\'); %aspect raito of the plot
axis([-1.5 1.5 -1.5 0.4]);
grid on;
% animation loop
ct=1;
for i = 1:length(t)
%point where mass is attached
p=L*[sin(x(i,1)) -cos(x(i,1))];
%origin_circle
O_circ = viscircles(O,0.01);
%pendulum
pend = line([O(1) p(1)],[O(2) p(2)],\'linewidth\',3,\'color\',\'black\');
%ball
ball = viscircles(p,0.05);
%time interval to update plot
pause(0.001);
M(ct)= getframe(gcf);
ct=ct+1;
%delete previous objects if it is not the final loop
if i<length(t)
delete(pend);
delete(ball);
delete(O_circ);
end
%F=getframe(gcf);
end
movie(M)
videofile = VideoWriter(\'animation_pendulum.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Youtube Animation link-
https://www.youtube.com/watch?v=HVWrv6jJl3s