Ordinary Differential Equations (ODE) in MATLAB
Ordinary Differential Equations (ODE) inMATLAB
Shan He
School for Computational ScienceUniversity 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 MATLABODE Solvers in MATLABSolving linear ODEs in MATLABSolving high order ODEs in MATLABSolving 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 involvinga 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 .
I Let y ′ = dydx be the first derivative with respect to x .
I Let y (n) = dnydxn 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 ′, . . . , y (n)) = 0
Ordinary Differential Equations (ODE) in MATLAB
Concepts about ODE
Linear ODE and Homogeneous Linear ODEI A ODE is said to be linear if F can be written as a linear
combination of the derivatives of y together with a constantterm, all possibly depending on x:
an(x)y n + an−1(x)y n−1 + · · ·+ a1(x)y ′ + a0y = r(x)
or more concisely,
y n =n−1∑i=0
ai (x)y (i) + r(x)
where ai (x) and r(x) are continuous functions in x and thefunction 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 solvedexactly: 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 numericalsolution of ODEs:
I ode45: based on an explicit Runge-Kutta (4, 5) formula andthe Dormand-Prince method.
I ode23: based on an explicit Runge-Kutta (2, 3) formula andthe Bogacki and Shampine method.
I We choose according to order of accuracy and the type ofsystems (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:{u′ = du(x)
dx = u(x) + v(x)
v ′ = dv(x)dx = u(x)
Analytical solution can be obtained, but how to solve it inMATLAB 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 ODEs1 function dy = simple_ode(t,y)2 % function to be integrated3 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 ′, . . . , 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 asystem 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 ′ = −ye(−t/5) + y ′e(−t/5) + 1
y ′′ = −2 sin(t)
I Step 1: Introduce a new variable that equals the firstderivative of the free variable in the second order equation:z = y ′
I Step 2: Taking the derivative of each side yields the following:z ′ = y ′′
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 ′:x ′ = −ye(−t/5) + ze(−t/5) + 1
z ′ = −2 sin(t)
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 theODE:
1 function dy = high_order_ode_example(t,x)2 % x(1) = x3 % x(2) = y4 % x(3) = z5 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 time2 tf = 20; % Stop time3 x0 = [1 -1 3] % Initial conditions4 [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 thenumerical 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 thenumerical method must take small steps to obtain satisfactoryresults.”
I Example:
y ′ = y 2 − y 3
y(0) = τ
0 ≤ t ≤ 2
τ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 thecoefficients 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 integrated3 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 ODEfunction:
1 tspan = [0 10];2 initvalue = [1 0];3 option = [];4 for ii=-10:45 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 hitthe ground?
I Equations:
y ′′ = 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 through4 % zero and stop integration.5 value = y(1); % Detect height = 06 isterminal = 1; % Stop the integration7 direction = -1; % Desired directionality of8 % 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 tfinal6 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 forODEs.
I To solve Boundary Value Problems: bvp4c.Tutorial.
I To solve Delay ODEs: dde23.Tutorial.
I To solve Stochastic ODEs: MATLAB SDE Toolbox.Tutorial.