Week 3 - Solving second order ODEs
AIM:-
To solve a second order ODE'S of a simple pendulum using MATLAB
OBJECTIVE:-
- To write program to solve the ODE Of the motion of a simple pendulum using MATLAB.
- To plot the graph of the displacement and velocity.
- To create a simulation of the pendulum in motion
THEORY:-
A second order ODE is an equation involving the unknown function y,its derivatives y',y'' and the variable x,which is written as,

FORMULA:-
The second order differnetial equation is defined as,

where,
b= damping coeeficient
g= gravity m/s^2
m= mass of the bob in kg
l= length of the string in m
PROGRAM:-
THE MAIN PROGRAM FOR SIMPLE PENDULUM:
%SOLVING SECOND ORDER ODE
close all
clear all
clc
%inputs
b=0.05 %damping coefficient
g=9.81 %gravity(m/s^2)
l=1 %length of the spring
m=1 %mass 9(kg)
%initial condition
%angular displacement is 0 and angular velocity is 4
theta_0=[0;4]
%time_points
t_span=linspace(0,20,250)
%solving ODE
[t,result]=ode45(@(t,theta)ode_function(b,g,l,m,t,theta),t_span,theta_0)
%plotting
hold on
plot(t,result (:,1),'color','r')
plot(t,result (:,2),'color','b')
xlabel('time')
ylabel('velocity')
grid on
% for loop
ct=1
for i=1:length(t)
x0=0
y0=0
x1=1*sin(result(i,1))
y1=-1*cos(result(i,1))
plot([-2,2],[0,0],'linewidth',3,'color','g')
line([x0,x1],[y0,y1],'linewidth',4)
axis([-2 2 -2 2])
hold on
plot(x1,y1,'o','markers',20,'markerfacecolor','y')
hold off
grid on
pause(0.03)
M(ct)=getframe(gcf)
ct=ct+1
end
% animation of the pendulum
movie (M)
videofile=VideoWriter('second_ode.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
PROGRAM FOR FUNCTION:
function [dtheta_dt]=ode_function(b,g,l,m,t,theta)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/1)*sin(theta1);
dtheta_dt= [dtheta1_dt;dtheta2_dt];
end
OUTPUT:
Plotted graph for the above program

The graph of the angular displacement and velocity over time has been obtained.due to low damping coefficient it can be observed that the oscillation are strong.
ANIMATION VIDEO LINK:-
ERROR FOR THE PROGRAM:
For the second order ODE simple pendulum using MATLAB . I did not faced any type of ERROR while writting this program.
CODE EXPLANATION:
- For the first program, the fuction is defined based on the derivation to split the second order ODE into first order ODE. once theta 1 and theta 2 are defined they are stored as solution vector where, dtheta_dt=[dtheta1_dt;dtheta2_dt]; is a coloumn matrix
- set the initial conditions for displacement and velocity of pendulum. we initialize the displacement as 0 and velocity as 4 m/s^2
- then next the time interval or time span for pendulum using linspace command for time array 0 to 20 with 250 points inbetwwen.
- the ode for pendulum is solved using the inbuilt command ode45 which is used for solving ordinary differntial in matlab.
- then mention t_span as defined before and give the initial condition theta_0 for solving ode respectively
- plot command has beed used to plot rate of change of displacement with respect to time and velocity
- for loop command has been used for creating the plots or figure of animation of pendulum motion.
- to create animation of the figures movie for different figures using getframe command gcf for current figure in a variable
- then increment the counter by 1 and ended the for loop
- finally, created the movie using movie command for the saved frame in variable respectively
RESULT:-
Thus we have analysed and understood the equation of motion with damping simple pendulum and created a program for solving ODE's using MATLAB.