Week 3 - Solving second order ODEs
AIM - To solve the second order ODE of motion of a simple pandulum with damping.
There is a word 'ODE' which is mentioned in the question. Let us understand it with a bit of detail.
Ordinary Differential Equation (ODE) :
An ordinary differential equation (ODE) is an equation which consists of one or more functions of one independent variable along with their derivatives. A differential equation is an equation that contains a function with one or more derivatives.
Here, in the question the second order equation is of the pandulum. So lets also get the bit of the info regarding pandulum as well.
Simple Pandulum :
A simple pendulum is a mechanical arrangement that demonstrates periodic motion. The simple pendulum comprises a small bob of mass ‘m’ suspended by a thin string secured to a platform at its upper end of length L.
The simple pendulum is a mechanical system that sways or moves in an oscillatory motion. This motion occurs in a vertical plane and is mainly driven by the gravitational force. Interestingly, the bob that is suspended at the end of a thread very light somewhat we can say it is even massless. The period of a simple pendulum can be made extended by increasing the length string while taking the measurements from the point of suspension to the middle of the bob. However, it should be noted that if the mass of the bob is changed it will the period remains unchanged. Period is influenced mainly by the position of the pendulum in relation to Earth as the strength of gravitational field is not uniform everywhere.

Above image is the representation of the motion of simple pandulum.
Now, as we have got the idea regarding what is the simple pandulum lets proceed further towards the coding.
The equation provided in the question is

In the equation given above,
L = Length of the pandulum (m),
m = mass of the ball (kg),
b = Damping Co-efficient,
g = Gravity (m/s2).
Value for above parameters given in the question which are
L = 1 m
m = 1 kg
b = 0.05
g = 9.81 m/s2
also there are three more values given in the question
Time Period - 0-20 s
Angular Displacement - 0
Angular Velocity - 3 rad/s at time t = 0.
For the program we do need to create function
Coding for the creation of Function :
%ODE 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
Explaination of function :
Here with the use of function the second order ODE is converted to the first order ODE by introducing theta1 & theta2 for the ease of coding.
Then the differentiation of theta1 & theta2 is done & we will get dtheta_dt which is a column matrix.
With the use of this function we are going to solve the second order ode equation.
Coding to solve the second order ODE of motion of Simple Pandulum & to get the animation for the same :
clear all
close all
clc
%Given parameters
%Damping Co-efficient (b), Acceleration due to gravity (g in m/s2), Mass (m in kg), Length (L in m),
L = 1;
m = 1;
b = 0.05;
g = 9.81;
%Initial Condition of Pendulum
theta_0 = [0;3];
%Consideration of the given time duration
t_span = linspace(0,20,200);
%calling the function to solve the ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,L,m), t_span, theta_0);
%plotting the results to get the time vs velocity & displacement graph
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Velocity')
legend('Displacement','Angular velocity')
grid on
hold off
Displacement = results(:,1)
velocity = results(:,2)
%Animation of the pandulum
ct = 1;
for i = 1:length(results(:,1))
x0 = 0;
y0 = 0;
x1 = L*sin(results(i,1)+pi);
y1 = L*cos(results(i,1)+pi);
figure(2)
plot([x0 x1],[y0 y1],'-o','MarkerSize',15,'MarkerFaceColor','b','linewidth',3)
axis([-1 2 -2 1])
hold on
plot([-2 2],[1 1],'linewidth',4,'color','[0 0.5 0]')
M(ct) = getframe(gcf)
hold off
ct = ct + 1;
end
%Creating Movie
movie(M)
video_file = VideoWriter('Simple_Pandulum.avi','Uncompressed AVI')
open(video_file)
writeVideo(video_file,M)
close(video_file)
Explaination of Program :
- In the above Program first of all we have dropped the provided values of Length,Damping Co-Efficient,mass & Gravity.
- At first Angle & Time are at the initial condition.
- In the question value of time is given 0-20 s for which we have taken the output as t_span with the help of linspace command & the total 200 values are taken in between.
- Then the value of ODE is taken with the help of ode45 command.
- Then we have plotted the graph for the values of velocity-Dispacement & Time in the Y & X axis respectively.
- Then with the help of loop command Mass at the different points & at various time marked & plotted in the graph.
- Then Movie command is used to get the animation of the simple pandulum.
- The movie is saved as Simple_Pandulum.
Plot of Velocity vs Time :

Now lets proceed further regarding the provision of the animation link of simple pandulum.
Attached the youtube link of the motion of simple Pandulum.
YouTube Link for Animation of Simple Pandulum :
Error Faced During Programming :
During programming I have faced one error regarding missing @ symbol in ode command.
Screen shot is attached for the reference.

Error printed in command window as Invalid Expression.
Error Solution :
To overcome the error have added the @ symbol at the appropriate place in ode command.
& then the program ran completely fine.