Simulation of transient motion of Simple Pendulum using Matlab
Objective :
To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of it\'s motion using Matlab.
Introduction:
In Engineering, ODE(Ordinary Differential Equation) is used to describe the transient behavior of a system. A simple example is a pendulum.
The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the \"ball\" w.r.t time.
The differential equation used to show the motion of a damped simple pendulum is stated as below:
Where,
L=1 metre, length of the pendulum in m
m =1kg, mass of the ball in kg,
b= 0.05 , damping coefficient
g=9.81 m/s2. , gravity
In order to solve second order ODE first we have to convert it into first order ODE as follows:
Where,
θ2 = velocity function and θ1 = position function.
% simulation of transient behaviour of a pendulam
close all
clear all
clc
% inputs
b = 0.05;
m = 1;
l = 1;
g = 9.81;
% initial conditions
theta0 = [0;3];
% time span
t_span = linspace(0,20,300);
%solving ode
[t,results] = ode45(@(t,theta)ode_func(theta,b,g,l,m),t_span,theta0);
z = results(:,1);
% loop formation
ct = 1;
for i = 1:length(z);
j = z(i);
x1 = 0;
y1 = 0;
x2 = l*sin(j);
y2 = -l*cos(j);
% plotting
plot ([x1 x2],[y1 y2],x2,y2,\'r.\',\'MarkerSize\',18);
axis([-2 2 -2 2]);
pause(0.01);
M(ct) = getframe(gcf);
ct = ct+1;
end
% creating movie of our animation
movie(M)
videofile = VideoWriter(\'pendulam_animation.avi\',\'Uncompressed AVI\')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation of Script:
% Inputs
Inputs are given to all values of second order ODE.
% Initial condition
Initial conditions are specified for angular displacement and angular velocity such as 0 and 3 rad/sec
% Time span
Time span is specified for 20 sec of duration for motion of simple pendulum. Linspace command is used to split the interval of 0-20 sec. into 300 values.
% Solving ODE
ode45 is a inbuilt function which takes the input and gives output in the form of position and velocity at any given point w.r.t time.
Now here in this case a separate funtion is used to give input to the ode solver as shown :
function [d_theta_dt] = ode_func(theta,b,g,l,m);
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
d_theta_dt = [dtheta1_dt;dtheta2_dt];
end
The function command is saved as a separate script with the same name as ode_func. This is very important that the \'Function\' command should be save with a same name as that of Function or else it may not work.
Values for x1,y1,x2,y2 is been specified and lenght of string is considered to be 1 m.
% Plotting
The values of x1,x1,y1,y2 are plotted and with help of \'Marker\' command the ball of pendulum is created of size 18 units and a colour red.
Pause of 0.01 second is given for each value of plot.
\'M(ct)\' command is used to save each and every plot in array.
axis is specified as -2 and 2 for both axis x and y.
% Animation
Command \'movie(m)\' is used to create movie of each plot stored in array M.
Videofile is created with the help of command \'VideoWriter\' and video is stored as a avi .
Script used to create a plot of displacement and velocity w.r.t time :
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'plot\')
xlabel(\'time\')
legend(\'displacement\',\'velocity\')
Plot :
Animation of motion.