Transient Behaviour Of A Simple Pendulum Simulation Using MATLAB
Transient behavior of a simple pendulum using MATLAB
The figure shows the moton of the pendulum, a bob is suspended by a massless string from a fixed point.
Let us assume,
m= mass of the bob
l= length of string
Equation for the damped motion of the pendulum is given below
Where,
b= damping coefficient
g= gravitational force
In the beginning,all the constant used in solving the equation are set to a variable and as an input initial velocity and initial displacement is provided. Time period is in the form of array/ matrix. Ode45 function is used to solve the differential equation. The main working code for damped motion of simple pendulum is given below
ode 45 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
Code to simulate the damped motion of pendulum
clear all
close all
clc
%input
b=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,result]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0)
To plot the result on a graph , plot function is used as below
%for ploting the graph
figure(1)
plot(t,result(:,1),\'linewidth\',1.5)
hold on
plot(t,result(:,2),\'linewidth\',1.5)
xlabel(\'time\')
ylabel(\'plot\')
hold on
Creating animation of the simple pendulum
To create an animation of the damped simple pendulum. A ‘ for’ loop is used to create a frame for each results and these frames are attached together in an uncompressed Avi using ‘movie’ function. Hold off is used to visualize the instantaneous displacement and velocity of the simple pendulum.
%%for making animation of pendulum%%
ct=1
for i=1:length(t_span)
x0=0;
y0=0;
x1=l*sind(result(i,1));
y1=-l*cosd(result(i,1));
figure(2)
plot([x0 x1],[y0 y1],\'linewidth\',3,\'color\',\'b\');
hold on
plot(x1,y1,\'marker\',\'o\',\'markersize\',20,\'markerfacecolor\',\'r\')
hold off
axis([-0.1 0.1 -1.5 0.5])
pause(0.3)
M(ct)=getframe(gcf)
ct=ct+1
end
movie(M)
videofile = VideoWriter(\'simple_pendulum.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Errors faced while programming was a case sensitive error while creating movie, as it got rectified by using uppercase letters.
MOTION OF SIMPLE PENDULUM ANIMATION