Sei sulla pagina 1di 8

Numerical Solutions to 2D Reaction Diffusion Systems in Matlab:

Finite Difference and Spectral Methods


Katharina F Becker - Introduction and Manual

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.

PDEs: A very short introduction Laplace operators. In a Cartesian coordinate system


they are given by the sum of the first and the unmixed
Partial differential equations (PDEs) comprise rates of second order partial derivatives of a function with re-
change with respect to multiple continuous variables, spect to each space variable respectively. For a two
i.e. their partial derivatives. Intuitively, they can dimensional space this equals
be understood as instructions on how a given system
changes from one point in time or space to the next. ∇u = ∂x u + ∂y u + ∂z u,
Given an initial distribution of a substance in space ∇2 u = ∆u = ∂xx u + ∂yy u + ∂zz u.
one could then formulate a differential equation that
describes how this substance diffuses over the spacial
domain in time or include functional terms that de- Example: The Diffusion equation
scribe the production or degradation of the substance The classical diffusion equation in one dimension is:
depending on its own local concentration or other vari- ∂u ∂2
ables. Problems with a given initial distribution and = Du 2 u.
∂t ∂x
differential equations describing the rates of change are
In two dimension it can be written as
called initial-value problems.
∂u  ∂2 ∂2 
Some PDEs can be solved analytically. In these
= Du + u
cases, the equations whose derivatives comprise the ∂t ∂x2 ∂y 2
PDEs can be determined. Many (bio-)physical mod- or
elling applications, however, require iterative numerical
∂u
approximations of these solutions for each time step. = Du ∇2 u. (1)
∂t
Notation The first derivative of a function u with re-
spect to a variable x can be written as Here, u is the distribution of a substance that diffuses
with diffusion coefficient Du . The notation of (1) also
∂u hold for three dimensions. Note, that all differential
= ∂x u,
∂x equations that describe changes in a two or higher di-
mensional system are necessarily PDEs since they con-
and the second derivative as
tain partial derivatives with respect to multiple space
∂2u dimension.
= ∂xx u.
∂x2 Equation (1) is clearly a PDE - even in the one di-
mensional case - since it contains partial derivatives,
Mixed derivatives can be noted as
namely the first derivative with respect to time and
∂ ∂u ∂2 the second derivative with respect to space. This PDE
= = ∂xy u. can be solved analytically. If u has a concentration
∂x ∂y ∂x∂y
distribution at t = 0 of
To simplify the notation of derivatives with respect to
multiple space dimensions one often uses the Nabla and u(x, 0) = u0 δ(x),

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

∂2u ∂2u General approach To evaluate derivatives numeri-


+ 2 = 0. cally they are discretised. The PDEs are therefore no
∂x2 ∂y
longer evaluated on an infinite domain but rather on an
Parabolic PDEs contain, in addition to the Laplace op- interval that is itself subdevided into a finite number
erator, a first order derivative, usually with respect to of grid points. For simplicity we assume a quadratic
time. A prime example is the diffusion equation given mesh for the spatial domain where the step size is
above. Hyperbolic PDEs also contain a second order ∆x = ∆y = h. The distribution of th substance u
derivative with respect to time that has a different sign. at each grid point can then be iteratively determined
The wave equation for each discrete time step ti = t0 + i∆t with step size
∆t.
∂2u ∂2u
α2 =
∂x2 ∂t2
is such a hyperbolic equation.
Boundary conditions While one can evaluate func-
tions on an infinite domain analytically one cannot do
so numerically. One therefore has to choose appro-
priate boundaries for any given problem. Boundaries
are, however, not only relevant in simulations but oc-
cur naturally, when (bio-)physical processes are limited
to finite areas or volumes. Depending on the process Figure 1: 2D space grid for time steps ti (purple) and
to be investigated one can choose different boundary ti+1 (cyan).
conditions. Dirichlet boundary conditions determine For the approximation of PDEs one generally dis-
the value of the function on all boundaries. For a one cretises the space derivative terms and transfers the
dimensional system on the interval [a, b] they can be PDEs into a system of ordinary differential equations
written as (ODEs). These can then be solved through temporal
discretisation, either manually or by using a built-in
u(a) = α, u(b) = β, MATLAB solver. For the finite differences and the

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

MATLAB: Finite Differences Method


Transforming the 2D PDE into a system of ODEs
We will now solve a RD system with two components a and b numerically in MATLAB using the Finite
Differences Method:
∂a ∂a
= Da ∇2 a + f (a, b) ⇒ = Da LH × a + f (a, b)
∂t ∂t
∂b ∂b
= Db ∇2 b + g(a, b) ⇒ = Db LH × b + g(a, b).
∂t ∂t
First, we reshape the two-dimensional initial condition of a and b into a vector using lexicographic ordering.
We then concatenate them into a single vector. With a suitable spatial discretisation we thus transfer our PDE
system into a system of 2N 2 ODEs, since there are N 2 grid points to be evaluated for each component.
1 % g r i d −− s p a c e
2 dx = 0 . 0 5 ; dim x =5; x=0: dx : dim x ; y=0: dx : dim x ;
3 dim = [ l e n g t h ( x ) l e n g t h ( y ) ] ; N = dim ( 1 ) ;
4 % g r i d −− time
5 t max = 2 ; dt = t max / 1 0 0 ; t s p a n = 0 : dt : t max ;
6
7 %The RD system has two components a and b .
8 % set i n i t i a l conditions
9 i c a =1.9∗ o n e s ( dim ) ;

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 ] ;

Constructing the derivation matrix


Now, we construct the derivation matrix that when multiplied with the vectorised form of a component a
determines its second order derivative ∇2 a = LH × avec . This matrix has dimensions N 2 × N 2 . To construct
this matrix we utilise the Kronecker product that generates a block matrix of its two operands. We use matrix
S to evaluate derivatives in x and y direction separately: LH = h12 (1N ⊗ S + S ⊗ 1N ). The Kronecker product
generates a matrix shaped in a way that allows it to evaluate the derivatives correctly, event when the two-
dimensional distribution of a component is reshaped into a lexicographically ordered vector. For more than
one component we need to expand the matrix. For components u1 , ...ur this results in the rN 2 × rN 2 matrix
Lr = 1r ⊗ LH .
1 % C o n s t r u c t s t h e c o r r e s p o n d i n g matrix Lh by u s e o f t h e k r o n e c k e r p r o d u c t
2 % t h i s combines t h e one d i m e n s i o n a l d e r i v a t i v e i n x with t h e one
3 % dimensional derivative in y d i r e c t i o n .
4 I N=s p e y e (N) ;
5 S=s p d i a g s ( repmat ( [ 1 , − 2 , 1 ] ,N, 1 ) , [ 1 , 0 , − 1 ] ,N,N) ;
6
7 % P e r i o d i c Boundary C o n d i t i o n s
8 S ( 1 ,N) =1;
9 S (N, 1 ) =1;
10
11 % D e r i v a t i o n m atrix
12 Lh=(1/dx ˆ 2 ) . ∗ ( kron ( I N , S )+kron ( S , I N ) ) ;
13 L2=kron ( s p e y e ( 2 ) , Lh ) ;

The function reshape generates a column vector by concatenating column-wise:


 
u11 u12 u13 >
u =  u21 u22 u23  , ur = u11 u21 u31 u12 u22 u32 u13 u23 u33 .
u31 u32 u33
The example below illustrates how the matrix product of the reshaped vector and the derivation matrix LH yields
the second derivative. For simplicity
 we choose N = 3 andevaluate the derivative at position u21 only. With
1 · u11 0 · u12 0 · u13
periodic boundaries we expect  −4 · u21 1 · u22 1 · u23  , and thus ∇2 u21 ≈ h12 (u11 −4u21 +u31 +u22 +u23 ).
1 · u31 0 · u32 0 · u33
The same result is obtained through the matrix multiplication LH × u:
−4 1 1 1 0 0 1 0 0 u11
   
 
 1 −4 1 ...
 0 1 0 0 1 0   u21 
  
 u11 − 4u21 + u31 + u22 + u23 
 1 1 −4 0 0 1 0 0 1   u31   
     ... 
 1 0 0 −4 1 1 1 0 0   u12   
1  1 
     
h2 
0 1 0 1 −4 1 0 1 0  ×  u22  = h2 
   

 0
 0 1 1 1 −4 0 0 1   u32 
   



 1
 0 0 1 0 0 −4 1 1 
  u13 
  



 0 1 0 0 1 0 1 −4 1   u23 
0 0 1 0 0 1 1 1 −4 u33
 
LH 0
For two components the matrix would be extended in the form L2 = . This ensures that only
0 LH
components of u are evaluated to determine ∇2 u, and only components of v are evaluated to determine ∇2 v.

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.

Spectral Methods It follows, that


Z
In a Fourier series a function f (x) of the real (spa- f˜0 (k) = f 0 (x)e−kxi dx
tial) variable x is approximated as the sum of sine
waves. The Fourier transform, in turn, decomposes partial integration:
a given function into these sine waves and represents Z
them by their frequency rather than their spatial ex- =− f (x)(−ike−kxi )dx
pansion. The Fourier transform of a function f (x) is Z
= ik f (x)e−kxi dx
Z
F{f (x)} = f˜(k) = f (x)e−kxi dx = ik f˜(x)

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.

