0% found this document useful (0 votes)
4 views

Lecture5 Body

Uploaded by

Fernando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture5 Body

Uploaded by

Fernando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Ordinary Differential Equations (ODE) in MATLAB

Ordinary Differential Equations (ODE) in


MATLAB

Shan He

School for Computational Science


University of Birmingham

Module 06-23836: Computational Modelling with MATLAB


Ordinary Differential Equations (ODE) in MATLAB

What will we learn from the next 5 lectures

I How to solve ODEs using MATLAB.


I How to model biological systems using ODEs in MATLAB.
I How to analyse ODEs using MATLAB.
I Understand bifurcation and chaos using MATLAB.
I Applications of bifurcation and chaos to biological problems.
Ordinary Differential Equations (ODE) in MATLAB
Outline

Outline of Topics

Concepts about ODE

Solving ODE in MATLAB


ODE Solvers in MATLAB
Solving linear ODEs in MATLAB
Solving high order ODEs in MATLAB
Solving ODEs in MATLAB: Advanced topics
Ordinary Differential Equations (ODE) in MATLAB
Concepts about ODE

What is ODE

An Ordinary Differential Equation (ODE) is an equation involving


a function and its derivatives.
Ordinary Differential Equations (ODE) in MATLAB
Concepts about ODE

Definition of ODE

I Let y be an unknown function of x.


dy
I Let y 0 = dx be the first derivative with respect to x.
dny
I Let y (n) = dx n be the nth derivative with respect to x.
I Let F be a given function
I Then an ODE of order n is an equation of the form:

F (x, y , y 0 , . . . , y (n) ) = 0
Ordinary Differential Equations (ODE) in MATLAB
Concepts about ODE

Linear ODE and Homogeneous Linear ODE


I A ODE is said to be linear if F can be written as a linear
combination of the derivatives of y together with a constant
term, all possibly depending on x:

an (x)y n + an−1 (x)y n−1 + · · · + a1 (x)y 0 + a0 y = r (x)


or more concisely,
n−1
X
n
y = ai (x)y (i) + r (x)
i=0

where ai (x) and r (x) are continuous functions in x and the


function r (x) is called the source term.
I A linear ODE is said to be homogeneous if r (x) = 0,
otherwise it is called non-homogeneous or inhomogeneous.
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
ODE Solvers in MATLAB

Solution to ODE

I If an ODE is linear, it can be solved by analytical methods.


I In general, an nth-order ODE has n linearly independent
solutions.
I Any linear combination of linearly independent functions
solutions is also a solution.
I If an ODE is not linear, most of the case it cannot be solved
exactly: we will use MATLAB to obtain approximate solutions
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
ODE Solvers in MATLAB

ODE Solvers in MATLAB

I Matlab has several different ODE solvers for the numerical


solution of ODEs:
I ode45: based on an explicit Runge-Kutta (4, 5) formula and
the Dormand-Prince method.
I ode23: based on an explicit Runge-Kutta (2, 3) formula and
the Bogacki and Shampine method.
I We choose according to order of accuracy and the type of
systems (stiff or nonstiff).
I Rule of thumb: Always try ode45 first.
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
ODE Solvers in MATLAB

How to use MATLAB ODE Solvers

I The MATLAB ODE solvers can be called as a function:


[T,Y] = ode**(@odefun,tspan,y0,options)
I @odefun: Function handle of the ODE function
I tspan: Interval of integration, [t0 ,tfinal ].
I y0: Initial conditions.
I options: Optional parameters.
I The ODE function odefun define the ODEs:
function [dy] = odefun(t, y, parameters)
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving linear ODEs in MATLAB

Solving linear ODE in MATLAB

One simple example:


