Number Mathematics

of 19

  • date post

    28-Feb-2018
  • Category

    Documents

  • view

    213
  • download

    0

Embed Size (px)

Transcript of Number Mathematics

  • 7/25/2019 Number Mathematics

    1/19

    Notes on Finite-Difference Methods forAME 301: Differential Equations, Vibrations and Controls

    Mihir Sen

    Department of Aerospace and Mechanical Engineering

    University of Notre DameNotre Dame, IN 46556, U.S.A.

    November 28, 2005

    1 Introduction

    It is common to use numerical methods to compute the solution of nonlinear or otherwise complicatedordinary differential equations. For partial differential equations it is necessitated also by a geometry forwhich an analytical solution may not exist. There are many numerical methods for solving differentialequations, both ordinary and partial, but here we will look only at the finite-difference method in somedetail.

    Since numerical solutions are usually calculated on digital computers, the use of numerical methodsrequire knowledge and efficient use of modern programming languages and techniques. There are a varietyof languages currently in use; some are compilable, such as Fortran, C and C++, while there are otherslike Matlab which are more suitable for graphing and use in conjunction with toolboxes. Matlab is usually

    sufficient to illustrate the use of a numerical method and hence will be shown below, but readers can convertthem to a compilable language for faster runs.

    2 Numerical differentiation

    2.1 Functions of a single variable

    Letu = u(x), so that on expanding in Taylor series around a pointx = x, we have

    u(x+ x) = u(x) +u(x)x +1

    2u(x)x2 +O(x3), (1)

    u(x x) = u(x) u(x)x +1

    2u(x)x2 +O(x3). (2)

    Terms which have xn are indicated simply as of the order of xn and written asO(xn).At this point we will change the notation somewhat to make it easier to write computer programs.

    Thus, referring to Fig. 1, we will write x(i 1) = x x, x(i) = x and x(i+ 1) = x+ x with x =x(i + 1)x(i) =x(i)x(i1) being the step size. Similarly, we will write u(x + x) = u(i +1),u(x) = u(i)andu(x x) = u(i 1). The indices will indicate the location where the quantity is being evaluated.

    From Eq. (1) we can obtain the forward-difference approximation

    u(i) = 1

    x[u(i+ 1) u(i)] +O(x2). (3)

    1

  • 7/25/2019 Number Mathematics

    2/19

    Similarly from Eq. (2), we have the backward-difference formula

    u(i) = 1

    x[u(i) u(i 1)] +O(x2). (4)

    Subtracting Eq. (2) from Eq. (1) gives the central difference

    u(i) = 1

    2x[u(i+ 1) u(i 1)] +O(x3)). (5)

    On adding Eqs. (1) and (2), we get the second derivative

    u(i) = 1

    x2[u(i+ 1) 2u(i) +u(i 1)] +O(x3). (6)

    u(x)

    xx(i 1) x(i) x(i+ 1)

    u(i 1) u(i) u(i+ 1)

    Figure 1: Function of a single variable.

    ExampleFind numerically the derivatives of the function u = sin(2x). Fig. 2 shows a comparison between the numerical and

    analytical values for the first and second derivative. It is seen that the smaller values of x gives better results. Thenumerical codes used to produce the results are similar to the ones shown in Section 7.2.

    2.2 Functions of two variables

    Let u = u(x, y) where (x, y) belongs to some two-dimensional area. The region may be divided up bya grid according to the coordinate system used. Here we will work with a Cartesian grid as shown inFig. 3 and use two indices to indicate where a quantity is being evaluated, the first for the x-directionand the second for the y. The mesh size is then x = x(i + 1, j) x(i, j) = x(i, j) x(i 1, j) andy= x(i, j+ 1) x(i, j) = x(i, j) x(i, j 1).

    Expansions in Taylor series around a point (i, j) give

    u(i+ 1, j) = u(i, j) +ux(i, j)x+1

    2uxx(i, j)x

    2 +O(x3), (7)

    u(i 1, j) = u(i, j) ux(i, j)x+1

    2uxx(i, j)x

    2 +O(x3), (8)

    u(i, j+ 1) = u(i, j) +uy(i, j)y+1

    2uyy(i, j)y

    2 +O(y3), (9)

    u(i, j 1) = u(i, j) uy(i, j)y+1

    2uyy(i, j)y

    2 +O(y3). (10)

    2

  • 7/25/2019 Number Mathematics

    3/19

    0 0.2 0.4 0.6 0.8 110

    5

    0

    5

    10

    0 0.2 0.4 0.6 0.8 140

    20

    0

    20

    40

    x

    x

    u(x)

    u(x)

    (a)

    (b)

    Figure 2: (a) First and (b) second derivatives ofu = sin(2x) using central differences; x = 0.2 (), 0.1(), 0.02 (); continuous line is analytical.

    These can be manipulated to give the finite-difference approximations. For example, the central-differenceapproximations for the two first-order partial derivatives are

    ux(i, j) = 1

    2x[u(i+ 1, j) u(i 1, j)] +O(x3), (11)

    uy(i, j) = 1

    2y[u(i, j+ 1) u(i, j 1)] +O(y3). (12)

    Similarly the approximations for the second-order partial derivatives are

    uxx(i, j) = 1x2 [u(i+ 1, j) 2u(i, j) +u(i 1, j)] +O(x3), (13)

    uyy(i, j) = 1

    y2[u(i, j+ 1) 2u(i, j) +u(i, j 1)] +O(y3). (14)

    The Laplacian operator defined as

    2u= uxx+uyy (15)

    can be approximated by

    2u =

    1

    x2[u(i+ 1, j) 2u(i, j) +u(i 1, j)]

    + 1

    y2[u(i, j+ 1) 2u(i, j) +u(i, j 1)] +O(max(x3, y3)). (16)

    If for simplicity we take x= y= h, then this becomes

    2u=

    1

    h2[u(i+ 1, j) +u(i 1, j) +u(i, j+ 1) +u(i, j 1) 4u(i, j)] +O(h3). (17)

    ExampleFind the Laplacian of the function u = sin[x(1 x)y(1 y)] in 0 x 1 and 0 y 1 shown in Fig. 4.The result obtained using Eq. (17) is shown in Fig. 5. The code in Section 7.3 was used to generate Figs. 4 and 5.

    3

  • 7/25/2019 Number Mathematics

    4/19

    (i 1, j) (i, j) (i+ 1, j)

    (i, j 1)

    (i, j+ 1)

    Figure 3: Cartesian mesh for two independent variables.

    0

    0.2

    0.4

    0.6

    0.8

    1

    0

    0.5

    1

    0

    0.05

    0.1

    0.15

    0.2

    xy

    u(x,y)

    Figure 4: Function u = sin[x(1 x)y(1 y)] shown using a 2020 mesh.

    3 Ordinary differential equations

    The finite differences defined in Section 2.1 can be introduced in an ordinary differential equation to reduceit to algebraic form. At this point there are an infinite number of solutions for the values of the unknown.To make the solution unique, boundary conditions have to be introduced. Boundary conditions can be ofthe following forms. Each one of them can be reduced to algebraic form and used.

    3.1 Dirichlet

    In this case the value of the function is prescribed at the boundary. For example, one can have

    u(1) =a (18)

    where i = 1 is the node at the end where the value ofu is given as a. u(1) is then no longer an unknownquantity.

    4

  • 7/25/2019 Number Mathematics

    5/19

    0

    0.2

    0.4

    0.6

    0.8

    1

    0

    0.5

    1

    0

    1

    2

    3

    4

    xy

    2u

    Figure 5: Laplacian ofu = sin[x(1 x)y(1 y)] (for clarity multiplied by 1);h = 0.1.

    3.2 Neumann

    Here the derivative of the function u is known at the boundary, so that a finite-difference approximationhas to be used. As an example, ifu =a at the boundary, we can approximate the condition by

    u(2) u(1)

    x =a (19)

    wherei = 1 lies on the boundary and i = 2 is the node next to it.

    3.3 Robin

    Sometimes a linear combination of the function u and its derivative u is known at the boundary, i.e.au+bu =c. Once again an algebraic form can be obtained using finite differences, so that

    au(1) +b u(2) u(1)

    x =c (20)

    wherei = 1 andi = 2 are again the nodes on the boundary and next to it.

    ExampleSolve

    u +u= 0, (21)

    with the Dirichlet boundary conditions u(0) =a and u(1) =b.Divide the interval [0, 1] into N sub-intervals, each of length x= 1/N, and number the nodes as i = 1, 2, . . . , N + 1

    so that x(i) = (i 1)x. Using Eq. (6) in (21), we get

    u(i+ 1) (2 x2)u(i) +u(i 1) = 0, (22)

    at i = 2, 3, . . . , N . Applying the boundary conditions u(1) = a and u(N+ 1) = b, we get N 1 algebraic equations in

    the unknowns u(2), u(3), . . . , u(N). In matrix form these are

    c 1 0 . . . 01 c 1 0 . . .0 1 c 1 . . ....

    ......

    ......

    0 0 . . . 1 c

    u(2)u(3)

    ...u(N 1)

    u(N)

    =

    a0...0b

    , (23)

    where c = 2 x2. Methods to solve Eq. (23) are in Section 5, and the solution is shown in Fig. 6.

    5

  • 7/25/2019 Number Mathematics

    6/19

    0 0.2 0.4 0.6 0.8 10

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0.8

    0.9

    1

    x

    u(x)

    Figure 6: Dots indicate the numerical solution of Eq. (21) with x= 0.01, and continuous line the analytical

    solutionu = sin x/ sin(1).

    4 Partial differential equations

    Finite differences described in Section 2.2 can be applied to PDEs to reduce them to algebraic equations.Boundary conditions may be of the types indicated in Sections 3.1, 3.2 and 3.3, the only difference being thatderivatives are in a direction normal to the boundary, and they can be approximated in a similar fashion.For the three conditions, one can, for example, write

    u(n) = a (24)u(n) u(n+ 1)

    n =a (25)

    au(1) +b u(n) u(n+ 1)

    n =c (26)

    respectively, where the indices n and n + 1 refer to points at the boundary and a distance n away from itin a direction normal to the boundary.

    Numerical solutions shouldconvergeas the mesh becomes finer. Furthermore, the result obtained shouldbe consistent in that the solution obtained should correspond to that of the original PDE. The numericalmethod chosen should also be stable.

    4.1 Heat equation in one spatial variable

    The heat equation is1

    uxx= ut, (27)

    with suitable boundary conditions. An explicit marching method can be used to calculate the values ofu(x, t) at time from a previous instant using the finite-d