To demonstrate the motion of a pendulum using Matlab
OBJECTIVE- To develop a code and demonstrate the motion of a simple pendulum using Matlab
Given below is the equation of motion of a simple pendulum with damping in the form of Ordinary Differential Equation (ODE) :
`(d theta^2)/dt +(b/m)(d theta)/dt +(g/L)sin theta = 0`
where g= gravity in m/s^2
L= length of the pendulum in m,
m= mass of the ball in kg,
b= damping coefficient.
For this case we are using the following values:
L= 1 metre,
m= 1 kg,
b=0.05,
g=9.81 m/s2.
We will simulate the motion between 0 to 20 seconds.
Boundary condition- Angular displacement=0, angular velocity = 3 rad/sec^2 at time t=0.
The Matlab code for solving the differential equation is as below:
function[ydot]=ode(y,t);
l=1;
m=1;
b=0.05;
g=9.81;
ydot=zeros(2,1);
ydot(1)=y(2);
ydot(2)=((-b/m)*y(2))-((g/l)*sind(y(1)));
end
The code for animation and plotting the graph is as below:
clear all
close all
clc
l=1;
m=1;
b=0.05;
g=9.81;
t=[0:0.1:20];
y0=[0 3];
[Results]=lsode(\'ode\',y0,t);
theta=Results(:,1);
ct=1;
for i=1:length(theta)
x1=l*sind(theta(i,1));
y1=l*cosd(theta(i,1));
figure(1)
plot([-0.5,0.5],[4,4],\'color\',\'k\',\'LineWidth\',6);
hold on
plot([0,x1],[4,y1],\'color\',\'g\');
hold on
plot(x1,y1,\'c\',\'MarkerEdgeColor\',\'r\',\'MarkerFaceColor\',\'r\',\'MarkerSize\',20)
hold off
axis([-2,2,0,5])
pause(0.03)
Text=sprintf(\"pendulum%d.png\",ct);
saveas(1,Text)
ct=ct+1;
end
figure(2)
subplot(2,1,1);
plot(t,Results(:,1),\'color\',\'r\');
hold on;
grid on;
xlabel(\'Time\');
ylabel(\'Angular displacement\');
subplot(2,1,2);
plot(t,Results(:,2),\'color\',\'b\');
grid on;
xlabel(\'Time\');
ylabel(\'Angular velocity\');
An animation video showing the motion of pendulum is given below:
The graph of angular displacement vs time and angular velocity vs time is shown below:
To show the effect of damping prominently on graph, let us consider a time of 0 to 200 seconds: