Finite Difference Methods by LONG CHEN
Finite Difference Methods by LONG CHEN
LONG CHEN
The best known methods, finite difference, consists of replacing each derivative by a difference quotient in the classic formulation. It is simple to code and economic to compute.
In a sense, a finite difference formulation offers a more direct approach to the numerical
solution of partial differential equations than does a method based on other formulations.
The drawback of the finite difference methods is accuracy and flexibility. Standard finite
difference methods requires more regularity of the solution (e.g. u C 2 ()) and the triangulation (e.g. uniform grids). Difficulties also arises in imposing boundary conditions.
1. F INITE DIFFERENCE FORMULA
In this section, for simplicity, we discuss Poisson equation posed on the unit square
= (0, 1) (0, 1). Variable coefficients and more complex domains will be discussed
in finite element methods. Furthermore we assume u is smooth enough to enable us use
Taylor expansion freely.
Given two integer m, n 2, we construct a rectangular grids Th by the tensor product
of two grids of (0, 1): {xi = (i 1)hx , i = 1, m, hx = 1/(m 1)} and {yj =
(j 1)hy , j = 1, n, hy = 1/(n 1)}. Let h = max{hx , hy } denote the size of Th .
We denote h = {(xi , yj ) } and boundary h = {(xi , yj ) }.
We consider the discrete function space given by Vh = {uh (xi , yj ), 1 i m, 1
j n} which is isomorphism to RN with N = m n. It is more convenient to use
sub-index (i, j) for the discrete function: ui,j := uh (xi , yj ). For a continuous function
u C(), the interpolation operator Ih : C() Vh maps u to a discrete function and
will be denoted by uI . By the definition (uI )i,j = u(xi , yj ). Note that the value of a
discrete function is only defined at grid points. Values inside each cell can be obtained by
interpolation of values at grid points.
Similar definitions can be applied to one dimensional case. Choose a mesh size h and
u Vh (0, 1). Popular difference formulas at an interior node xj for a discrete function
u Vh include:
uj uj1
The backward difference: (D u)j =
;
h
uj+1 uj
The forward difference: (D+ u)j =
;
h
uj+1 uj1
The centered difference: (D u)j =
;
2h
uj+1 2uj + uj1
The centered second difference: (D2 u)j =
.
h2
It is easy to prove by Talyor expansion that
(D u)j u0 (xj ) = O(h),
LONG CHEN
We shall use these difference formulation, especially the centered second difference to
approximate the Laplace operator at an interior node (xi , yj ):
2
2
(h u)i,j = (Dxx
u)i,j + (Dyy
u)i,j
ui+1,j 2ui,j + ui1,i
ui,j+1 2ui,j + ui,j1
=
+
.
2
hx
h2y
It is called five point stencil since only five points are involved. When hx = hy , it is
simplified to
4ui,j ui+1,j ui1,i ui,j+1 ui,j1
h2
and can be denoted by the following stencil symbol
1
1 4 1 .
1
(1)
(h u)i,j =
For the right hand side, we simply take node values i.e. fi,j = (fI )i,j = f (xi , yj ).
The finite difference methods for solving Poisson equation is simply
(2)
(h u)i,j = fi,j ,
1 i m, 1 j n,
with appropriate processing of boundary conditions. Here in (2), we also use (1) for boundary points but drop terms involving grid points outside of the domain.
Let us give an ordering of N = m n grids and use a single index k = 1 to N for
uk = ui(k),j(k) . For example, the index map k (i(k), j(k)) can be easily written out
for the lexicographical ordering. With any choosing ordering, (2) can be written as a linear
algebraic equation:
(3)
Au = f ,
where A RN N , u RN and f RN .
Remark 1.1. There exist different orderings for the grid points. Although they give equivalent matrixes up to permutations, different ordering does matter when solving linear algebraic equations.
2. B OUNDARY CONDITIONS
We shall discuss how to deal with boundary conditions in finite difference methods.
The Dirichlet boundary condition is relatively easy and the Neumann boundary condition
requires the ghost points.
Dirichlet boundary condition. For the Poisson equation with Dirichlet boundary condition
(4)
u = f in ,
u = g on = ,
the value on the boundary is given by the boundary conditions. Namely ui,j = g(xi , yj )
for (xi , yj ) and thus not unknowns in the equation. There are several ways to impose
the Dirichlet boundary condition.
One approach is to let aii = 1, aij = 0, j 6= i and fi = g(xi ) for nodes xi . Note
that this will destroy the symmetry of the corresponding matrix. Another approach is to
modify the right hand side at interior nodes and solve only equations at interior nodes. Let
us consider a simple example with 9 nodes. The only unknowns is u5 with the lexicographical ordering. By the formula of discrete Laplace operator at that node, we obtain the
adjusted equation
1
4
u5 = f5 + 2 (u2 + u4 + u6 + u8 ).
2
h
h
We use the following Matlab code to illustrate the implementation of Dirichlet boundary
condition. Let bdNode be a logic array representing boundary nodes: bdNode(k)=1 if
(xk , yk ) and bdNode(k)=0 otherwise.
1
2
3
4
5
freeNode = bdNode;
u = zeros(N,1);
u(bdNode) = g(node(bdNode,:));
f = f-A*u;
u(freeNode) = A(freeNode,freeNode)\f(freeNode);
n
A natural approximation to the normal derivative is a one sided difference, for example:
u
u1,j u2,j
(x1 , yj ) =
+ O(h).
n
h
But this is only a first order approximation. To treat Neumann boundary condition more
accurately, we introduce the ghost points outside of the domain and next to the boundary.
We extend the lattice by allowing the index 0 i, j n + 1. Then we can use center
difference scheme:
u
u0,j u2,j
(x1 , yj ) =
+ O(h2 ).
n
2h
The value u0,j is not well defined. We need to eliminate it from the equation. This is
possible since on the boundary point (x1 , yj ), we have two equations:
(6)
(7)
From (7), we get u0,j = 2h g1,j + u2,j . Substituting it into (6) and scaling by a factor 1/2,
we get an equation at point (x1 , yj ):
2u1,j u2,j 0.5 u1,j+1 0.5 uj1 = 0.5 h2 f1,j + h g1,j .
The scaling is to preserve the symmetry of the matrix. We can deal with other boundary
points by the same technique except the four corner points. At corner points, even the
LONG CHEN
norm vector is not well defined. We will use average of two directional derivatives to get
an approximation. Taking (0, 0) as an example, we have
(8)
(9)
(10)
So we can solve u0,1 and u1,0 from (9) and (10), and substitute them into (8). Again to
maintain the symmetric of the matrix, we multiply (8) by 1/4. This gives an equation for
the corner point (x1 , y1 )
u1,1 0.5 u2,1 0.5 u1,1 = 0.25 h2 f1,1 + h g1,1 .
Similar technique will be used to deal with other corner points. We then end with a linear
algebraic equation
Au = f .
It can be shown that the corresponding matrix A is still symmetric but only semi-definite
(see Exercise 2). The kernel of A consists of constant vectors i.e. Au = 0 if and only if
u = c. This requires a discrete version of the compatible condition (5):
N
X
(11)
fi = 0
i=1
1in+1,
1jm+1
The subscript h indicates this norm depends on the triangulation since for different h, we
have different numbers of vi,j . Note that this is the l norm for RN .
We shall prove 1
h : (Vh , k k,h ) (Vh , k k,h ) is stable uniform to h. The
proof will use the discrete maximal principal and barrier functions.
Theorem 3.1 (Discrete Maximum Principle). Let v Vh satisfy
h v 0.
Then
max v max v,
h
i=1
Thus equality holds throughout and v achieves its maximum at all the nearest neighbors
of x0 as well. Applying the same argument to the neighbors in the interior, and then to
their neighbors, etc, we conclude that v is constant which contradicts to the assumption
maxh v > maxh v. The second statement can be proved easily by a similar argument.
Theorem 3.2. Let uh be the equation of
(12)
h uh = fI at h \h ,
uh = gI at h .
Then
(13)
kuh k,h
1
kfI k,h \h + kgI kh , .
8
1
kh uI (u)I k,h \h .
8
The next step is to study the consistence error kh uI (u)I kh, . The following
Lemma can be easily proved by Taylor expansion.
Lemma 3.4. If u C 4 (), then
kh uI (u)I k,h \h
h2
4u
4u
max{k 4 k,h \h , k 4 k,h \h }.
6
x
y
We summarize the convergence results on the finite difference methods in the following
theorem.
Theorem 3.5. Let u be the solution of the Dirichlet problem (4) and uh the solution of the
discrete problem (12). If u C 4 (), then
kuI uh k,h Ch2 ,
with constant
C=
4u
4u
1
max{k 4 k , k 4 k }.
48
x
y
In practice, the second order of convergence can be observed even the solution u is
less smooth than C 4 (), i.e. the requirement u C 4 (). This restriction comes from the
point-wise estimate. In finite element method, we shall use integral norms to find the right
setting of function spaces.
~ =0
u
on @
~ = (f1 , f2 )t .
~ = (u, v)t , and f
where u
2. MAC D ISCRETIZATION
LONG CHEN
( A ) index for p
F IGURE 1. A cell centered uniform grid
( B ) index for u
( C ) index for v
In some applications,
computational
fluid we
dynamics
the Poisson
2.1.notable
MACthe
Scheme.
Suppose
have a(CFD),
rectangular
decomposition, for each cell,
equation is solved on gree
slightly
different
grids.
In
this
section,
we
consider
FDM
for
the edge centers and horizont
of freedoms for u and v are located on the vertical
Poisson equation at cell centers; see Fig 4.
centers, respectively, and the degree of freedoms for pressure p are located at cell c
At interior nodes, the standard (4, 1, 1, 1, 1) but boundary conditions will be
Thedistance
MAC in
scheme
is written
as (interior
= 1) nodes is still h but
treated differently. The
axis direction
between
i 1,j boundary)
i,j away
1
i,j+1the
the near boundary nodes (centers of4u
thei,jcellsutouching
ui+1,j isuh/2
ufrom
pi,j pi,j 1
(2.1)
+
= f1i,j
boundary. One can then
easily verify that for Neumann boundary
condition,
the
stencil
for
2
h
h
near boundary nodes is (3, 1, 1, 1) and for corner cells (2, 1, 1). Of course the
i,j
i 1,j
i+1,j
i,j 1
i,j+1
i 1,j
4v and moved
v
v right handv side.
v
p
pi,j
boundary condition g should be evaluated
to the
(2.2)
+
= f2i,j
2
The Dirichlet boundary condition is more subtle for cell h
centered
difference. We can
h
still introduce the ghost grid points and use standard (4, 1, 1, 1)ui,j+1
stencil for
near v i,j
ui,j
v i+1,j
boundary nodes. But (2.3)
no grid points are on the boundary. The ghost value can be elim-+
=0
h.
h
inated by linear extrapolation, i.e, requiring (u0,j + u1,j )/2 = g(0, yj ) := g1/2,j
8
6u1,j 34 u2,j u1,j1 u1,j+1
3 g1/2,j
=
f
+
.
1,j
h2
h2
We denote the near boundary stencil by (6, 43 , 1, 1, 83 ). The quadratic extrapolation will lead to a better rate of convergence since the truncation error is improved. The
disadvantage of this treatment is that the symmetry of the matrix is destroyed.
For the Poisson equation, there is another way to keep the second order truncation error
and symmetry. For simplicity, let us consider the homogenous Dirichlet boundary condition, i.e., u| = 0. Then the tangential derivatives along the boundary is vanished, in
particular, t2 u = 0. Assume the equation u = f holds also on the boundary condition. Note that on the boundary, the operator can be written as t2 + n2 . We then get
n2 u = f on . The sign is determined by if the norm direction is the same as the
axis direction. Then we can use u1 , u1/2 = 0 and n2 u = f to fit a quadratic function and
(15)
error.
h2
f1/2,j
4