24
Introduction to Finite Difference Methods ME 448/548 Notes Gerald Recktenwald Portland State University Department of Mechanical Engineering [email protected] ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation

Introduction to Finite Di erence Methods · 2020. 1. 4. · Substituting Equation (6.c) into Equation (5) gives A n= 2 L Z L 0 sin ˇx L sin nˇx L dx: The sine function is orthogonal

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

  • Introduction to Finite Difference MethodsME 448/548 Notes

    Gerald Recktenwald

    Portland State University

    Department of Mechanical Engineering

    [email protected]

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation

  • Overview

    1. Motivation for numerical solution to the heat equation

    2. Finite difference notation

    3. Finite difference approximations

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 1

  • Model Problem: 1D Heat Equation

    ∂u

    ∂t= α

    ∂2u

    ∂x2, 0 ≤ x ≤ L (1)

    u(0, t) = 0, u(L, t) = 0 (2)

    u(x, 0) = u0(x) (3)

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 2

  • Example: Place a hot pot on a table.

    What is the transient temperature distribution in the table?

    t < 0 t > 0

    xL

    x

    0

    L

    TTL

    T0

    increasing time

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 3

  • Model Problem: 1D Heat Equation

    Exact Solution

    u(x, t) =

    ∞∑n=1

    un(x, t) =

    ∞∑n=1

    An exp(−αn2π2t/L2) sin(nπx

    L

    )(4)

    An =2

    L

    ∫ L0

    u0(x) sin

    (nπx

    L

    )dx. (5)

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 4

  • Example: Decay of u0(x) = sin(πx/L)

    We will use the following problem to test our finite-difference solutions.

    ∂u

    ∂t= α

    ∂2u

    ∂x2, 0 ≤ x ≤ L, t ≥ 0 (6.a)

    u(0, t) = u(L, t) = 0 (6.b)

    u(x, 0) = sin

    (πx

    L

    ). (6.c)

    This model problem has a simple solution that is convenient as a benchmark for numerical

    schemes for the heat equation.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 5

  • Example: Analytical solution to decay of u0(x) = sin(πx/L)

    Substituting Equation (6.c) into Equation (5) gives

    An =2

    L

    ∫ L0

    sin

    (πx

    L

    )sin

    (nπx

    L

    )dx.

    The sine function is orthogonal in the following sense∫ L0

    sin

    (πx

    L

    )sin

    (nπx

    L

    )dx =

    {L/2 if n = 1

    0 otherwise

    so that

    A1 = 1,

    An = 0, n = 2, 3, . . .

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 6

  • Example: Analytical solution to decay of u0(x) = sin(πx/L)

    Therefore, the analytical solution to the toy problem is

    u(x, t) = exp

    (−απ2t

    L2

    )sin

    (πx

    L

    ). (7)

    The solution is just an exponential decay of the initial condition for this problem and

    problems with Dirichlet BC and no source term.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 7

  • Example: Analytical solution to decay of a square pulse

    u0(x) =

    0, if 0 ≤ x < xluc, if xl ≤ x ≤ xr0, if xr < x ≤ L.

    uc

    xl

    xr

    L0

    An infinite number of An terms are needed to match the initial condition.

    An =2

    L

    ∫ xrxl

    uc sin

    (nπx

    L

    )dx

    =2uc

    [cos

    (nπxl

    L

    )− cos

    (nπxr

    L

    )]n = 0, 1, 2, . . .

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 8

  • Example: Analytical solution to decay of a square pulse

    0 0.2 0.4 0.6 0.8 1−0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    x

    T(x

    ,t)

    t = 1.0e−04

    t = 1.0e−03

    t = 1.0e−02

    t = 5.0e−02

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 9

  • Numerical Model

    ContinuousPDE for u(x,t)

    Discretedifferenceequation

    uik approximationto u(xi,tk)

    Finitedifference

    Algebraicsolution

    orFinite volume

    orFinite element

    orBoundar element

    or ...

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 10

  • Finite Difference Mesh

    The finite difference solution is obtained at a finite set of points. These points are called

    nodes and the network of these nodes is called a mesh or grid.

    On a uniform mesh, the x-direction nodes are located at

    xi = (i− 1)∆x, i = 1, 2, . . . , nx, ∆x =L

    nx − 1(8)

    where nx is the total number of spatial nodes, including those on the boundary.

    i – 1 i + 1i

    ∆x

    x = 0

    ∆x

    i = 1 ...x = L

    i = nx

    xi – 1 xi xi + 1...

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 11

  • Finite Difference Mesh

    Analogous to the x-direction mesh, there are nodes at discrete times.

    For uniform time steps

    tk = (k − 1)∆t, k = 1, 2, . . . , nt, ∆t =tmax

    nt − 1(9)

    where nt is the number of time steps.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 12

  • Finite Difference Mesh

    The x and t direction nodes form a finite version of a semi-infinite strip

    t

    i i+1i 1i=1 nx

    k+1

    k

    k 1

    . . .. . .

    . . .. . .

    . . .. . .

    . . .

    x=Lx=0

    t=0, k=1

    Interior node: u(x,t) is initially unknown.

    Boundary node: u(0,t) and u(0,t) are knownor computable from additional data.

    Initial condition: u(x,0) must be specified.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 13

  • Nomenclature

    For the one-dimensional mesh, we define

    Symbol Meaning

    u(x, t) Analytical solution (true solution).

    u(xi, tk) Analytical solution evaluated at x = xi, t = tk.

    uki Approximate numerical solution at x = xi, t = tk.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 14

  • First order forward finite difference

    Use a Taylor series expansion about the point xi

    u(xi + δx) = u(xi) + δx∂u

    ∂x

    ∣∣∣∣xi

    +δx2

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    +δx3

    3!

    ∂3u

    ∂x3

    ∣∣∣∣∣xi

    + · · ·

    where δx is a small distance, and δx2 and δx3 are shorthand for (δx)2 and (δx)3.

    Solve for ∂u/∂x|xi

    ∂u

    ∂x

    ∣∣∣∣xi

    =u(xi + δx)− u(xi)

    δx−δx

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    −δx2

    3!

    ∂3u

    ∂x3

    ∣∣∣∣∣xi

    + · · ·

    In the limit as δx→ 0 the coefficients of the higher order derivative terms vanish, andthe first term on the right hand side becomes equal to the derivative on the left hand side.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 15

  • First order forward finite difference

    Replace δx with ∆x = xi+1 − xi, and substitute ui ≈ u(xi)

    ∂u

    ∂x

    ∣∣∣∣xi

    ≈ui+1 − ui

    ∆x−

    ∆x

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    −∆x2

    3!

    ∂3u

    ∂x3

    ∣∣∣∣∣xi

    + · · · (10)

    The mean value theorem can be used to replace the higher order derivatives (exactly)

    ∆x

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    +(∆x)2

    3!

    ∂3u

    ∂x3

    ∣∣∣∣∣xi

    + · · · =∆x

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣ξ

    where xi ≤ ξ ≤ xi+1. Thus

    ∂u

    ∂x

    ∣∣∣∣xi

    ≈ui+1 − ui

    ∆x+

    ∆x

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣ξ

    or∂u

    ∂x

    ∣∣∣∣xi

    −ui+1 − ui

    ∆x≈

    ∆x

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣ξ

    (11)

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 16

  • First order forward finite difference

    In general, ξ is not known. Since ∆x is the only parameter under the user’s control that

    determines the error, the truncation error is simply written

    ∆x

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣ξ

    = O(∆x)

    This expression means that the left hand side is bounded by a product of an unknown

    constant times ∆x. Although the expression does not give us the exact magnitude of

    (∆x/2) (∂2u/∂x2)∣∣ξ, it indicates how quickly that term approaches zero as ∆x is

    reduced.

    Recall that the point of Big O notation is to look at the exponent of the truncation errorterm.

    We conclude that the error in the first order forward difference formula is linear in the

    mesh spacing.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 17

  • First order forward finite difference

    Using big O notation, Equation (11) is written

    ∂u

    ∂x

    ∣∣∣∣xi

    =ui+1 − ui

    ∆x+O(∆x). (12)

    Equation (12) is called the forward difference formula for ∂u/∂x|xi because it involvesnodes xi and xi+1.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 18

  • First order backward finite difference

    Repeat the preceding analysis with a Taylor series expansion for u(xi − δx)

    ui−1 = ui −∆x∂u

    ∂x

    ∣∣∣∣xi

    +∆x2

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    −(∆x)3

    3!

    ∂3u

    ∂x3

    ∣∣∣∣∣xi

    + · · · (13)

    Solve for ∂u/∂x|xi to get

    ∂u

    ∂x

    ∣∣∣∣xi

    =ui − ui−1

    ∆x+

    ∆x

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    −(∆x)2

    3!

    ∂3u

    ∂x3

    ∣∣∣∣∣xi

    + · · ·

    or

    ∂u

    ∂x

    ∣∣∣∣xi

    =ui − ui−1

    ∆x+O(∆x). (14)

    This is called the backward difference formula because it involves the values of u at xiand xi−1.

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 19

  • First order central difference

    Combine Taylor Series expansions for ui+1 and ui−1 to get

    ∂u

    ∂x

    ∣∣∣∣xi

    =ui+1 − ui−1

    2∆x+O(∆x2) (15)

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 20

  • Summary of first order difference approximations

    Forward:∂u

    ∂x

    ∣∣∣∣xi

    =ui+1 − ui

    ∆x+O(∆x)

    Backward:∂u

    ∂x

    ∣∣∣∣xi

    =ui − ui−1

    ∆x+O(∆x)

    Central:∂u

    ∂x

    ∣∣∣∣xi

    =ui+1 − ui−1

    2∆x+O(∆x2)

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 21

  • Second Order Central Difference

    The Taylor series expansion for ui+1 is

    ui+1 = ui + ∆x∂u

    ∂x

    ∣∣∣∣xi

    +∆x2

    2

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    +(∆x)3

    3!

    ∂3u

    ∂x3

    ∣∣∣∣∣xi

    + · · · (16)

    Adding Equation (13) and Equation (16) yields

    ui+1 + ui−1 = 2ui + ∆x2 ∂

    2u

    ∂x2

    ∣∣∣∣∣xi

    +2∆x4

    4!

    ∂4u

    ∂x4

    ∣∣∣∣∣xi

    + · · ·

    Notice that all odd derivatives cancel because of the alternating signs of terms on the

    right hand side of Equation (13).

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 22

  • Second Order Central Difference

    Solving for∂2u

    ∂x2

    ∣∣∣∣∣xi

    gives

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    =ui−1 − 2ui + ui+1

    ∆x2+

    ∆x2

    6

    ∂4u

    ∂x4

    ∣∣∣∣∣xi

    + · · ·

    or

    ∂2u

    ∂x2

    ∣∣∣∣∣xi

    =ui−1 − 2ui + ui+1

    ∆x2+O(∆x2). (17)

    ME 448/548: Introduction to Finite Difference Approximation of the Heat Equation page 23