MATLAB: Fourier Discretisation


Transforming the 2D PDE into a system of ODEs
We will now solve a RD system with two components a and b numerically in MATLAB using the Fourier
Discretisation or Spectral Method:
∂a ∂ã
= Da ∇2 a + f (a, b) ⇒ = −Da k 2 ã + F{f (a, b)}
∂t ∂t
∂b ∂ b̃
= Db ∇2 b + g(a, b) ⇒ = −Db k 2 b̃ + F{g(a, b)}.
∂t ∂t
The distribution of our variables a and b can remain in matrix form since the Laplace operator can later on be
replaced by the element wise multiplication with a scalar wave number.
1 %S e t t i n g t h e domain . For t h e f a s t f o u r i e r t r a n s f o r m t o work e f f i c i e n t l y
2 %with N logN c o m p l e x i t y t h e number o f g r i d p o i n t i n each d i m e n s i o n s h o u l d
3 %be a power o f 2 . Here , t h e g r i d i s s q u a r e d with dx=dy f o r s i m p l i c i t y .
4 N=2ˆ8; %Number o f g r i d p o i n t s i n x and y d i r e c t i o n
5 dx = . 1 ; dim x = N∗dx ; x = dx : dx : dim x ; y = dx : dx : dim x ;
6 dim = [ l e n g t h ( x ) l e n g t h ( y ) ] ; dt = 0 . 0 4 2 ; T = 2 0 0 ;
7
8 %The RD system has two components u and v .
9 %i n i t i a l c o n d i t i o n s
10 i c a =5∗ o n e s ( dim ) +0.1∗ rand (N) −0.05∗ o n e s ( dim ) ;
11 i c b =5∗ o n e s ( dim ) +0.1∗ rand (N) −0.05∗ o n e s ( dim ) ;
12
13 %i n i t i a t i o n
14 u = ic a ;
15 v = ic b ;
16
17 %p a r a m e t e r s
18 c1 = 0 . 1 ; c2 = 0 . 9 ; c 1 =1; c3 =1;
19 Du=1; Dv=40;
20
21 %non−l i n e a r f u n c t i o n s i e . r e a c t i o n k i n e t i c s ( Schnakenberg )
22 f=@( u , v ) c1−c 1 . ∗ u+c3 . ∗ u . ˆ 2 . ∗ v ;
23 g=@( u , v ) c2−c3 ∗u . ˆ 2 . ∗ v ;

Constructing the wave number matrix


To solve this system with Fourier discretisation we need to generate a matrix with wave numbers that has
the dimensions of our spacial grid. The derivatives in x and y direction are each represented by one matrix.

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 ;

Solving the system of ODEs


The linear components of our ODE system can now be solved implicitly. The non-linear function terms are
solved explicitly. This semi-implicit time stepping is given by
ãt+∆t − ãt
= −Da k 2 ãt+1 + F{f (at )}
∆t
ãt+∆t − ãt = −∆tDa k 2 ãt+∆t + ∆tF{f (at )}
ãt+∆t (1 + ∆tDa k 2 ) = ãt + ∆tF{f (at )}
ãt + ∆tF{f (at )}
ãt+∆t = .
1 + ∆tDa k 2
The denominator is a scalar depending on the wave number. It remains constant for a given grid and can thus
be evaluated once in the beginning. We here refer to the denominator as µ.
1 %i m p l i c i t time d i s c r e t i s a t i o n f o r l i n e a r d i f f u s i o n
2 mu a=1./(1+ dt ∗K∗Du) ;
3 mu b=1./(1+ dt ∗K∗Dv) ;
4 f o r i = 1 :T/ dt
5 %semi−i m p l i c i t time s t e p p i n g v i a FFT
6 %f a s t f o u r i e r t r a n s f o r m o f u , v , f ( u , v ) and g ( u , v ) i n t o f r e q u e n c y s p a c e
7 a F=f f t 2 ( a ) ; b F=f f t 2 ( b ) ;
8 f F=f f t 2 ( f ( a , b ) ) ; g F=f f t 2 ( g ( a , b ) ) ;
9
10 %i m p l i c i t : Euler , e x p l i c i t : E u l e r
11 a F=mu a . ∗ ( a F+dt ∗ f F ) ;
12 b F=mu b . ∗ ( b F+dt ∗ g F ) ;
13
14 %t r a n s f o r m i n g a and b back i n t o r e a l s p a c e
15 a= i f f t 2 ( a F ) ;
16 b= i f f t 2 ( b F ) ;
17
18 %f o r p l o t t i n g u s e r e a l p a r t o n l y : a p l o t=r e a l ( a ) ;
19 end
20 toc

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.

Potrebbero piacerti anche