Week 3 - Solving second order ODEs
AIM:
To write a report on solving the equation of a simple pendulum with damping and simulating the pendulum motion using matlab.
OBJECTIVES:
1) To write a program in matlab which can solve the second order equation.
2) To write a program that can animate the motion of a simple pendulum.
THEORY:
A simple pendulum is a device in which the point mass is suspended from a fixed support and is attached to a light inextensible thread. The mean position of a simple pendulum is the vertical line passing through the fixed support.

The ODE which we will be using is shown below:
This is a second order differential equation which represents the motion of a simple pendulum with damping.
To solve this equation in matlab the second order differentials have been converted to first order by taking theta as theta(1) and dtheta/dt as theta(2). By substituting these in the 2nd order ODE we get the following:
dtheta1/dt = theta2
dtheta2/dt = -(b/m)*theta2-(g/l)*sind(theta1)
These are combined into an array so that we can solve it using matlab.
PROGRAM:
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% initial conditions
theta_0 = [ 0; 5];
% time points
t_span = linspace(0, 20, 500);
% solve ode
[t, results] = ode45(@(t, theta) ode_func(t, theta, b, g, l, m), t_span, theta_0);
% plot
ct = 1;
for i = 1:length(results(:, 1))
x0 = 0;
y0 = 0;
x1 = -l*sind(results(i, 1));
y1 = -1*cosd(results(i, 1));
plot([-0.5 0.5], [0, 0], 'linewidth', 3)
line([x0 x1], [y0 y1], 'linewidth', 3)
hold on
plot(x1, y1, 'o', 'color', 'r')
grid on
axis([-1.5 1.5 -1.5 0.5]);
pause(0.01)
M(ct) = getframe(gcf);
hold off
ct = ct+1;
end
movie(M);
Videofile = VideoWriter("ode_main.m.avi", "Uncompressed AVI");
open(Videofile);
writevideo(Videofile, M);
close(Videofile);
FUNCTION CALL:
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)*sind(theta1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
CODE EXPLANATION:
1) The required inputs have been given.
2) The initial conditions have been taken at 0 displacement which is maximum velocity in a matrix.
3) The time span has been taken for 20 secs with 500 intervals.
4) The differential equation is solved by using ode45 which is an inbuilt matlab command.
5) The results will be obtained in two columns. One for displacement and other for velocity.
6) For loop is used for calculating the motion of simple pendulum.
7) Get frame is used to take all the frames which is assigned to a variable m.
8) Movie is created by storing the video file of all these frames.
ERRORS OCCURED:


These errors have been occuring due to the hold on command which should be hold off because we dont want the existing plots.
RESULT:
The link for the required animation plot is given below:
CONCLUSION:
A second order ode has been solved using matlab and the necessary animation has been obtained.