<<

Chapter 12 Finite Difference Approximations

In the previous chapter we discussed several conservation laws and demonstrated that these laws lead to partial differ- ential equations (PDEs). In this chapter, we will show how to approximate partial using finite differences.

46 Self-Assessment

Before reading this chapter, you may wish to review... • Taylor expansions • Multi-Step Methods 4 After reading this chapter you should be able to... • determine the coefficients for finite difference approximations given a numerical stencil • devise finite difference approximations meeting specifications on order of accuracy Relevant self-assessment exercises: 1-5

47 Finite Difference Approximations

Recall from Chapters 1 - 4 how the multi-step methods we developed for ODEs are based on a truncated Tay- ∂U lor series approximation for . Specifically, we can consider each multi-step method as computing U at dis- ∂t ∂U crete instances in time (t0, t1,..., tn,...) where the = Un is approximated using a combination of ∂t t n (Un+1, Un, Un−1,...). Finite difference methods for PDEs are essentially built on the same idea, but working in space as opposed to time. Namely, the solution U is approximated at discrete instances in space (x0, x1,..., xi−1, xi, xi+1,..., xNx−1, xNx ) ∂U ∂ 2U where the spatial derivatives = Ux , = Uxx ,... are approximated using a combination of (Ui, Ui±1, Ui±2,...).  ∂x i ∂x2 i  i i

∆x

x0 x1 xi−1 xi xi+1 xNx−1 xNx

Fig. 18 One-dimensional structured mesh for finite difference approximations.

61 62 ∂U In the following we derive a finite difference approximation of at node x . (Note: this is essentially the same ∂x i procedure as was used to derive the midpoint method in Chapter 3). For a differentiable function U, the derivative at the point xi is given by:

∂U U(x + ∆x) −U(x − ∆x) = lim i i (95) ∂x ∆x→0 2∆x xi

The finite difference approximation is obtained by eliminating the limiting process:

U(x + ∆x) −U(x − ∆x) U −U − U ≈ i i = i+1 i 1 ≡ δ U . (96) xi 2∆x 2∆x 2x i

The finite difference δ2x is called a central difference operator. Finite difference approximations can also be one-sided. For example, a backward difference approximation is,

1 − U ≈ (U −U − ) ≡ δ U , (97) xi ∆x i i 1 x i and a forward difference approximation is, 1 U ≈ (U −U ) ≡ δ +U . (98) xi ∆x i+1 i x i

Exercise 1. Write a MATLAB function which computes the central difference approximation at nodes x1, x2,..., xNx on the domain [0,1] with periodic boundary conditions (i.e U0 = UNx ). The function should T have the form function dU = centraldiff(U) where U = (U1, U2,..., UNx−1, UNx ) , and dU = δ δ δ δ T ( 2xU1, 2xU2,..., 2xUNx−1, 2xUNx ) .

Exercise 2. Write the function forwarddiff which uses a forward difference approximation with the same input.

Exercise 3. Write the function backwarddiff which uses a backward difference approximation with the same input.

47.1 Local Truncation Error for a Derivative Approximation

In Chapter we determined the local order of accuracy of multi-step methods by computing the truncation error. The same approach may be used to determine the order of accuracy of finite difference approximations. Suppose we use δ − a backwards difference, x Ui to approximate the first derivative, Ux at point i. The local truncation error for this derivative approximation can be calculated using as we have done in the past: 63

τ δ − ≡ x Ui −Uxi 1 = [U −U − ] −U ∆x i i 1 xi 1 1 2 3 = Ui − Ui − ∆xUx + ∆x Uxx + O(∆x ) −Ux ∆x   i 2 i  i 1 = − ∆xU + O(∆x2). 2 xxi Thus, the backwards difference approximation is a first-order accurate discretization of the derivative at node i.

Exercise 4. What is the truncation error and order of accuracy of the central difference approximation? 1 ∆ ∆ 2 (a) 3 xUxxi + O( x ), first-order accurate 1 ∆ 2 ∆ 2 (b) 6 x Uxxxi + O( x ), first-order accurate 1 ∆ 2 ∆ 3 (c) 6 x Uxxxi + O( x ), second-order accurate 1 ∆ 2 ∆ 3 (d) 3 x Uxxxi + O( x ), second-order accurate

47.2 Finite Difference approximations for higher-order derivatives

