SIMULATION OF SIMPLE PENDULUM
AIM:
To write a code that solves second order ODE which represents the equation of motion of a simple pendulum with damping.
To simulate themotion of the pendulum for 0-20 seconds for angular displacement=0, angular velocity=3 rad/sec^2 at time=0.
To create an animation video of the motion of the simple pendulum.
GIVEN INFORMATION:
given equation is:
`(d^2 theta)/dt^2`+(b/m)*`(d theta)/dt`+(g/l)*sin(`theta`)=0
where
g=9.81m/s^2 (gravity)
m=1kg (mass of the ball)
l=1m (length of the pendulum)
b=0.05 (damping coefficient)
CONVERSION OF SECOND ORDER ODE INTO FIRST ORDER ODE:
A second order ODE cannot be solved directly, so we have to convert this ODE into two first order ODE\'s. If it is first order simple ODE we can solve it using ananymous function.
Splitting second order ODE:
theta=theta1
theta1=dtheta/dt
dtheta1/dt=theta2
dtheta2/dt=-(b/m)*theta2-(g/l)*sin(theta1)
CODING:
clear all
close all
clc
%given information
l=1; %length of the pendulum in meters
m=1; %mass of the ball in kg
b=0.05; %damping coefficient
g=9.81; %gravity in m/s^2
%intial condition
theta_0=[0 3];
%time points
t_span=linspace(0,20,500); %plotting for 500 values between 0-20seconds
%solve ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,l,m,g),t_span,theta_0);
%plotting of angular displacement and velocity
figure(3)
subplot(1,2,1)
plot(t,results(:,1),\'color\',\'r\')
xlabel(\'time\')
ylabel(\'angular displacement\')
title(\'Variation of angular displacement with respect to time \')
subplot(1,2,2)
plot(t,results(:,2),\'color\',\'b\')
xlabel(\'time\')
ylabel(\'angular velocity\')
title(\'Variation of angular velocity with respect to time \')
figure(2)
plot(t,results(:,1),\'color\',\'r\')
hold on
plot(t,results(:,2),\'color\',\'b\')
xlabel(\'time\')
ylabel(\'angular displacement and angular velocity\')
legend(\'angular displacement\',\'angular velocity\')
title(\'Variation of angular diplacement over angular velocity with respect to time \')
% Pendulum Animation
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(1)
plot([-1,1],[0,0],\'linewidth\',3,\'color\',\'m\');
hold on
plot([x0 x1], [y0 y1],\'linewidth\',3,\'color\',\'b\');
hold off
hold on
plot(x1,y1,\'o\',\'markersize\',30,\'markerfacecolor\',\'r\',\'markeredgecolor\',\'b\');
hold off
title(\'Simulation of a Damped Pendulum Motion\');
grid on
axis([-1. 1.5 -1.5 .5]);
text=sprintf(\'pendulu%d.png\',ct);
saveas(1,text);
ct = ct + 1;
end
FUNCTION CODE:
function [dtheta_dt]= ode_func(t,theta)
l=1; %in terms of meters
m=1; %in terms of kg
b=0.05;
g=9.8;
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
PROCEDURE:
STEP1:
- Give intial conditions and time_span values.
- Then define the ode solver using \'ode45\' function code.
STEP2:
- Plotting the results, to compare and see the variation for angular displacement and angualr velocity with respect to time.
- Can give the plots in one window or you can use seperate windows.
STEP3:
- Animation is generated for the simple pendulum by creating a GIF with the obtained plots.
- Different procedures are there for \'Matlab\' and \'Octave\'.Do use the right procedure so that you don\'t waste ypur time.
STEP4:
- Upload it in the youtube for the further reference.
RESULTS:
Results are shown in the plots and in the animation.
Youtube link:
https://youtu.be/cyA6P1Rrzw8