You are on page 1of 12

Finite Dierence Methods Basics

Zhilin Li Center for Research in Scientic Computation & Department of Mathematics North Carolina State University Raleigh, NC 27695, e-mail: zhilin@math.ncsu.edu

Examples of dierential equations in one space dimension


Newton cooling model: du = c(usur u), dt t>0 u(0) = u0 is given (1)

This is an ordinary dierential equation (ODE) with an initial value u(0) is given. It is also called an initial value problem (IVP). Heat diusion and heat equations. u t = u , a < x < b, t > 0, x x 2u = 2, if is a constant, x u(b, t) = g2 (t), the boundary condition, (2) (3) (4) (5)

u(a, t) = g1 (t), u(x, 0) = u0 (x),

the initial condition.

The equation is a partial dierential equation (PDE) with initial and boundary conditions. It is also called an initial and boundary value problem (IBVP). The following equation is called one dimensional linear parabolic equation with a source; u = t x u x + (x, t) u + (x, t)u + f (x, t), t a < x < b, t > 0, (6)

with a boundary condition at two ends and an initial condition. The one way wave equation (rst order PDE) u u + c = 0, a < x < b, t > 0, t x if c > 0, the boundary condition, u(a, t) = g1 (t), u(x, 0) = u0 (x), the initial condition, 1 (7) (8) (9)

Finite Dierence Method where c is called the wave speed. Note that there is only one boundary condition. The following equation is called one dimensional rst order linear PDE u t = c u + (x, t)u + f (x, t), x a < x < b, t > 0,

(10)

with a boundary condition at one end and an initial condition. One-dimensional elliptic equations (steady state solution of the parabolic dierential equation). x u x + (x) u + (x)u = f (x), a < x < b, x u(a) = ua , u(b) = ub , the boundary condition (11) (12)

It is an ODE and is called a two-point boundary value problem (BVP). It is an elliptic type dierential equation.

1.1

General linear second order partial dierential equations:


a(x, y)uxx + 2b(x, y)uxy + c(x, y)uyy + d(x, y)ux + e(x, y)uy + g(x, y)u = f (x, y), (13)

where (x, y) is a domain in x-y coordinates. The PDE above can be classied as one of the following: Elliptic equation if b2 ac < 0 for all (x, y) . Examples include the Laplace equation uxx + uyy = 0, and the Poisson equation uxx + uyy = f (x, y). (15) (14)

Parabolic equation if b2 ac = 0 for all (x, y) . One example is the heat equation with/without a source ut = uxx + f (x, t). An example in two space dimensions is ut = (uxx + uyy ) + f (x, y, t). (17) (16)

Hyperbolic equation if b2 ac > 0 for all (x, y) . Example includes the two way wave equation utt = c2 uxx . Mixed type if b2 ac changes sign in . (18)

Finite Dierence Method

Finite dierence approximations

A nite dierence method typically involves the following steps: 1. Generate a grid, for example (xi , t(k) ), where we want to nd an approximate solution. 2. Substitute the derivatives in an ODE/PDE or an ODE/PDE system of equations with nite dierence schemes. The ODE/PDE then become a linear/non-linear system of algebraic equations. 3. Solve the system of algebraic equations. 4. Implement and debug the computer code. 5. Do the error analysis, both analytically and numerically. Note that besides Finite Dierence methods, there are other methods that can be used to solve ODE/PDEs such as Finite element methods, spectral methods etc. Generally nite dierence are simple to use for problems dened on regular geometries, such as an interval in 1D, a rectangular domain in two space dimensions, and a cubic in three space dimensions.

Some commonly used nite dierence formulas

Below we list three commonly used nite dierence formulas to approximate the rst order derivative of a function u(x) using the function values only. The forward nite dierence D+ u(x) = u(x + h) u(x) 1 = u (x) + u ()h. h 2 (19)

Therefore the error (absolute error) of the forward nite dierence is proportional to h and the approximation is referred as a rst order approximation. The backward nite dierence D u(x) = u(x) u(x h) 1 = u (x) u ()h. h 2 (20)

The approximation again is a rst order approximation. The central nite dierence D0 u(x) = u(x + h) u(x h) h2 = u (x) + u (). 2h 6 (21)

Finite Dierence Method

The error is proportional to h2 and the approximation is referred as a second order approximation. Note that D0 u(x) = 1 (D+ u(x) + D+ u(x)) 2 (22)

3.1

Approximation to high order derivatives