(
du(x)
u0 = dx = u(x) + v (x)
dv (x)
v0 = dx = u(x)

Analytical solution can be obtained, but how to solve it in


MATLAB numerically?
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving linear ODEs in MATLAB

Solving linear ODE in MATLAB

Steps for solving this equation numerically in MATLAB:


I Step 1: Create a MATLAB function that defines the ODEs
1 function dy = simple_ode(t,y)
2 % function to be integrated
3 dy = zeros(2,1);
4 dy(1) = y(1) + y(2);
5 dy(2) = y(1);
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving linear ODEs in MATLAB

Solving linear ODE in MATLAB

I Step 2: Call a numerical solver provided in MATLAB, e.g.,


[T,Y] = ode45(odefun,tspan,y0)

1 tspan = [0 10];
2 y0 = [1 0];
3 [X,Y] = ode45(@simple_ode,tspan,y0);
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB

Reduction of ODE order

I Recall an ODE of the general form:

F (x, y , y 0 , . . . , y (n) ) = 0

I We said this system is an ODE of order n.


I Any differential equation of order n can be reduced to a
system of n first-order (n = 1) differential equations.
I We do so because high order ODE (n > 1) is difficult to solve.
I MATLAB ODE solvers cannot handle higher order ODE!
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB

Reduction of ODE order

Methods:
I We will use a second order ODE as an example:
(
x 0 = −ye (−t/5) + y 0 e (−t/5) + 1
y 00 = −2 sin(t)

I Step 1: Introduce a new variable that equals the first


derivative of the free variable in the second order equation:
z = y0
I Step 2: Taking the derivative of each side yields the following:
z 0 = y 00
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB

Reduction of ODE order

Methods:
I Step 3: Substituting the second order ODE with z and z 0 :

0 (−t/5) + ze (−t/5) + 1
x = −ye

z 0 = −2 sin(t)

 0
y =z
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB

Solve high order ODE in MATLAB

Methods:
I Step 4: Write a MATLAB function ho ode.m to define the
ODE:
1 function dy = high_order_ode_example(t,x)
2 % x(1) = x
3 % x(2) = y
4 % x(3) = z
5 dy = [-x(2) * exp(-t/5) + ...
6 x(3) * exp(-t/5) + 1;
7 x(3);
8 -2*sin(t)]
9 end
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving high order ODEs in MATLAB

Solve high order ODE in MATLAB

Methods:
I Step 5: evaluate the system of equations using ODE45:
1 t0 = 5; % Start time
2 tf = 20; % Stop time
3 x0 = [1 -1 3] % Initial conditions
4 [t,s] = ode45(@ho_ode,[t0,tf],x0);
5 x = s(:,1);
6 y = s(:,2);
7 z = s(:,3);
8 plot(t,s);
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics

Stiffness of ODE equations


I Stiffness is a subtle, difficult, and important concept in the
numerical solution of ordinary differential equations.
I “A problem is stiff if the solution being sought varies slowly,
but there are nearby solutions that vary rapidly, so the
numerical method must take small steps to obtain satisfactory
results.”
I Example:

y0 = y2 − y3
y (0) = τ
2
0≤t≤
τ
I If we weren’t concerned with efficiency of ODE solver, we
wouldn’t be concerned about stiffness.
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics

Passing parameters to ODEs

I By passing the value to ODE function, we can change the


coefficients in ODEs each time we call the ODE solver.
I We need to define the parameters in the ODE function header:
1 function dy = ode_pv(t,y,A)
2 % function to be integrated
3 dy = zeros(2,1);
4 dy = A*y
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics

Passing parameters to ODEs

I Call the ODE solver and pass the parameter to the ODE
function:
1 tspan = [0 10];
2 initvalue = [1 0];
3 option = [];
4 for ii=-10:4
5 A = [1 ii; 2 -1];
6 [t,y] = ode45(@ode_pv,...
7 tspan,initvalue,option,A);
8 hold on;
9 plot(t, y);
10 end
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics

Events
I Recall syntax of the ODE solvers:
[T,Y] = ode**(@odefun,tspan,y0,options)
I We generally assume tspan is known, e.g.,

t0 ≤ t ≤ tfinal
I But sometimes it is also important to determine tfinal .
I Example: a ball is falling because of gravity, when does it hit
the ground?
I Equations:

y 00 = g = −9.8
where y (t) is the height of the object at time t
I The question: for what t does y (t) = 0? We can use events.
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics

Define events

I Define the ODE function:


1 function dydt = ode_ball(t,y)
2 dydt = [y(2); -9.8];
I Define event function for this problem:
1 function [value,isterminal,direction] ...
2 = events(t,y)
3 % Locate the time when height passes through
4 % zero and stop integration.
5 value = y(1); % Detect height = 0
6 isterminal = 1; % Stop the integration
7 direction = -1; % Desired directionality of
8 % the zero crossings:
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics

Run ODE Solver with events

I Define the event function as an option for ODE solver:


1 opts = odeset(’events’,@ball_events);
I Solve it using MATLAB ode45:
2 y0 = [20; 0];
3 [t,y,tfinal] = ode45(@ode_ball,[0 Inf]...
4 ,y0,opts);
5 tfinal
6 plot(t,y(:,1),’-’,[0 tfinal],[1 0],’o’)
Ordinary Differential Equations (ODE) in MATLAB
Solving ODE in MATLAB
Solving ODEs in MATLAB: Advanced topics

Other ODE solvers

I What we have introduced are all for Initial Value Problems for
ODEs.
I To solve Boundary Value Problems: bvp4c.
Tutorial.
I To solve Delay ODEs: dde23.
Tutorial.
I To solve Stochastic ODEs: MATLAB SDE Toolbox.
Tutorial.

You might also like