MATLAB Program for simulation of Damped Oscillation of Simple Pendulum using second order Ordinary Diffrential Equations
AIM:
To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of its motion.
OBJECTIVE:
To write a program that solves the following Ordinary Differential Equation which represents the equation of motion of a simple pendulum with damping.
`(d^2theta)/dt^2 + b/m(d(theta))/dt+g/Lsin(theta) = 0`
where,
`theta` = angular displacement in radians
b = Damping Coefficient
L = Length of Pendulum in m
m = mass of the ball in kg
g = acceleration due to gravity `m/s^2`
THEORY:
Simple Pendulum is one in which a point mass is suspended in a string or rod of negligible mass pivoted at the free end along which the pendulum oscillated to and fro about the equilibrium position due to the influence of gravity. The simple pendulum traces the Simple Harmonic Motion where the restoring is directly proportional to the displacement and acts in the direction opposite to that of displacement.
MATLAB PROGRAM:
clear
clc
%Inputs
L=1;
m=1;
b=0.05;
g=9.81;
%Initial conditions
theta_0 = [1;3];
%Time Points
t_span = linspace(0,20,500);
%Solve ODE
[t, result] = ode45(@(t,theta) ode_fun(t, theta,L,m,b,g),t_span, theta_0);
%Plotting
plot(t, result(:,1));
hold on
plot(t, result(:,2));
ylabel(\"Plot\");
xlabel(\'time\');
legend(\'Angular displacement\',\'Angular velocity\');
title(\'Damped oscillation of a simple Pendulum\');
hold off
ct = 1;
for i = 1:length(result(:,1))
x0 = 0;
y0 = 0;
x1 = L*sin(result(i,1));
y1 = -L*cos(result(i,1));
plot([-0.5 0.5], [0 0],\'-r\',\'LineWidth\',3);
hold on
plot([x0 x1], [y0 y1],\'-b\',\'LineWidth\',3);
hold on
plot(x1, y1, \'o\',\'markers\',18,\'markerface\',\'r\');
hold off
axis([-1 1.5 -2 2]);
pause(0.05);
M(ct) = getframe(gcf);
ct = ct + 1;
end
movie(M)
videofile = VideoWriter(\'Simple Pendulum.avi\',\'UnCompressed AVI\');
open(videofile);
writeVideo(videofile,M);
close(videofile);
function [dtheta_dt] = ode_fun(t, theta,L,m,b,g)
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
EXPLANATION OF THE ABOVE PROGRAMS:
- The above program is used to simulate the harmonic motion of a simple pendulum based on the Ordinary differential equation.
- The input data and initial conditions like angular displacement, angular velocity, time, time span as give are typed in the live editor.
- The graph of angular displace and angular velocity is obtained by using an inbuilt function \'ode 45\'.
- For tracing the motion of a simple pendulum a function for second-order ODE of a simple pendulum is created in a separate Matlab file under the same folder where the previous program is situated.
- Then the created function \'ode_fun\' is inserted in the main function program.
- Based on the time span and number of frames between the given time span for loop is executed with the help of counter \'ct\' command.
- Then the co-ordinates for placing the pendulum set up is typed according to our convenience following the above syntax.
ERRORS FACED:
The below error was due to the use of line command instead of plot command in line 38. The error was rectified by replacing the line command with plot command.
The below error occurred due to the missing of a negative sign in line 34 and due to the axis values that are given in line 42. The error was rectified by adding a positive sign in the line 34 and offsetting the axis values/
RESULT/OUTPUT:
The plot of Angular displacement and Angular velocity Vs time is obtained for damped oscillation of a Simple Pendulum by substituting the given values.
Also the simulation of simple pendulum was created and uploaded in youtube.
YOUTUBE LINK OF SIMULATION:
THANK YOU :)