Usually nite dierence schemes for high order derivatives can be obtained from the formulas for lower order derivatives. The central nite dierence scheme for the second order derivative uxx is u(x) u(x h) h (23) u(x h) 2u(x) + u(x + h) h2 (4) = = uxx (x) + u (). h2 12 So it is a second order approximation. The third order derivative uxxx can be approximated,
2 D0 u(x) = D+ D u(x) = D+

for example u(x + 2h) 3u(x + h) + 3u(x) u(x h) 2 D3 u(x) = D+ D0 u(x) = h3 (24) h (4) = uxxx (x) + u (). 2 There are other approaches to construct nite dierence schemes, for example, the method of un-determined coecients, polynomial interpolations and others.

4
4.1

Finite dierence approximations for model problems


Newton cooling model

The dierential equation is: du = c(usur u), dt The nite dierence equation is uk+1 uk = c(usur uk ), t u0 = u 0 (25) t>0 u(0) = u0 is given

where uk is an approximation to the solution u(tk ) = u(kt). The forward nite dierence approximation to ut is called the forward Euler method.

4.2

General 1D parabolic equation

Consider the dierential equation: u 2u = 2 + f (x, t), t x a < x < b, t > 0.

Finite Dierence Method In order to derive a nite dierence scheme, we need to generate a grid xi = a + ih, tk = k t, i = 0, 1, , m, k = 0, 1, . h= ba , m

The t should satisfy the time step restriction (to guarantee the stability) 0 < t The nite dierence scheme is uk+1 uk i i t k = 0, 1, , uk 2uk + uk i i+1 = i1 + fik , h2 i = 1, 2, , m 1, if u(a, t) and u(b, t) are given, (27) h2 . 2 (26)

where uk is an approximation to u(x, t), and fik = f (xi , tk ). One of nite dierence schemes i for 1D general parabolic equation u u 2u = 2 + (x, t) + (x, t)u + f (x, t), t x t is
k uk uk 2uk + uk uk+1 uk i i+1 i1 k u k i i + i i+1 = i1 + i uk + fik . i t h2 2h

(28)

However, the time step restriction is dierent.

4.3

The one way wave equation

The dierential equation is: u u +c = f (x, t), t x A simple nite dierence scheme is uk u k uk+1 uk i1 i i +c i = fik , t h i = 0, 1, , if c > 0. (29) a < x < b, t > 0.

Question: Can we use the central nite dierence approximation for ux ?

4.4

One dimensional elliptic equations.

The dierential equation is: u 2u + (x)u = f (x), a < x < b, + (x) x2 x u(a) = ua , u(b) = ub , the boundary condition.

Finite Dierence Method The second order nite dierence approximation is ui1 2ui + ui+1 ui+1 ui1 + i ui = fi , + i 2 h 2h i = 1, 2, m 1.

(30)

Note we need to solve a linear system of equations to get the approximate solution to the dierential equation. Question: Can you write down the system of equations in matrixvector form? In order to have a nite dierence scheme work, two conditions need to be satised: the consistence and the stability.

The local truncation error and consistence of a nite dierence scheme

The local truncation error is dened as the dierence of the dierential equation and the nite dierence scheme. The nite dierence scheme is called consistent if the limit of the local truncation error is zero as h and/or t approach zero. The local truncation errors for the forward, backward, and central nite dierence are Th (D+ ) = f (x) Th (D ) = f (x) Th (D0 ) = f (x)
h0

h f (x + h) f (x) = f () h 2 f (x) f (x h) h = f () h 2 h2 (4) f (x + h) f (x h) = f (). 2h 6

In all three cases, we have lim Th = 0. Therefore they are all consistent.

5.1

The truncation error of the nite dierence method for the Newton cooling model (25)
Th = u(t + h) u(t) c(usur u(t)). t (31)

Therefore, the local truncation error is simply the result of the nite dierence equation with exact solution plugged in. Using the extended mean value theorem, we can get Th = t u (), 2 and
t0

lim Th = 0.

(32)

So the nite dierence equation is consistent. A Remark: In the literature, there is another denition of the truncation which is Th = u(t + h) u(t) c t (usur u(t)). (33)

Finite Dierence Method We can see that Th = t Th .

(34)

5.2

The truncation error of the nite dierence method for the 1D parabolic equation (27)
u(x + h, t) 2u(x, t) + u(x h, t) u(x, t + h) u(x, t) f (x, t). t h2

Th =

(35)