So far we have developed several finite difference approximations for the first derivative Ux. However, we are generally interested in solving PDEs which may also involve higher spatial derivatives (Uxx, Uxxx,...). As we’ve seen before for first derivative approximations, finite difference approximations for higher derivatives may be obtained from the definition of a derivative. For example, ∂ ∂ U ∆ − U − ∆ ∂ 2U ∂ (x + x/2) ∂ (x x/2) (x)= lim x x ∂x2 ∆x→0 ∆x 1 U(x + ∆x) −U(x) U(x) −U(x − ∆x) = lim − ∆x→0 ∆x  ∆x ∆x  1 = lim [U(x + ∆x) − 2U(x)+U(x − ∆x)]. ∆x→0 ∆x2 The finite difference approximation for the second order derivative is obtained eliminating the limiting process.

2 1 U ≈ δ U ≡ (U − 2U +U − ). (99) xxi x i ∆x2 i+1 i i 1 In practice, generating a finite difference approximation starting from the definition of the derivative can be quite tedious. Instead, we may consider defining a finite difference approximation which minimizes the truncation error using a specific set of points. Consider a generic finite difference approximation of the form:

∂ kU 1 ≈ ∑ α U (100) ∂xk (∆x)k j j i j∈S

where α j are the coefficients for the finite difference approximation. S is called the stencil, and contains the list of δ 2 points used in the finite difference approximation. For example the central difference approximation, x , has a 3-point stencil using the nodal values Ui−1, Ui and Ui+1. Different finite difference approximations are obtained by considering different stencils near Ui. 64

Example 1. 4-point difference approximation We now obtain a four point finite difference approximation for the first derivative using the points Ui−1, Ui, Ui+1 and Ui+2. First consider the Taylor series expansions about point Ui, − ∆ 1 ∆ 2 − 1 ∆ 3 ∆ 4 Ui−1 = Ui xUxi + 2 x Uxxi 6 x Uxxxi + O( x ) Ui = Ui ∆ 1 ∆ 2 1 ∆ 3 ∆ 4 Ui+1 = Ui + xUxi + 2 x Uxxi + 6 x Uxxxi + O( x ) ∆ 1 ∆ 2 1 ∆ 3 ∆ 4 Ui+2 = Ui +(2 x)Uxi + 2 (2 x) Uxxi + 6 (2 x) Uxxxi + O( x ) Our finite difference approximation has the form: 1 ≈ α Uxi ∆ ∑ jUj x j∈S α α α α 1 = ( i−1 + i + i+1 + i ) ∆xUi +( −αi−1 + αi+1 + 2αi ) Uxi 1 α 1 α 4 α ∆ +( 2 i−1 + 2 i+1 + 2 i) xUxxi − 1 α 1 α 8 α ∆ 2 +( 6 i−1 + 6 i+1 + 6 i) x Uxxxi +O(∆x3)

Since we have 4 coefficients (αi−1, αi, αi+1, αi+2), we may choose these coefficients such that the terms corresponding to Uxi sum to 1, while the terms corresponding to Ui, Uxxi and Uxxxi sum to zero. Using matrix notation: α α − 1 1 111 i−1 0 i−1 3 − α α − 1  1012  i   1   i   2  1 1 4 α = which gives α = . 2 0 2 2 i+1 0 i+1 1  1 1 8        1   − 0  αi 2   0   αi 2   −   6 6 6  +     +   6 

Thus the third-order accurate difference formula using the points (Ui−1, Ui, Ui+1, Ui+2) is:

−2U − − 3U + 6U −U U ≈ i 1 i i+1 i+2 xi 6∆x

Exercise 5. Give the coefficients of the finite difference approximation for Uxxxi using the 5-point stencil (Ui−2, Ui−1, Ui, Ui+1, Ui+2).

Exercise 6. What is the order of accuracy for this finite difference approximation?

47.3 Finite Difference Approximations in 2D

We can easily extend the concept of finite difference approximations to multiple spatial dimensions. In this case we represent the solution on a structured spatial mesh as shown in Figure 19. Using Ui, j to denote the value of U at node (i, j) our central derivative approximations for the first derivatives are: 65

Fig. 19 Two-dimensional structured mesh for finite difference approximations.

∂U Ui 1 j −Ui−1 j ≈ + , , ≡ δ U . ∂x 2∆x 2x i, j i, j

∂U Ui, j+1 −Ui, j−1 ≈ ≡ δ U . ∂y 2∆y 2y i, j i, j

In general, finite difference approximation will involve a stencil of points surrounding Ui, j.