Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
April 2018
These notes are intended as a guide to implement numerical methods solving partial differential equations
(PDEs) in two space dimensions in MATLAB. They therefore give a short introduction into PDEs, relevant
notations and the concepts behind both finite differences and spectral methods.
1
where δ(x) is the Dirac delta function, then all of u is with numerical values α and β which are often chosen
concentrated at position x = 0 and the total amount to be zero. Neumann boundary conditions on the other
of u is u0 . The solution for this system on a infinite hand determine the flux across the boundaries:
domain is a normal or Gaussian distribution: ∂ u(a) = α, ∂ u(b) = β.
x x
u0 −x2
When α and β are zero, these conditions are also re-
u(x, t) = √ e 4Dt .
2 πDt ferred to as no-flux boundary conditions. When inves-
tigating pattern formation, one often chooses periodic
This solution can now be used to determine the exact boundary conditions, where all efflux across one border
distribution of u for each time step. For a detailed re-enters the domain on the opposing side. This can
derivation of the diffusion equation see Murray (2003) be formalised as:
Volume I Chapter 11.1. To model processes such as
chemotaxis and pattern formation one adds a reaction u(a) = u(b),
term f (u) that comprises production or degradation of ∂x u(a) = ∂x u(b),
the substance u and is often chosen to be non-linear: ∂xx u(a) = ∂xx u(b),
∂u ... .
= Du ∇2 u + f (u). (2)
∂t Periodic boundary conditions are useful to simplify ge-
ometry, e.g. by modelling a circle as a line with peri-
Equations of this form are therefore also referred to as
odic boundaries, or to model repeating patterns. The
Reaction-Diffusion (RD) systems.
solvers explained in these notes will therefore employ
Types of PDEs PDEs can be classified according to
periodic boundary conditions.
the types of derivatives they contain. PDEs comprising
only second derivatives with respect to space are elip-
tic. A simple eliptic equation is the Laplace equation: Numerical approximation
2
spectral methods the discretised forms of equation (2) which determines the limit of time stepping for explicit
are temporal discretisation in section 3.2 of his Short Ad
Hoc Introduction To Spectral Methods. He also pro-
∂u ∂ ũ
= Du LH × u + f (u) and = −Du k 2 ũ + F{f (u)}, vides all Matlab skripts used throughout the text as
∂t ∂t
files on his website.
respectively. They will be derived and explained in Implicit methods To overcome this limit in time
more detail later on. For now , it is important to note, stepping implicit methods infers the currents state
that LH is a matrix and LH × u is a matrix product. from the next. The implicit Euler is therefore:
k 2 on the other hand is a matrix of wave numbers that
has the same size as ũ. The scalar entries in k 2 are ∂ut+∆t ut+∆t − ut
≈ . (4)
multiplied element-wise with the entries in ũ. ∂t ∆t
First, we will consider the simpler case of the diffusion
equation (1):
Temporal discretisation ut+∆t − ut
= Du ∇2 ut+∆t
∆t
In general, there a two approaches to obtain numerical
ut+∆t − ut = ∆tDu ∇2 ut+∆t
approximations of a system for a time step: explicit
methods infer this approximation ut from the previous ut+∆t (1 − ∆tDu ∇2 ) = ut
state ut−∆t . This can be easily done via the difference ut
ut+∆t = .
quotient but has the drawback of becoming unstable 1 − ∆tDu ∇2
when time steps are to large. Implicit methods over-
This equation is harder to solve. Rather than being
come this by solving and equation that infers ut from
simply multiplied with a scalar the Laplace operator
ut+∆t . While this is more complex it has the advantage
is now in the denominator. This becomes even more
of being unconditionally stable.
apparent, when adding the non-linear reaction term.
Explicit methods Explicit methods utilise the previ-
The equation
ous states of a system to approximate the current one.
The simplest approach is the forward Euler method: ut+∆t − ut
= Du ∇2 ut+∆t + f (ut+∆t )
∆t
∂ut ut+∆t − ut
≈ . (3)
∂t ∆t can only be solved for ut+∆t if f (u) has an inverse
f −1 (u). While implicit time stepping is uncondition-
Substituting equation (3) into equation (2) yields ally stable it is thus mainly suited for linear compo-
ut+∆t − ut nents of a PDE. Systems with non-linear parts can be
= Du ∇2 ut + f (ut ) approximated using a so called semi-implicit method
∆t
ut+∆t − ut = ∆t(Du ∇2 ut + f (ut )) that combines implicid methods for linear components
with explicit ones for non-linearities. We will get back
ut+∆t = ut + ∆tDu ∇2 ut + ∆tf (ut ). to this matter once spatial discretisation has been dis-
cussed.
This system can now be solved when the Laplace
operator is replaced by a suitable spatial discretisa-
tion. It only requires a scalar multiplication with the Finite Differences Method
value ∆t for the approximation ut+h to follow explic-
itly from ut . The forward Euler is the simplest ex- As the name suggests, the finite differences method
plicit method. Others, such as the Runge-Kutta and utilises the discretised equidistant mesh with step size
Adams-Bashforth methods were developed to increase h we introduced earlier and approximates:
accuracy. While these explicit methods remain compu-
tationally easy to handle, they become unstable when ∂ux ux+h − ux
≈ . (5)
the time steps are chosen to be too large. For diffusion ∂x h
with Du = 1 and ∆x = 0.1 these instabilities occur The error for this approximation can be estimated with
for ∆t > 0.002 and manifest as large oscillations at the the Taylor Series
edges of the domain (see also Uecker Figure 2). Uecker
describes this failure of numerical integration and the h 0 h2
f (x + h) = f (x) + f (x) + f 00 (x)....
so called Courant–Friedrichs–Lewy (CFL) condition, 1! 2!
3
For a two dimensional space this is so that ∇2 uh2 = C ∗ u. This, like any kernel convolu-
∂u ∂ 2 u h2 tion, leads to border problems - requiring values out-
u(x + h, y) = u(x, y) + h+ 2 + ... + O(h4 ) side of the boundaries - that can be overcome with
∂x ∂x 2
∂u ∂ 2 u h2 periodic boundary conditions. To this end, one con-
u(x − h, y) = u(x, y) − h+ 2 − ... + O(h4 ) structs a matrix with dimensions of u to obtain the
∂x ∂x 2
∂u ∂ 2 u h2 second derivatives in x and y direction through matrix
u(x, y + h) = u(x, y) + h+ 2 + ... + O(h4 ) multiplication:
∂y ∂y 2
∂u ∂ 2 u h2
−2 1 1
u(x, y − h) = u(x, y) − h+ 2 − ... + O(h4 ). 1 −2 1
∂y ∂y 2
The sum of these terms is 1 −2 1
S=
.. .. ..
u(x + h, y) + u(x − h, y) + u(x, y + h) + u(x, y − h) =
. . .
∂2 ∂2 2
1
4u(x, y) + + uh + O(h4 ) 1 1 −2
∂y 2 ∂x2
| {z } ∂2u 2 ∂2u 2
∇2 with h = u × S and h = S × u.
∂x2 ∂y 2
1
∇2 u = [u(x + h, y) + u(x − h, y) + u(x, y + h)+
Here, the position of S within the matrix product de-
h2
termines whether it is evaluated row or column wise
u(x, y − h) − 4u(x, y)] + O(h2 ).
(green). The entries in the top right and bottom left
The first derivative can be derived analogously using corner (blue) continue the derivatives from the respec-
differences instead of sums. Now, we want to apply tive other side and thus ensure periodic boundary con-
this and thus determine the second derivative in both ditions.
x and y direction for all points u(x, y). This can be
done through the convolution with Laplace filter
0 1 0 0 0 0 0 1 0
C = 0 −2 0 + 1 −2 1 = 1 −4 1
0 1 0 0 0 0 0 1 0
| {z } | {z } | {z }
∂2 2 ∂2 2 ∇2 h2
h h
∂y 2 ∂x2
4
10 i c b =2.5∗ o n e s ( dim ) ;
11
12 % G e n e r a t e s and r e s h a p e s t h e r i g h t hand s i d e ( l e x i c o g r a p h i c a l o r d e r i n g )
13 i c r a=r e s h a p e ( i c a , [ Nˆ 2 , 1 ] ) ;
14 i c r b=r e s h a p e ( i c b , [ Nˆ 2 , 1 ] ) ;
15 ic = [ ic r a ; ic r b ] ;
5
Solving the system of ODEs
We now hand the vectorised initial conditions and the derivation matrix to a built-in ODE solver for temporal
discretisation. This solver then returns approximated distributions for each time step.
1 [ T, C]= ode23 (@( t , u ) o defu n ( t , u , L2 , i c ) , t s p a n , i c ) ;
2
3 f u n c t i o n dydt = odef un ( t , u , Lh , i c )
4 D1=1;
5 D2=40;
6 a1 = 0 . 1 ;
7 a2 = 0 . 9 ;
8 a 1 =1;
9 a3 =1;
10
11 %D i f f u s i o n o n l y
12 dydt = Lh∗u ;
13 %adding Schnakenberg r e a c t i o n k i n e t i c s
14 %t h e f i r s t component a i s r e p r e s e n t e d by t h e f i r s t h a l f o f t h e r e s h a p e d
15 %v e c t o r u , b i s t h e r e p r e s e n t e d by t h e s e c o n d h a l f a c c o r d i n g l y . When
16 %r e f e r e n c i n g a o r b one t h e r e f o r e has t o s e l e c t a l l e n t r i e s i n t h e f i r s t o r
17 %s e c o n d h a l f o f u . D i f f u s i o n a c t s on both a and b and t h e r e f o r e on t h e
18 %e n t i r e v e c t o r u . R e a c t i o n term f ( a , b ) o n l y a c t s on a w h i l e r e a c t i o n term
19 %g ( a , b ) o n l y a c t s on b . They a r e t h e r e f o r e added s e p e r a t e l y .
20 dydt ( 1 : end / 2 ) = D1 . ∗ dydt ( 1 : end / 2 ) . . .
21 + a1 −a 1 . ∗ u ( 1 : end / 2 )+a3 . ∗ u ( 1 : end / 2 ) . ˆ 2 . ∗ u ( ( end /2+1) : end ) ;
22 dydt ( ( end /2+1) : end ) = D2 . ∗ dydt ( ( end /2+1) : end ) . . .
23 + a2 − a3 . ∗ u ( 1 : end / 2 ) . ˆ 2 . ∗ u ( ( end /2+1) : end ) ;
24 end
Runtime complexity
As can be seen above , this approximation of the laplace operator alone requires N 2 operations at each grid
points totalling up to O(N 4 ). In addition, the ode23 is an implementation of an explicit Runge-Kutta (2,3)
pair. The explicit temporal discretisation limits the size of time steps that can be evaluated without leading to
instabilities. This further slows down the computation.
and therefore
with the spatial frequency or wave number k. The
Fourier transform is an integral operator and therefore ∂2 ∂2
both linear and continuous. The differential operator F{f (x)} = F{ f (x)} = −k 2 f˜(x).
∂x2 ∂x2
is linear as well and can thus be moved inside the in-
tegral: The RD system
∂2 ∂2 ∂u
F{f (x)} = F{ f (x)}. = ∇2 u + f (u)
∂x2 ∂x2 ∂t
6
can therefore be reformulated as size. For a equidistant grid with ∆x = ∆y = h the
∂2 ∂2
wave numbers for ∂x 2 and ∂y 2 are therefore identical.
∂u
F{ } = −k 2 ũ + F{f (u)} The so called Nyquist critical frequency kc = 2∆x 1
is
∂t
∂ ũ the highest frequency that can be resolved in a grid
= −k 2 ũ + F{f (u)}. with mesh size ∆x. Higher frequencies would be lost
∂t
in between the discrete grid points. For a grid with N
This ODE now only contains a first derivative with re- grid points, the discrete Fourier transform can generate
wave numbers kn = N n∆x with n ∈ −N −N N
spect to time. 2 , 2 + 1, ... 2 ,
−1 1
A short remark regarding the wave number k: the i.e. from 2∆x to 2∆x . On a domain of length L and
continuous Fourier transform decomposes a continu- L
with ∆x = N this corresponds to kn = Ln .
ous signal into a continuous frequency spectrum. A Since Fourier series are based on sin waves this dis-
discrete Fourier transform of a discrete signal conse- cretisation is inherently periodic. There are approaches
quently only provides a discrete number of frequencies. for spectral methods to handle non-periodic problems
The number of these frequencies depends on the mesh that are not discussed here.
7
These matrices are then squared element-wise, added and multiplied with the respective element in the Fourier
transform of components a and b. MATLAB requires a certain order of these wave numbers.
1 %mat r i x o f wavenumbers
2 k =[2∗ p i / dim x ∗ ( 0 :N/ 2 ) 2∗ p i / dim x ∗(−N/2+1: −1) ] ;
3 [ Kx , Ky]= meshgrid ( k ) ;
4 Kx2=Kx . ˆ 2 ; Ky2=Ky . ˆ 2 ;
5 K=Kx2+Ky2 ;
Runtime complexity
This approximation of the laplace operator requires one scalar multiplication at each grid point. This reduces
runtime complexity by the factor of N 2 in comparison to the finite differences method described above. The non-
linear terms in our RD system have to be evaluated in real space since they depend on the spacial distribution of
our components a and b. When time stepping can be realised implicitly - i.e. when there are no non-linearities
in our PDE - no back-transformation into real space is required. This reduces runtime significantly since the
fft (fast Fourier transform) function scales with O(N logN ). With this semi-implicit time stepping the size of
the time steps can be increased without risking instabilities since the explicit method evaluates no derivatives.
Taken together this implementation of Fourier discretisation drastically reduces computation time.
References
James D. Murray. Mathematical Biology. Springer, 2003.
Hannes Uecker. A short ad hoc introduction to spectral methods for parabolic PDE and the Navier-Stokes equationsr.