Motion of the Periodic Swing of a Pendulum using Matlab Program
AIM - To Simulate the motion of the Periodic Swing of a Pendulum and to get an animation of motion.
OBJECTIVE -
1. To creat a programme for simple harmonic motion of a pendulum.
2. To creat a programme for motion of the periodic swing of a pendulum.
THEORY -
Simple Harmonic Motion
simple harmonic motion is a special type of periodic motion where the restoring force on the moving object is directly propotional to the object's displacement magnitude and acts towards the object's equilibrium position.
Simple harmonic motion can serve as a mathematical model for a variety of motions, but is typified by the oscillation of a mass on a spring when it is subject to the linear elastic restoring force given by Hooke's law. The motion is sinusoidal in time and demonstrates a single resonant frequency. Other phenomena can be modeled by simple harmonic motion, including the motion of a simple pendulum, although for it to be an accurate model, the net force on the object at the end of the pendulum must be proportional to the displacement
Simple Pendulum
A pendulum is a body suspended from a fixed support so that it swings freely back and forth under the influence of gravity. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging it back and forth.
Mathematical Equation -
Programming Part -
Inputs are
b = Damping Coefficient
l = Length of string (m)
g = Acceleration due to gravity (m/s^2)
m = mass of bob (kg)
Function Used:
Ode45
The new ode45 introduced in the late 1990s is based on an algorithm of Dormand and Prince. It uses six stages, employs the FSAL strategy, provides fourth and fifth order formulas, has local extrapolation and a companion interpolant.
The new ode45 is so effective that it is the only function in the suite where the default value of the refine argument is set to 4. This means that the step size the algorithm naturally wants to choose is so large that the output is more widely spaced than most people prefer. So the interpolant is called up to produce more finely spaced output at no additional cost measured in function evaluations.
Ode45 is a standard inbuild function in Matlab which is used to differentiate the given function by default, the matlab is going to do 1ode by default ode_fun was used because calculating the pendulum moment in an second order differential equation, so we have used another function manually for loop is used to make the animation of pendulum.
movie
The movie function plays the movie defined by a matrix whose columns are movie frames (usually produced by getframe).
VideoWriter
Use a VideoWriter object to create a video file from an array or MATLAB movie. The object contains information about the video and the properties that control the output video. You can create a VideoWriterobject using the VideoWriter function, specify its properties, and then write the video using object functions.
%**************Motion of the Periodic Swing of a Pendulum***********
clear all % Delete all Variables
close all % Close all plots
clc % Clear command window
% Inputs
b = 0.05; % Damping Coefficient
l = 1; % Length of the pendulum(m)
g = 9.81; % Acceleration due to gravity (m/s^2)
m = 1; % Mass of the ball(kg)
% Initial Condition that theta_0 is an array,[0;3] refers to
% [Displacement;Velocity]
theta_0 = [0;3];
% t_span runs on linspace it splits 0-20sec into 500 intervals
t_span = linspace(0,20,500);
% To solve ODE
[t, results] = ode45(@(t,theta)ode_func(t,theta,g,l,m),t_span,theta_0);
figure(1)
hold on
plot (t,results(:,1));
plot (t,results(:,2));
xlabel('Time');
ylabel('Displacement&Velocity');
legend('Angular Displacement','Angular Velocity')
hold off
grid on;
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(2)
plot([-1 1],[0 0],'linewidth',5,'color','b');
hold on
plot([x0 x1],[y0 y1],'linewidth',2,'color','r');
hold on
plot(x1,y1,'o','markers',20,'markerfacecolor','y');
hold off
grid on
axis([-1.5 1.5 -1.5 0.5]);
pause(0.0002);
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile = VideoWriter('Pendulum_animation.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
The following function code has been called in the above Matlab Program:
%*******************Ode_func********************
function [dtheta_dt] = ode_func(t,theta,b,g,l,m)
theta1 = theta(1)
theta2 = theta(2)
dtheta_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt = [dtheta_dt;dtheta2_dt]
end
Results:
Simple Harmonic Motion
Motion of the periodic swing of a simple pendulum
Youtube link for Animation
https://youtu.be/8UMEIIw63ug