pemdulum animation
A pendulum of mass m and length L is iniatially at theta=0 rad and is given the angular velocity 3 rad/sec. and for given situation damping coefficient is given as 0.05 . This motion is simulated using the matlab code in between the time gap of 0-20sec.
The ODE represents the equation of motion
`(d^2theta)/dt^2 +(b/m)*((d theta)/dt)+(g/L)*sin(theta)`
here is the matlab code for the simulated motion:-
%solving a second order ODE of pendulum with damping
clear all
close all
clc
%inputs
b=0.05;
m=1;
l=1;
g=9.81;
%t_span
t=linspace(0,20,500);
theta0=[0 3];
[t,theta_R]= ode45(@(t,theta) odefun_pendulum_amimation(t,theta,b,g,l,m),t,theta0);
%plotting
figure(1)
subplot(2,1,1)
plot(t,theta_R(:,1));
hold on
plot(t,theta_R(:,2));
xlabel('time');
ylabel('ammplitude');
legend('angle(rad)','angulur velocity(rad/s)');
subplot(2,1,2)
plot(theta_R(:,1),theta_R(:,2))
xlabel('position(rad)')
ylabel('angular velocity')
%%Animation Of Pendulum Motion
figure(2)
o=[0 0];
axis(gca,'equal')
%limits of the plot
axis([-1.5 1.5 -1.5 1]);
grid on;
ct=1;
for i=1:length(t)
%mass point
P= l*[sin(theta_R(i,1)) -cos(theta_R(i,1))];
%circle of origin
O_circ=viscircles(o,0.01);
%pendulum
pend=line([o(1) P(1)],[o(2) P(2)]);
%ball
ball=viscircles(P,0.05);
%time interval
pause(0.001);
M(ct)=getframe(gcf); %capture the current figure using getrame command
ct=ct+1; %loop counter
delete(O_circ)
delete(pend)
delete(ball)
end
movie(M) %stiching all the frames in the M array to create an animation
%creating an uncompressed file called forward_kinematics.avi and assigning
%it to variable video file
videofile=videowriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writevideo(videofile.M)
closevideo
In this matlab code a function is also used to solve the ode of the pendulum motion.
ODE function code is written below
%function to solve second order ode for pendulum motion
function [dtheta_dt]= odefun_pendulum_amimation(t,theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);
dtheta_dt= zeros(2,1);
%rate of change of angle = angular velocity
dtheta_dt(1)= theta2;
%acceleration
dtheta_dt(2)=-((b/m).*(theta2))-((g/l).*(sin(theta1)));
end
The curve the were obtained by simulating the motion are shown below
Animation video of pendulum motion:-