MATLAB Programming/Order Differential Equations

Order of Differential Equations


A differential equation is an equation with a function and one or more of its derivatives. It is usually to describe changes.

Note: Derivatives =   .

An order of differential equation (ODE) is an equation that has highest derivative of the dependent variable involved some ordinary derivatives of a function with respect to the independent variable. Often, the goal is to solve an ODE, i.e., determine what function or functions satisfy the equation. That means the differential equation defines the relationship between variables and their derivatives.

There are a primarily two of ODE types

  1. 1st Order Differential Equation (1st ODE)
  2. 2nd Order Differential Equation (2nd ODE)

First Order Differential Equation


All the linear equations in the form of derivatives are in the first order. It has only the first derivative such as dy/dx, where x and y are the two variables and is represented as


Second-Order Differential Equation


When the order of the highest derivative present is 2, then it is a second order differential equation.


Using dsolve solve differential equation


We have an differential equations as follow,


In order to solve this differential equation in MATLAB , we uses dsolve command to get the answer as followed


s =
(5*x)/4 - exp(-t)*(15^(1/2)*C1 - (5*x)/4 + 7) + exp(-t/2)*cos((15^(1/2)*t)/2)*(15^(1/2)*C1 - (5*x)/2 + 8) - C1*exp(-t/2)*sin((15^(1/2)*t)/2)

Type of ODE solver in MATLAB


There are few types of ODE solver but we will focus on just the commonly used :

ODE Accuracy Use case suggestion
ode45 Medium For most use case, ode45 should be enough to solve the ODE
ode23 Low ode23 can be more efficient than ode45 at problems with crude tolerances
ode113 Low to High ode113 can be more efficient than ode45 at problems with stringent error tolerances, or when the ODE function is computationally intensive.

Examples: ODE45 to solve differential equations


There are $10,000 deposited into the bank fixed deposit accounts and the interest rate is 2% annually.

To start with, we need to do a equation to model the money.

Equation 1:    : Annual saving interest is 2% annually on the balance of the bank balance over time ,

Equation 2:   : Amount of money initial is at $10,000

From Equation 1, we take out (Note: Need to consult on other Wikiprojects on how to derive the linear equations)




  , where  

  , taken from equation 2


Using dsolve functions , we can similarly convert the differential equations

>> syms M(t) p
>> eqn = diff(M,t) == 0.02*M;
>> S= dsolve(eqn,M(0)==10000)
S =

Hence. we have equations (shown as S) to do our modelling.

In MALAB to calculate differential equations using ode45 , the syntax is as followed

[t,y] = ode45(odefun,tspan,y0)


t = Evaluation points, returned as a column vector

y = Solutions, returned as an array

odefun = Function of the ode. You may use anonymous functions instead of writing a new files for the functions

tspan = Interval of integration, specified as a vector. At minimum, tspan must be a two element vector [t0 tf] specifying the initial and final times

y0 = Initial conditions, specified as a vector. y0 must be the same length as the vector output of ode function,

Using dsolve for converted differential equations ode45 functions to solve ODE

function [T,M] = money_in_bank(R)

% Enter the initial values for the amount of money in the bank
M0 = 1000;

% Enter the interest rate
R = 2;

% Enter the time period
T0 = 1;
Tf = 30;

% using dsolve previously

% Solve the differential equation
[T, M] = ode45(@(T,M)S, [T0, Tf], M0);

% Plot the solution

% Add a legend and labels
ylabel('Money in Bank')