Transient Behavior of Second order ODE Simple Pendulum using MATLAB
Transient Behavior of Second order ODE Simple Pendulum using MATLAB
Aim - To write a program to simulate transient behaviour of a Simple Pendulum and to create an animation of its motion using MATLAB.
Objective
- To write a program to solve the Second order ODE for pendulum motion.
- To plot a graph showing damping of angular displacement and angular velocity w.r.t. Time.
- To create an animation file of the pendulum motion.
Introduction to Simple Pendulum
Simple Pendulum is defined as small object of mass \'m\' in kg which is suspended from a pivot so that it can swing freely back and forth due to gravity, when pendulum is displaced sideways from its initial position , it is subjected to a Restoring force due to gravity which will bring it back to rest position.
Governing Equation
Second order differential equatiom for Simple Pendulum
where,
b = damping coefficient in (kg/s)
m = mass of the hanged body in (kg)
g = acceleration due to gravity in (m/s^2)
l = length of pendulum rod in (m)
Inputs
To write a program in Matlab that will simulate the pendulum motion using
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0, angular velocity=3 rad/sec at time t=0.
Program
Main Program
%Program of 2nd ODE for Pendulum
clear all
close all
clc
%Inputs
%damping coefficient
b = 0.05;
%acceleraction due to gravity in m/s^2
g = 9.81;
%length of pendulum in m
l = 1;
%mass of ball in kg
m = 1;
%Initial conditions
theta_0 = [0;3];
%Time points
t_span = linspace (0,20,100);
%Solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%Plotting
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
xlabel(\'Time\')
ylabel(\'Displacement and Velocity\')
title(\'Angular Displacement & Velocity wrt Time\')
legend(\'Angular displacement\',\'Angular velocity\')
grid on
hold off
%Animation
figure(2)
ct = 1;
for i = 1:length(t)
x1 = 0;
y1 = 0;
x2 = sin(results(i,1));
y2 = -cos(results(i,1));
%Plotting
plot([-1 1],[0 0],\'linewidth\',3,\'color\',\'b\')
hold on
plot([x1 x2],[y1 y2],\'linewidth\',3,\'color\',\'b\')
plot(x2,y2,\'marker\',\'O\',\'markersize\',7,\'markerfacecolor\',\'r\')
hold off
axis([-2 2 -1.5 1])
pause(0.01)
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter(\'simple_pendulum_motion_2_ode.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Sub Program
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 explanation
-Inputs are given as per the instructions to solve the project.
-theta_θ is the matrix created for second order ODE in which first value is for angular displacement and second for angular velocity
-t_span is the time array for how long integration is to be done as per the requirement 0-20 sec are the start and end limit and linspace is used to equally space 200 values between 0-20 sec
-ode45 is a solver in MATLAB having main inputs as \'t\' time & \'theta\' which are dependent & independent variables of ODE systems
-ode_func is the function created having input parameters as (t,theta,b,g,l,m)
-plotting the damping of Angular displacement and Angular Velocity w.r.t Time
-hold on is used to plot both graph on the same graph
-grid on is basically to show grids in graph.
-counter of value one is given
-\'i\' is array of 100 values of equally spaced time values
-initial and final valyes are given
-plot is made to show the pendulum movement
-axis limits are defined
-then rest of coding is used to create a movie, saving it in array called \'M\' ,make a file to save the video, hence animation is created.
Errors
Error caused due to wrong sin and cos values assinged to x2 & y2.
Error solved by assinging correct trignometric function to x2 & y2. Multiple frames also solved and given color code to plot.
Result
Damping of Angular Displacement & Angular Velocity w.r.t. Time.
To watch the animation click on the given link below.
Conclusion
-Simulation of Transient behavior of a Simple Pendulum motion by solving second order ODE.
-Animation for Simple Pendulum is Simulated.