SOLVING 2ND ORDER ODE (SIMPLE PENDULUM)
Aim:- Matlab program to simulate the pendulum motion (2nd order ode)
Theory:-
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 back and forth.
The mathematics of pendulums are in general quite complicated. Simplifying assumptions can be made, which in the case of a simple pendulum allow the equations of motion to be solved analytically for small-angle oscillations.
The ordinary differential equation (ODE) which represents the motion of a simple pendulum with damping is
`(d^2theta)/dt^2 + b/m*(d theta)/dt + (g/L)*sintheta = 0`
where, g = Acceleration due to gravity,
L = Length of the pendulum,
`theta` = Angular displacement,
m = Mass of the ball,
b = Damping Coefficient
Solving the ODE:-
`(d^2theta)/dt^2 + (b/m)*(d theta)/dt + (g/L)*sintheta= 0 ->` main equation
let's consider `theta = theta_1`
then, `d theta = d theta_1 = theta_2 ->` equation 1
`(d^2 theta)/dt^2 = d/dt ((d theta)/dt) = d/dt ((d theta_1)/dt) = d/dt ((d theta_2)/dt) ->` equation 2
substituting equation 1 and equation 2 in main equation.
`(d theta_2)/(d t) + (b/m)* theta_2 + (g/L)*sin theta_1 = 0`
`(d theta_2)/(d t) = - (b/m)* theta_2 - (g/L)*sin theta_1`
Now, these equations are used to find out the displacement and velocity of the simple pendulum using MATLAB.
MATLAB FUNCTION:
%%
function [dtheta_dt] = ode_fun(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
The same equations which were derived in the SOLVING THE ODE section are used to write this matlab function
In this function, time, theta, b, g, L, m are given as Inputs and Displacement and Velocity are Output values.
MAIN PROGRAM:
%% Program to simulate the TRANSIENT behaviour of a simple pendulum
close all
clc
%INPUTS
b = 0.8; %Damping_Co-efficient.
g = 9.81; %Gravity.
L = 1; %Length of the Pendulum rod.
m = 1; %Mass of the Pendulum bob.
%INITIAL CONDITIONS
theta_0 = [0;3]; % Initial Displacement and Velocity.
% TIME POINTS
t_span = linspace(0,10,500); % Integration time
% Solving ODE
[t,results] = ode45(@(t,theta) ode_fun(t,theta,b,g,L,m),t_span,theta_0);
%PLOTTING
figure(1),clf
plot(t,results(:,1),'b','linewidth',2)
hold on
plot(t,results(:,2),'r','linewidth',2)
xlabel('Time')
ylabel('Plot')
legend('Displacement','Velocity')
title('Displacement and Velocity variations of a simple pendulum')
%%
%{
RESULTS
As the damping coefficient increases, the vibrations in the simple pendulum decreases,i.e.,
the Displacement comes to zero in less time.
this variation can be seen in Plotting by varying the damping coefficient value
%}
%% PENDULUM ANIMATION
%Input values of Pendulum.
x0 = 0;
y0 = 0;
ct = 1;
for i = 1:length(t)
x1 = L*sin(results(i,1)); % X-axis co-ordinates of bob.
y1 = -(L*cos(results(i,1))); % Y-axis co-ordinates of bob.
% Animation Plot
figure(2),clf
hold on
plot([-L-0.5 L+0.5],[0,0],'linewidth',2,'color','k')
plot([0 0],[-5,5],'k--','linewidth',1)
plot([x0,x1],[y0,y1],'k','linewidth',2)
plot(x1,y1,'o','markersize',30,'markerfacecolor','m','markeredgecolor','b')
axis([-L-1,L+1,-L-2,0.5])
axis off
title('ANIMATION OF SIMPLE PENDULUM')
grid on
M(ct) = getframe(gcf);
ct = ct+1;
end
% CREATING A MOVIE
movie(M)
v = VideoWriter('SIMPLE_PENDULUM.avi','UNCOMPRESSED AVI');
open(v)
writeVideo(v,M)
close(v)
%%
CODE EXPLANATION:
- At first, the input values are provided (g, L, b, m).
- The initial conditions are to be determined.
- As we have (2) 1st order ODE'S -> theta_0(1) = Displacement and theta_0(2) = Velocity.
- These are provided as initial conditions to the system
- Then, we are integrating the ODE system to obtain the solution, so we define a value for time.
- Calling the ODE function, As we subdivided the 2nd order ODE into 2 first order ODE'S, we create a function name 'odefun' and calling the function for solving the ODE.
- Then, the results are obtained.
- For visual representation of the results, plotting is done
- Variation of displacement and velocity can be seen in the graphs and proper color bar and legends to provided to differentiate them.
- By using Movie command, animation is created.
PLOTS:-
Fig 1: Displacement and Velocity Variations of Simple pendulum with respect to Time.
Fig 2: Snapshot from the simple pendulum animation.
RESULTS :-
As the damping coeffficient increases, the vibrations in the simple pendulum decreases, i.e., the displacement comes to zero in less time.
This variation can be seen in plotting by varying the damping coefficient value.
ANIMATION VIDEO LINK:
https://www.youtube.com/watch?v=vi_J88RCi1I&feature=youtu.be