Week 3 - Solving second order ODEs
To solve a second order ODE (ordinary differential equation ) of a simple Harmonic Pendulum
In order to solve this we have to learn the basics of ODE and the equations to solve the same.
In mathematics, a differential equation is an equation that relates one or more unknown functions and their derivatives. In applications, the functions generally represent physical quantities, the derivatives represent their rates of change, and the differential equation defines a relationship between the two. Such relations are common; therefore, differential equations play a prominent role in many disciplines .
The pendulum is a simple mechanical system that follows a differential equation. A simple pendulum consists of a ball of mass (M) hanging from a massless string of length (L) and fixed at a pivot point (P) when displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. By appling Newton's second law for rotational system, the equation of motion for pendulum may be obtained.
When the pendulum is displaced by an angle θ and released, the force of gravity pulls it back towards its resting position. Its momentum causes it to overshoot and come to an angle −θ (if there are no frictional forces), and so on. The restoring force along the motion of the pendulum due to gravity is −mgsinθ. Thus, according to Newton's second law, the mass times the acceleration must equal −mgsinθ.
`(d^2 θ)/〖dt〗^2 +b/m dθ/dt+g/L sinθ=0`
`(d^2 θ)/d^t =-b/m θ_2-g/L sinθ`
- L=1m (length of the pendulum)
- m=1Kg (mass of the ball)
- b=0.05 (damping coefficient)
- g=9.81 m/s^2(gravitional force)
- angular velocity=3rad/sec at time t=0
From this we can create a plot of angular velocity and displacement of a simple harmonic pendulum.
For solving this we have to create two programs, one is the program having function codes and the other is the main program which call the function and execute the codes.
1) A program which is derived as function by using second order ordinary differential equations of a simple harmonic pendulum.
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
- By applying Newton's second law for rotational system in second order ODEs the above coding is created and which will be used for solving any second order ODEs of a simple harmonic pendulum.
2) The main program of a simple harmonic pendulum by using second order differential equations.
% Second order ODE for pendulum TEST
clear all
close all
clc
% Inputs
b=0.05;
g=9.81;
l=1;
m=0.1;
% Initial condition
theta_0=[0;5];
% time points
t_span=linspace(0,20,50);
% solve ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
% To defined pivot point of pendulum and plot it
x0=0;
y0=0;
plot(x0,y0,'Marker','o');
y=results(:,1);
% plotting
figure(1);
hold on
plot(t,results(:,1));
plot(t,results(:,2));
xlabel('Time');
ylabel('Plot');
title("Simple Harmonic motion of a pendulum");
% Animation of pendulum
ct=1;
for i=1:length(t_span)
x1=l*sind(y(i));
y1=-l*cosd(y(i));
figure(2)
% plotting
plot([x0 x1], [y0 y1],'linewidth',3,'Marker','o');
hold on
plot(x1,y1,'Marker','o','color','r','Markersize',30,'Markerfacecolor','g');
hold off
title("Simple Harmonic motion of a Pendulum");
axis([-1 1 -2 0]);
pause(0.03);
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M);
videofile=VideoWriter('oscillation_of_pendulum.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
- 'ode45' is a inbuilt command in Matalab used for solving ordinary differential equations.
- 'marker' command is used for creating the ball on one end of the pendulum string.
- marker is for giving color to the surface of the ball.
- t_span will give the time interval of the oscillation.
from the above program we will get plot and an animation file which will give the angular displacement and angular velocity of the simple harmonic pendulum.
The above plot shows the angular velocity and angular displacement of a simple harmonic pendulum.

This is the final plot of the simple harmonic pendulum before animation.
Below is the youtube link for the animation of a simple harmonic motion of a pendulum.