Simple Pendulum Kinematics - MATLAB
Problem Statement:
Write program in Matlab to simulate simple pendulum motion.
Objectives:
1. Plot the responses of dispacement and velocity against time
2. Create animation for movement of pendulum
MATLAB scripting for the function:
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
MATLAB scripting for plots and animation:
clear all
close all
clc
% Inputs
b = 0.05; % damping coefficient
m = 0.1; % mass of pendulum in Kg
l = 1; % length of pendulum in m
g = 9.81; % acceleration due to gravity in m/s²
% Initial Conditions
theta_0 = [0;3]; % Angular dispacement = 0; Angular Velocity = 3 m/s
% Time Span
t_span = linspace(0,20,500);
% Solving the ODEs
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
dispacement = results(:,1);
% Plotting Results
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel(\'Time\')
ylabel(\'Plot\')
% Creating Animation
% Initial conditions
x0 = 0;
y0 = 0;
ct = 1;
for i = 1:length(dispacement)
THETA1 = dispacement(i);
x1 = l * sin(THETA1);
y1 = -l * cos(THETA1);
figure(2)
plot([-.5,.5],[0,0],\'--\',\'linewidth\',3,\'color\',\'b\')
hold on
plot([x0,x1],[y0,y1],\'linewidth\',2,\'color\',\'r\')
hold on
plot(x1,y1,\'o\',\'MarkerSize\',20,\'MarkerFaceColor\',[1 0 1])
hold off
grid on
axis([-1.5 1.5 -1.5 0.5]);
pause(0.003)
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter(\'Simple_Pedulum_Kinematics.avi\',\'Uncompressed AVI\')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Stepwise Explanation:
- Second order differential equation for Simple Pendulum Kinematics is as follows; d²/dt²(Θ) + (d/dt(Θ))(b/m) + (g/l)sinΘ = 0 _______(A) let, Θ = Θ1; differentiating, d/dt (Θ)= d/dt(Θ1) = Θ2______________ (i) differentiating again, d²/dt²(Θ) = d²/dt² (Θ1)= (d/dt)(dΘ1/dt) = d/dt(Θ2) by putting above values eq. A becomes; d/dt(Θ2) = -(b/m)(Θ2)-(g/l)(sinΘ1)__________(ii) This way we hav converted 2nd order differentail equation into 2 single order differential equations. Solving these two equations, i & ii we can get values of Θ1 & Θ2. These Θ1 & Θ2 are nothing but dispacement and velocity resp. If eq. i & ii represented in matrix form;
- Actual scripting starts with stating the inputs. Inputs are as belows; i. b = damping coefficient = 0.05 ii. m = mass of pendulum = 0.1 Kg iii. l = length of pendulum = 1 m iv. g = acceleration due to gravity = 9.81 m/s²
- As stated in problem, initial conditions are; time span = 0 to 20 sec. , angular dispacement = 0 , angular velocity = 3 m/s at time = 0 sec. In matlab these input values can be stated as variable[displacement;velocity]. Let this varible be theta_0. Therefore, theta_0 = [0;3]. To facilitate the calculation of displacement and velocity in between 0 to 20 sec., 500 intervals are considered in between. This will help in animating simple pendulum motion. In matlab scripting this time intervals are given by,t_span = linspace(0,20,500).
- For calculating dispacement & velocity at each time interval, a function is needed to specify considering eq. i & ii. A function dtheta_dt is specified which depends on time(t),theta,b,g,l & m.
- By calling this function in main scripting, we can get values of t & results. t is nothing but time values of 1x500 matrix & results is matrix containing values of Θ1 & Θ2, which are nothing but our final results.
- Above ODE solver gives us values of dispacement and velocity. First column of result will be values of dispacement and second column will be the values of velocities. After getting values of dispacement and velocity, next step is plot the results i.e. response of velocity and dispacement with time.
- Second objective is to create animation of motion of pendulum. It is done using movie() command.
- After collecting all frames, metlab stitches all frames using movie() to create animation.
Results:
Time Plots:-
Plot shows that dispacement starts from 0 & angular velocity starts with 3 m/s. Due to consideration of damping factor both the factors attains near zero values over the time span of 20 sec.
Animation of motion of simple pendulum:
Above animation shows, motion of simple pndulum dampens with time.
Error Occured:
1. Missing of VideoWrite() function in line 45.
Issue resolved by adding VideoWriter function so as to define file name and format.
2. Not specifying l1 function left it unrecognised. This typing error is corrected by correcting l1 as l.
3. Not using hold off command, holds all the position of pendulum thereby making undertanding animation difficult. Adding hilding off command at line 45 cures this issue.