SOLVING ODEs USING THE ode45 SOLVER
Differential equations are composed of an unknown function and its derivatives. The
equation is called an ordinary differential equation (ODE) when the unknown function
involves only one independent variable and a partial differential equation (PDE)
when more than one independent variable is involved.
There are two main categories of ODEs,
i) Initial value problem (problems where all the conditions (usually values of
the function and possibly its derivatives) are given at a single point,
typically the starting point.)
ii) Boundary value problem (problems where the value of an unknown
function, or its derivative, is constrained at two different points are known
as boundary-value problems.)
Initial value problem Boundary value problem
𝑑𝑇 𝑑2 𝑦
= −2 (𝑇 − 25), 𝑇(0) = 35 𝑥 2 . 2 − 6𝑦 = 0, 𝑦(1) = 1, 𝑦(2) = 1
𝑑𝑡 𝑑𝑥
In this discussion, we will focus mainly on initial value problems
Solving first-order ODEs using the ode45 solver
𝑑𝑇
We will use the example shown above that is = −2 (𝑇 − 25), 𝑇(0) = 35
𝑑𝑡
In this equation, T is the dependent variable, and t is the independent variable
Now, in order to use the ode45 solver, you need to prepare 3 main parameters which
include,
The ode function is a function we create that will return the derivative of the
dependent variable(s).
The tspan is a row vector that can contain a minimum of 2 elements, in which case it
will be the starting value for the independent variable, and the second value will be
the stopping value of the independent variable. If you do this, MATLAB will
automatically determine the interval h. The other option would be to define the full
set of t values or independent variable values that the solver will use to estimate the
different values of the dependent variable at different values of the independent
variables.
The Y0 ( initial value of the dependent variable).
Note that you can name these parameters different names depending on what you
are working on, so long as you hold the purpose in mind.
For our example, the script will look like;
From this, it is evident that, at t = 10, T = 25 oC
Example 2
Solve the following differential equation and determine the value of y at x = 3
𝑑𝑦 𝑥3 + 𝑦3
= , 𝑥 = 1 𝑤ℎ𝑒𝑛 𝑦 = 4
𝑑𝑥 𝑥. 𝑦 3
Solving two first-order equations simultaneously
We still use the ode45 solver which means we will have to prepare the 3 mandatory
parameters. The difference is, mainly in the ode function and the Y0.
The odefunction is going to return a column vector of the different derivatives of the
dependent variables with respect to a single independent variable.
Also since we no longer have a single dependent variable, we will have to specify Y0
as the column vector for the initial values of the different dependent variables.
tspan remains the same.
Example 1
Solve the following pair of differential equations simultaneously and determine the
value of x and y at t = 5.
𝑑𝑦 𝑑𝑥
+ 𝑥 = 1, − 𝑦 + 4𝑒 𝑡 𝑎𝑡 𝑡 = 0, 𝑥 = 0 𝑎𝑛𝑑 𝑦 = 0
𝑑𝑡 𝑑𝑡
Making the derivatives the subject gives
𝑑𝑦 𝑑𝑥
= 1 − 𝑥, = 𝑦 − 4𝑒 𝑡
𝑑𝑡 𝑑𝑡
Note: always make the derivatives the subject before you do anything else.
Example 2
Solve the following system of differential equations and determine the values of x, y
and z at t = 4;
𝑑𝑥 𝑑𝑦 𝑑𝑧
= −2𝑥 + 𝑦 + 𝑧, = 𝑥 − 3𝑦 + sin(𝑡) , = 𝑥𝑦 − 𝑧 + 𝑒 −𝑡
𝑑𝑡 𝑑𝑡 𝑑𝑡
𝑥(0) = 1, 𝑦(0) = 0, 𝑧(0) = 2
Solution
Solving second-order differential equations using the ode45 solver
The trick here is to decompose a second-order differential equation into 2 first-order
differential equations. Note that the same trick applies to high-order differential
equations. For example, an nth ordered differential equation can be broken down
into nth first-order differential equations
Example
Given the differential equation below, determine the value of y at t = 5
𝑑 2 𝑦 24𝑑𝑦 𝑑𝑦
9 − + 16𝑦 = 0, 𝑎𝑡 𝑡 = 0, 𝑦 = =3
𝑑𝑡 2 𝑑𝑡 𝑑𝑡
As usual, we have to make the highest ordered derivative the subject, in which case
we get
𝑑2 𝑦 24 𝑑𝑦 16
2
=( ) − 𝑦
𝑑𝑡 9 𝑑𝑡 9
𝑑𝑦
Now we are going to let 𝑦 = 𝑥 and =𝑧
𝑑𝑡
𝑑𝑥
The first differential equation will be, = 𝑧………….𝑖
𝑑𝑡
𝑑2𝑦 𝑑 𝑑𝑦 𝑑 𝑑𝑧
It should be noted that
𝑑𝑡 2
= ( ) = 𝑑𝑡 (𝑧) = 𝑑𝑡
𝑑𝑡 𝑑𝑡
𝑑𝑧 24 16
the second differential equation will be, = ( )𝑧 − 𝑥 … … … . . 𝑖𝑖
𝑑𝑡 9 9
the new boundary conditions will be at t = 0, x = 3, and z = 3