Again the local truncation error is simply the result of the nite dierence equation with exact solution plugged in. Using the extended mean value theorem, we can get Th = ut (x, t) + t h2 utt (x, ) uxx (x, t) + uxxxx (, t) f (x, t) 2 6 2 t h = utt (x, ) uxxxx (, t) 2 6 = O(t, h2 ). (36) (37) (38)

The method therefore is called rst order in time and second order in space. Since
t0, h0

lim

Th = 0,

(39)

the nite dierence scheme is consistent.

5.3

The truncation error of the nite dierence method for the 1D one-way wave equation (29)
u(x + h, t) u(x, t) u(x, t + h) u(x, t) +c f (x, t), t h t h2 = ut (x, t) + utt (x, ) cux (x, t) c uxx (, t) 2 2 = O(t, h), lim Th = 0.
t0, h0

Th =

c>0

(40) (41) (42)

Therefore the nite dierence scheme is consistent and is rst order in time and rst order in space.

The stability of a nite dierence scheme

As we see from the numerical test, whether a nite dierence scheme can work depend on the choice of t even if the nite dierence scheme is consistent. The stability condition is

Finite Dierence Method

a requirement that the error in the computed solution would be amplied in the subsequent computations. An intuitive denition is that |uk+1 | + |uk+1 | + |uk+1 | + + |uk+1 | + |uk+1 | + |uk+1 | m 1 2 3 m2 m1 |uk | + |uk | + |uk | + + |uk | + |uk | + |uk | m 1 2 3 m2 m1 The stability condition of the nite dierence (25) for Newton cooling model is 2 0 < t . c This can be proved through the von Neumann analysis by setting uk+1 = g uk Then the scheme is stable if and only if |g| 1. The stability condition of the nite dierence scheme (27) for the heat equation with a source term is h2 . 2 This can be proved using the von Neumann analysis by setting 0 < t uk = eijh , j uk+1 = g()eijh , j (46) (43)

(44)

(45)

(47)

where i =

1. Then the nite dierence scheme (27) is stable if |g| 1.

The stability condition of the nite dierence scheme (29) for the one way wave equation is 0 < t using the von Neumann analysis. The global error (overall error) of a nite dierence scheme is the absolute error of the computed solution. For the solution of the nite dierence scheme for the Newton cooling model, it is E = ukf inal u(kf inal t) For one dimensional heat equation and one way wave equation, the global error is Ei = ui f inal u(xi , tf inal ), i = 1, 2, , m Usually we use one measurement called the innity normal of the error ||E|| = max{ |E1 |, |E2 |, , |Em1 |, |Em | } (51)
k

h . c

(48)

(49)

(50)

A nite dierence method is convergent to the true solution if the global error approaches to zero as and h approach to zero. Theorem 1 A consistent and stable nite dierence method is convergent.

Finite Dierence Method

7
7.1

Implicit Discretization
Backward Euler for IVP of an ODE system.

Given an initial value problem of the following form dy = f (y, t), y(0) = y0 . dt the forward Euler method is yk+1 yk = f (yk , tk ), k = 0, 1, t Usually there is a time step constraint on t. For example, for the Newtons cooling law y = c (ysur y), c > 0.

The forward nite dierence method is y k+1 y k = c (ysur y k ). t 1 We need to choose 0 < t to get a stable algorithm so that the computed solution is a c reasonable approximation to the true solution of the ODE. If we use the backward nite dierence, often called the backward Euler method, for y (t) at tk+1 , then we get the backward Euler method yk+1 yk = f (yk+1 , tk+1 ), k = 0, 1, (52) t The method works for much larger t that that of implicit method. For example, for the Newtons cooling law, the backward Euler method is y k+1 y k = c (ysur y k+1 ). t We can choose any t and the solution will not blow-up. However, to get reasonable accuracy, we still need to choose t small because the global error is proportional to t. We do not need to worry about the stability anymore. If we use the central nite dierence scheme for y (t), yk+1 yk1 = f (yk , tk ), k = 0, 1, (53) t the scheme may or may not be stable, however, we need to use a dierent method to compute y1 before we can get started. Quite often, the Crank-Nicolson method is an ideal method because it has second order accuracy and it is implicit. The Crank-Nicolson method for the IVP-ODE is yk+1 yk1 1 = f (yk , tk ) + f (yk+1 , tk+1 ) . t 2 The global error is O((t)2 ). (54)

Finite Dierence Method

10

7.2

The 1D heat equations

For the heat diusion equation ut = uxx + f (x, t), > 0,

