Week 3 - Solving second order ODEs
AIM : To solve second order differential equation of a simpe pendulum using matlab.
OBJECTIVE :
- To write a program, solve the ODE of motion of the simple pendulum using matlab.
- Plot the graph of the displacement and velocity.
- Create a simulation of the pendulum using movie command. an
THEORY:
A second order differntial equation is an equation involving the unknown function y. Its derivatives y' and y" and the variables x. Which is written as (d^2y/dx^2)+(dy/dx)+y = 0.
This Linear equation helps us in understanding different engineering phenomenon and principles. Here we shall consider the motion of a simple pendulum through a second order differential equation.
A simple pendulum consist of a bob of mass 'm' suspended by spring with negligible mass of length l. The forces that act on the pendulum are gravitational force 'g' and tension of the string.

FORMULAE:
The second order differential equation is defined as,
(d^2(theta)/dt^2) + b/m*(d(theta)/dt) + g/l*(sin(theta)) = 0
where, b = damping coefficient
m = mass of the bob in Kg.
g = gravity in m/s^2.
l = length of the string in m.
Breaking down the second order differential equations into 2 first order differential equations,
let us consider theta =theta1.
upon differntiating , d(theta)/dt =d(theta1)/dt
Here we shall define, d(theta1)/dt= theta2
By differentiating theta twice we get, d^2(theta)/dt^2=d^2(theta1)/dt^2= d/dt(d(theta1)/dt)=d/dt(theta2)
Substituting it in the above second ode we get,
d(theta1)/dt= theta2
d(theta2)/dt = -(b/m)*(theta2)-(g/l)*(sin(theta1))
From this deviation we have defined theta1 and theta2.
PROGRAM :
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
%initial condition [displacement, velocity]
theta_0=[0;3];
%time points
t_span = linspace(0,20,200);
%solve ode
[t,results]= ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
title('damped harmonic motion of pendulum')
legend('angular displacement','angular velocity')
ylabel('plot')
xlabel('time in sec')
ct=1;
for i= 1:length(results)
x_start=0;
y_start=0;
x_end= l*sin(results(i,1));
y_end= -l*cos(results(i,1));
figure(2)
plot ([-1 1],[0 0],[x_start x_end],[y_start y_end])
hold off
line(x_end,y_end,'marker','O','markersize',20,'markerFacecolor','r');
axis([-1.5 1.5 -2 0.5])
title('Simple Pendulum');
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('simple_pendulum.avi', 'uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
EXPLANATION :
For the first program, the function is defined based on the deviation to split the second order differential equation into 2 first order differential equations. Once theta1 and theta2 defined, they are stored as a solution vector where, d(theta)_dt =[d(theta1)_dt : d(theta2)_dt] is a column matrix.
In the main program, the initial conditions of the angular displacement and velocity must be provided to solve the ODE.
The ODE integrated in order to obtain the solution. The t_span is defined using linear array.
ode45 is an inbuilt solver in matlab used to integrate differential equations.To solve the differential equation we use an anonymous functions.
RESULTS:

The graph of the angular displacement and velocity over time has been obtained.
The animation video of the simple pendulum given in below link:
https://drive.google.com/file/d/19I-4mfussxZGJ0gCS4PZCr1Tst-bbzqv/view?usp=sharing