Studying the motion of pendulum using MATLAB
In Engineering, ODE is used to describe the transient behavior of a system.
This ODE represents the equation of motion of a simple pendulum with damping
In this project where we study the motion of pendulum using the matlab code, the initial conditions being angular displacement=0,angular velocity=3 rad/sec at time t=0. We will study the motion for t=0 to t=20 sec.
The code and the animation are posted below.
The data for the pendulum motion which we have assumed is
length of pendulum string = 1m
mass of bob = 1kg
g= 9.81m/s^2
c= 0.05
The differential equations which wh use in odefunc function to solve the equations are:
dtheta1_dt=theta2
dtheta2_dt=-(c/m)*theta2-(g/l)*sin(theta1)
Following is the code.
clear all
close all
clc
%solving ode
c=0.05;
g=9.81;
l=1;
m=1;
%initial condition
theta_0=[0;3];
%time points
t_span=linspace(0,20,500);
%solve ode
[t,results]=ode45(@(t,theta)odefunc(t,theta,c,g,l,m),t_span,theta_0)
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
legend('Angular displacement','Angular Velocity')
xlabel('time')
ylabel('plot')
axis([0 20 -7 7])
figure(2)
plot(results(:,1),results(:,2),'color','g')
xlabel('Angular displacement')
ylabel('Angular Velocity')
ct=1;
for i=1:length(results(:,1))
x0=0;
y0=0;
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
figure(3)
plot([-1,1],[0,0],'linewidth',4,'color','k');
hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','r')
hold on
plot(x1,y1,'-o','markers',20,'markerfacecolor','k')
hold off
grid on
axis([-1 1 -2 0.5])
pause(0.001)
M(ct)= getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('damped pendulum.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
%function to solve differential equation
% creating function odefunc
function[dtheta_dt]=odefunc(t,theta,c,g,l,m)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt=theta2;
dtheta2_dt=-(c/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
Following plots are plotted using the data obtained.
Some instants of motion of pendulum
Below is the youtube link of the animation