the forward Euler method (FW-CT, meaning forward in time, central in space) is uk 2uk + uk uk+1 uk i i+1 i i = i1 + fik . 2 t h It is rst order in time and second order in space (O(t + h2 )). There is a severe time step size constraint 0 < t h2 /(2) which potentially makes the method very slow. Usually we prefer to use t h so that we can advance quickly. The backward Euler method (BW-CT) scheme is uk+1 2uk+1 + uk+1 uk+1 uk i i+1 i i = i1 + fik+1 . t h2 (55)

The truncation error is O(t + h2 ). It is unconditionally stable meaning that we can take any t. However, we need to solve a linear system of equations below 2 k+1 1 h2 0 0 uk+1 f1 + h2 u(0, tk+1 ) 1 t + h2 k+1 k+1 1 u2 h2 f2 + 2 h2 0 t h2 . . . . . . . . . . . . . . = . . . . . . . k+1 1 h2 uk+1 fm2 0 h2 t + 2 m2 h2 2 k+1 1 0 0 h2 uk+1 fm1 + h2 u(L, tk+1 ) m1 t + h2

to get an approximate solution of the dierential equation at t = tk+1 The system of equations is a tridiagonal and it is very to solve, in Matlab, we can simply use the command u = A\b, see heat im.m for example.

7.3

Central discretization for ut ?

The central nite dierence of ut for heat diusion equations is unconditionally unstable (no matter what t we choose).

Finite Dierence Method

11

7.4

The Crank-Nicolson scheme for 1D heat equation.

If we want to take t h and keep second order accuracy, then we can use the Crank-Nicolson scheme uk+1 uk i i = t 2 uk+1 2uk+1 + uk+1 uk 2uk + uk i i+1 i1 i i+1 + i1 h2 h2 + fi
k+ 1 2

(56)

The local truncation error is O((t)2 + h2 ) and it is unconditionally unstable (no matter what t we choose). We usually take t = h. The global error is O(h2 +t)2 ). However, we need to solve the system of equations Auk+1 = Buk + bk to get the solution at time tk+1 = (k + 1)t, where A, B, and bk are given below: A = B =
1 t

h2

2h2 1 t

0
2h2

. . .
1 t

0 0 . . .
h2 2h2 1 t

k b =

f1

k+ 1 2

h2

u(0, tk+ 2 )

2h2

+ . . .

h2

k+ 1 f2 2

. . . 0 0
1 t

. . .
2h2

. . .
2 fm2 2 fm1 +

k+ 1

0 0
h2 2h2

2h2

h2

k+ 1

2h2

h2 1 t

2h2

. . .
1 t

0 0 . . .
h2 1 t 2h2

h2

u(L, tk+ 2 )

. . .

. . . 0 0

. . .
2h2

2h2

h2

If we denote Fk = Buk + bk , then the approximate solution the dierential equation at tk+1 is the solution of Auk+1 = Fk which can be solved easily, for example, in Matlab, by u = A\F, see heat im.m.

7.5

Implicit method for the 1D one-way wave equations

The 1D one-way wave equation ut + cux = f (x, t), 0 < x < L, u(0, t) = g(t), u(x, 0) = u0 (x),

is a hyperbolic type equation. The FW-BW (forward in time and backward in space) nite dierence scheme is uk u k uk+1 uk i1 i i +c i = fik , t h i = 0, 1, , if c > 0,

Finite Dierence Method

12

whose truncation error is O(t + h) (rst order in time and rst order in space). The time step restriction t h/c is often acceptable (not like the explicit nite dierence method for the heat equation where t h2 /(2)). The implicit nite dierence scheme is uk+1 uk+1 uk+1 uk i1 i i +c i = fik+1 , t h i = 0, 1, , if c > 0,

We can write the scheme as a matrix-vector form Auk+1 = Buk + bk , A= , bk = ,

d . . .

0 d . . .

0 0 . . . 0 . . . d

0 0 . . . 0 d

k+1 t f1 u(0, tk+1 ) k+1 t f2

B = I,

. . . 0
k+1 t fn

0 0

where d = 1 +

c t c t ,= . The system can be easily solved using a forward substitution h h uk+1 = 1 uk+1 = 2
k+1 uk + t f1 u(0, tk+1 ) 1 d

k+1 uk + t f2 uk+1 2 1 d =

uk+1 = i

uk + t fik+1 uk+1 i i1 d =
k+1 uk + t fn uk+1 n n1 . d

uk+1 = n

You might also like