Finite Difference

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

4. BOUNDARY-VALUE PROBLEMS 4.1 Introduction 4.2 Finite-difference forms for dy/dx and d2y/dx2 4.

3 Two-point boundary-value problems 4.4 Boundary conditions on dy/dx 4.5 Non-linear equations 4.6 Shooting methods

SPRING 2005

4.1 Introduction Hitherto we have looked at initial-value problems all boundary conditions specified at the same point. These are solved by forward marching. Boundary-value problems have boundary conditions at more than one point. These can be solved by finite-difference methods. The solution is found at all points simultaneously. In the second half of the course we shall find that analogous methods of both types exist for partial differential equations. In mechanics, the commonest type of boundary-value problems are 2nd-order differential equations with either a value or a derivative specified at the end points; i.e. d2 y dy dy given at x A and x B ) a 2 + b + cy = s ( y or dx dx dx 4.2 Finite-Difference Forms for dy/dx and d2y/dx2 Finite differences are discrete approximations to derivatives; e.g: y y i 1 dy i +1 dx i 2h

y yi+1

d2 y dx 2

y 2 y i + y i 1 i +1 h2

yi-1 x i-1

yi h xi x i+1 x

The finite-difference method is to substitute these expressions into the governing differential equation to obtain an algebraic system consisting of a set of simultaneous (linear equations) for y0, y1, ... , yN.
y

This set of equations is solved simultaneously: there is no forward-marching procedure as in initial-value problems.
yi-1 h x0 x1 xi-1 xi yi

yi+1

h xi+1 xN-1 xN

Computational Mechanics

4-1

David Apsley

Derivation From Taylor Series Taylor-series expansions about a point x yield: 1 1 y ( x + h) = y ( x) + hy ( x) + h 2 y ( x) + h 3 y ( x) + 3! 2! 1 1 y ( x h) = y ( x) hy ( x) + h 2 y ( x) h 3 y ( x) + 2! 3! Subtraction gives 3 y ( x + h) y ( x h) = 2hy ( x) + 1 3 h y ( x) + y ( x + h) y ( x h) = y ( x) + O(h 2 ) 2h Hence, y y i 1 dy = i +1 + O(h 2 ) dx i 2h error
Alternatively, adding gives y ( x + h) + y ( x h) = 2 y ( x) + h 2 y ( x) + O(h 4 ) y ( x + h ) 2 y ( x ) + y ( x h) = y ( x) + O(h 2 ) 2 h Hence, y 2 y i + y i 1 d2 y = i +1 + O(h 2 ) 2 2 h dx i error

1 4 iv h y ( x) + 4! 1 4 iv h y ( x) + 4!

For both approximations, the local error term is proportional to the square of the step size h. Since all equations are solved simultaneously, there is no accumulation of errors and the error at any point is, therefore, also proportional to h2. Thus, the finite-difference approximations for dy/dx and d2y/dx2 are second-order accurate.

Note: (i) More points can be used to obtain higher-order accuracy; for example, by expanding y(x2h) and y(x+2h) one may obtain the fourth-order scheme y i + 2 + 8 y i +1 8 y i 1 + y i 2 dy = + O(h 4 ) dx i 12h (ii) The above discretisations are centrally-differenced; i.e. symmetric about the central node i. In fact, it is not necessary to use central differencing. For example, 3 y 4 y i 1 + y i 2 dy = i + O(h 2 ) dx i 2h In computational fluid dynamics, for example, there are good physical reasons for using one-sided (upwind) differencing because information propagates predominantly in the direction of fluid flow.

Computational Mechanics

4-2

David Apsley

4.3 Two-Point Boundary-Value Problems

Example. The excess temperature in a thin rod free to exchange heat with its surroundings is given by d dT )= T (k dx dx If k = 100 W m1 K1, = 2500 W m1 K1 and the rod is 1 m long, with its end temperatures fixed at 68 C and 2 C above ambient, use a finite-difference method with step size 0.2 m to find the temperature distribution in the rod.

T0 =68 T1 x=0 T2 h=0.2 T3 T4

T5 =2

x=1

Computational Mechanics

4-3

David Apsley

The general second-order linear ODE is d2 y dy a 2 + b + cy = s dx dx where a, b, c and s may be functions of x but not y. Substituting in the expressions for the derivatives, one obtains y 2 y i + y i 1 y y i 1 ai i +1 + ci y i = s i + bi i +1 2 2h h which can be rearranged as 2a a i bi a i bi ( 2 ) y i 1 + (ci 2i ) y i + ( 2 + ) y i +1 = si 2h 2h h h h or, simply, Ai yi 1 + Bi yi + Ci y i +1 = si As a matrix equation this looks like:
B1 A2 C1 B2 A3 0 y1 y2 y3 s1 A1 y 0 s2 s3

C2 B3

0 C3 AN 2

BN 2 C N 2 y N 2 s N 2 AN 1 B N 1 y N 1 s N 1 C N 1 y N with all but the central three diagonals in the left hand matrix being zero (a tridiagonal matrix). For small problems this is easily solved by Gaussian elimination. For larger problems the solution is easily automated by a procedure known as the tridiagonal matrix algorithm (TDMA) or Thomas algorithm. This is almost invariably supplied as a subroutine, but it basically works as follows: Since y0 is known, the first equation gives y1 in terms of y2. Hence, the second equation gives y2 in terms of y3. The process continues until the last equation gives yN1 in terms of yN. But yN is known, hence yN1 is found. The remaining sequence yN2, yN3, ... , y1 is found by back-substitution.

Computational Mechanics

4-4

David Apsley

4.4 Boundary conditions on dy/dx

Suppose the gradient =

dy is given at x = 0, rather than y itself. dx

Method 1: first-order finite difference from near-boundary nodes. y y0 dy = = 1 + O ( h) dx 0 h Substitute y 0 = y1 h in the first difference equation: A1 y 0 + B1 y1 + C1 y 2 = s1

y y1 y0 h

x ( B1 + A1 ) y1 + C1 y 2 = s1 + hA1 This method requires only minimal change in the calculation matrix. However, since the boundary condition is O(h), the whole solution is O(h).
Method 2: introduce a ghost point y-1. y y 1 dy = 1 + O(h 2 ) = dx 0 2h Substitute y 1 = y1 2 h in the new first difference equation: A0 y 1 + B0 y 0 + C 0 y1 = s 0

y y 1
ghost point

B0 y 0 + (C 0 + A0 ) y1 = s 0 + 2 hA0 There is one extra row in the matrix equation, but the boundary value is found automatically and the global solution is O(h2).

y -1

y 0

specified gradient

Computational Mechanics

4-5

David Apsley

4.5 Non-Linear Equations

If the governing equation is non-linear then a direct solution (with matrix inversion by the tridiagonal matrix algorithm) is not possible. Instead an iterative technique is used. The secondorder equation d2 y dy = f ( x, y , ) 2 dx dx is formally discretised as yi +1 2 y i + yi 1 y yi 1 = f ( xi , yi , i +1 ) 2 h 2h and solved by the iterative procedure n +1) n +1) (n) n) 2 yi( n +1) + yi( yi(+ yi(+ 1 y i 1 1 1 ( , , ), = n = 1 , 2 , ... f x y i i h2 2h with each iteration inverted by the tri-diagonal matrix algorithm and the RHS treated as constant for that iteration. However, the method is not guaranteed to work without a good initial guess for {yi} and sometimes under-relaxation (adopting only a fraction of the change y(n) y(n+1)) see later.
4.6 Shooting Methods

Shooting methods are an attempt to solve boundary-value problems using simpler initialvalue, forward-marching techniques. The basic idea is to shoot at the solution with two different starting parameters and extrapolate or interpolate from the answers to get a better guess. As with artillery shells this may require several iterations to get right! For example, to solve the second-order ODE with boundary values fixed: d2 y dy y A and y B given = f ( x, y, ), 2 dx dx A forward-marching solution requires both y and dy/dx at xA. We have to guess the latter and solve with successive guesses so as to hit y(xB) = yB. Once we have solutions with two initial guesses we can then improve on our next guess by interpolation or extrapolation.
yB
ved impro

ss 2 gue

yA

guess 1

Solve the initial value problems with yA and successive xA guessed values, 1 and 2 say, for the initial gradient (dy/dx)A. Comparing the predicted values y1(xB) and y2(xB) at xB with the given boundary value yB obtain a better estimate of the actual gradient at xA by interpolation. Continue this iterative process until the solution hits the second boundary value exactly.

xB

Computational Mechanics

4-6

David Apsley

You might also like