Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
Solving ODEs and PDEs in MATLAB
Sören Boettcher
16.02.2009
Introduction
Solving ODEs
and PDEs in
Quick introduction to MATLAB syntax MATLAB
Sören
ODE in the form of Initial Value Problems (IVP) Boettcher
what equations can MATLAB handle
how to code into MATLAB
how to choose the right MATLAB solver
how to get the solver to do what you want
how to see the result(s)
several examples
Boundary Value Problems (BVP)
Delay Differential Equations (DDE)
Partial Differential Equations (PDE)
NOT todays topic: numerical methods, ODE, BVP, DDE, PDE
or MATLAB
Problem
Solving ODEs
and PDEs in
MATLAB
DEs are functions of one or several variables that relate the Sören
values of the function itself and of its derivatives of various Boettcher
orders
An ODE is a DE in which the unknown function is a function of
a single independent variable
y 0 = f (t, y ) (1)
In many cases, a solution exists, but the ODE may not
necessarily be directly solvable. Instead, the solution may be
numerically approximated using computers
There are many numerical methods in use to solve (??), but one
has to use the right solver in order to obtain good solutions
The MATLAB ODE Solvers
Solving ODEs
Explicit methods for nonstiff problems: and PDEs in
MATLAB
ode45 - Runge-Kutta pair of Dormand-Prince Sören
Boettcher
ode23 - Runge-Kutta pair of Bogacki-Shampine
ode113 - Adams predictor-corrector pairs of orders 1 to 13
ode15i - BDF
Implicit methods for stiff problems:
ode23s - Runge-Kutta pair of Rosenbrock
ode23t - Trapezoidal rule
ode23tb - TR-BDF2
ode15s - NDF of orders 1 to 5
All these methods have built-in local error estimate to control the
step size; codes are found in the /toolbox/matlab/funfun folder
Basic usage for MATLAB’s solvers
Solving ODEs
and PDEs in
MATLAB
Sören
Apply a solver: Boettcher
[t,y] = solver(@odefun, time interval, y0, options)
odefun - a function handle that evaluates the right side of the
differential equations.
time interval - a vector specifying the interval of integration.
[t0,tf] - initial and final value
[t0,t1,. . . ,tn] - evaluation of the method at certain points
y0 - a vector of initial conditions.
options - structure of optional parameters that change the
default integration properties.
Approach
Solving ODEs
Consider the IVP: and PDEs in
MATLAB
y 00 + y 0 = 0, y (0) = 2, y 0 (0) = 0 Sören
Boettcher
Rewrite the problem as a system of first-order ODEs:
y10 = y2
y20 = −y1
Code the system of first-order ODEs:
function dy dt = odefun(t,y)
dy dt = [y(2); -y(1)];
Apply a solver to the problem:
[t,y] = ode45(@odefun, [0,20], [2,0]);
The algorithm selects a certain partition of the time interval and
returns the value at each point of the partition.
Solution of harmonic oscillation
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
View the solver output:
plot(t, y(:,1),’r’,t,y(:,2),’b’)
title(’Solution of van der Pol Equation);
xlabel(’time t’); ylabel(’solution y’);
legend(’y 1’,’y 2’)
Options
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
Several options are available for MATLAB solvers.
The odeset function lets you adjust the integration parameters
of the following ODE solvers.
Save options in opts
opts=odeset(’name1’,’value1’,’name2’,’value2’,. . . )
Expand opts
opts=odeset(old opts,’name’,’value’)
If no options are specified, the default values are used.
The ODESET Options
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
name meaning default value
RelTol relative error tolerance 10−3
AbsTol absolute error tolerance 10−6
Refine output refinement factor 1 (4)
MaxStep upper bound on step size
Stats display computational cost statistics off
The estimated error in each integration step satiesfies
ek ≤ max{RelTol · yk , AbsTol}
whereas yk the approximation of y (xk ) at step k
Example of efficiency differences
Solving ODEs
and PDEs in
MATLAB
Van der Pol oscillator as a system of first-order ODEs: Sören
Boettcher
y10 = y2
y20 = µ(1 − y12 y2 − y1 )
as a function with µ = 1000:
function dy dt = vdp(t,y,mu)
dy dt = [y(2); mu*(1-y(1).^2).*y(2)-y(1)];
Apply a solver (takes 123 seconds)
[t,y]=ode23(@(t,y)vdp(t,y,1000),[0,3000],[2,0]);
Different solver (takes 56 milliseconds)
[t,y]=ode15s(@(t,y)vdp(t,y,1000),[0,3000],[2,0]);
Example of efficiency differences
Solving ODEs
and PDEs in
MATLAB
Sören
Although the above function is stiff for large µ, ode23 has almost Boettcher
achieved the same result as ode15s
Example for false solver
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
Simple ODE:
y 0 = sin(1000t), y (0) = 1.2
Analytic solution:
− cos(1000t) + 1201
y (t) =
1000
2 different solvers, one for stiff ODEs:
[t,y]=ode23(@(t,y)sin(1000*t),[0,3],1.2);
[t,y]=ode23s(@(t,y)sin(1000*t),[0,3],1.2);
Example for false solver
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
ode23 is totally wrong, ode23s makes it well
Solving BVPs with MATLAB
Solving ODEs
BVPs can have multiple solutions and one purpose of the initial and PDEs in
MATLAB
guess is to indicate which solution you want. The 2nd order DE
Sören
Boettcher
y 00 + |y | = 0
has exactly two solutions that satisfy the boundary conditions
y (0) = 0, y (4) = −2
DE for boundary value
function dy dx = bvpex(x,y)
dy dx = [y(2); -abs(y(1))];
Evaluate residual of boundary condition
function res = bc(ya,yb)
res = [ ya(1); yb(1) + 2];
Apply a solver:
solinit = bvpinit(linspace(0,4,5),[-1 0]);
sol = bvp4c(@bvpex,@bc,solinit);
Solving DDEs with MATLAB
Solving ODEs
and PDEs in
MATLAB
A DDE is a DE in which the derivative of the unknown function
Sören
at a certain time is given in terms of the values of the function Boettcher
at previous times.
Consider the problem
2y (t − 2)
y 0 (t) = −y (t), t ∈ [0, 100], y (t) = 0.5 for t < 0
1 + y (t − 2)9.65
Code the function:
function dy dt = ddes(t,y,z)
dy dt = 2*z/(1+z^9.65)-y;
Apply a solver:
sol=dde23(@ddes,2,0.5,[0,100])
plot(sol.x,sol.y)
Numerical Solution of PDEs with MATLAB
Solving ODEs
and PDEs in
MATLAB
Sören
A PDE is a DE in which the unknown function is a function of Boettcher
multiple independent variables and their partial derivatives.
solver nonlinear system
1D pdepe X X
2D pdenonlin X ×
(elliptic)
parabolic × ×
hyperbolic × ×
3D × × ×
Specifying an IVBP
Solving ODEs
and PDEs in
MATLAB
pdepe solves PDEs of the form Sören
Boettcher
µ(x, t, u, ux )ut = x −m (x m f (x, t, u, ux ))x + s(x, t, u, ux )
x ∈ (a, b), a > 0, t ∈ [t0 , tf ], m = 0, 1, 2, µ ≥ 0
The problem has an initial condition of the form
u(x, t0 ) = Φ(x), x ∈ [a, b]
The boundary conditions are
p(a, t, u(a, t)) + q(a, t)f (a, t, u(a, t), ux (a, t)) = 0, t ≥ t0
p(b, t, u(b, t)) + q(b, t)f (b, t, u(b, t), ux (b, t)) = 0, t ≥ t0
Example
Solving ODEs
and PDEs in
MATLAB
Consider the PDE
Sören
Boettcher
π 2 ut = uxx , x ∈ (0, 1), t ∈ (0, 2]
with boundary conditions
u(0, t) = 0, ux (1, t) = −π exp(−t)
and initial conditions
u(x, 0) = sin(πx)
The exact solution for this problem is
u(x, t) = exp(−t) sin(πx)
Example
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
The specification of the problem for solution by pdepe is
m = 0, a = 0, b = 1, t0 = 0, tf = 2,
µ(x, t, u, ux ) = π 2 , f (x, t, u, ux ) = ux , s(x, t, u, ux ) = 0,
p(a, t, u(a, t)) = u(a, t), q(a, t) = 0,
p(b, t, u(b, t)) = π exp(−t), q(b, t) = 1,
Φ(x) = sin(πx)
Solving an IBVP
Solving ODEs
The syntax of the MATLAB PDE solver is and PDEs in
MATLAB
sol=pdepe(m,pdefun,icfun,bcfun,xmesh,tspan) Sören
Boettcher
pdefun is a function handle that computes µ, f and s
[mu,f,s]=pdefun(x,t,u,ux)
icfun is a function handle that computes Φ
phi=icfun(x)
bcfun is a function handle that computes the BC
[pa,qa,pb,qb]=bcfun(a,ua,b,ub,t)
xmesh is a vector of points in [a, b] where the solution is
approximated
tspan is a vector of time values where the solution is
approximated
sol is a 3D array where sol(i,j,1) is the solution at
tspan(i) and xmesh(j)
References
Solving ODEs
and PDEs in
MATLAB
Books
Sören
Boettcher
Coombes et al.; Differential Equations with MATLAB
Cooper; Introduction to PDEs with MATLAB
Fansett; Applied Numerical Analysis using MATLAB
Moler; Numerical Computing with MATLAB
Shampine et al.; Solving ODEs with MATLAB
Stanoyevitch; Introduction to ODEs and PDEs using
MATLAB
Papers
Shampine, Reichelt; The MATLAB ODE Suite
Shampine et al.; Solving BVPs for ODEs in MATLAB with
bvp4c
MATLAB Help
Exercises
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
y 0 + 2y = sin(t) + exp(−5t), y (0) = 0
y 0 + y cot(t) = 5 exp(cos(t)), y (0) = 0
y 00 − 4y 0 + 5y = exp(−2t) tan(t), y (0) = 0, y 0 (0) = 0
2y 00 + y = 2 tan(t), y (0) = 0, y 0 (0) = 1
Solving ODEs
and PDEs in
MATLAB
Sören
Boettcher
Thank you for your attention.