Five-point stencil

From Wikipedia, the free encyclopedia

An illustration of the five-point stencil in one and two dimensions (top, and bottom, respectively).
An illustration of the five-point stencil in one and two dimensions (top, and bottom, respectively).

In numerical analysis, given a square grid in one or two dimensions, the five-point stencil of a point in the grid is made up of the point itself together with its four "neighbors". It is used to write finite difference approximations to derivatives at grid points.

Contents

[edit] One dimension

In one dimension, if the spacing between points in the grid is h, then the five-point stencil of a point x in the grid is

\ \{x-2h, x-h, x, x+h, x+2h\}.

[edit] First derivative

The first derivative of a function f of a real variable at a point x can be approximated using a five-point stencil as

f'(x) \approx \frac{-f(x+2 h)+8 f(x+h)-8 f(x-h)+f(x-2h)}{12 h}

[edit] Obtaining the formula

This formula can be obtained by writing out the four Taylor series of f(x \pm h) and f(x \pm 2h) up to terms of h3 (or up to terms of h5 to get an error estimation as well) and solving this system of four equations to get f'(x). Actually, we have at points (x + h) and (xh)

f(x \pm h) = f(x) \pm h f'(x) + \frac{h^2}{2}f''(x) \pm \frac{h^3}{6} f^{(3)}(x) + O_{1\pm}(h^4). \qquad (E_{1\pm})

Evaluating (E1 + ) − (E1 − ) gives us

f(x+h) - f(x-h) = 2hf'(x) + \frac{h^3}{3}f^{(3)}(x) + O_1(h^4). \qquad (E_1)

Note that the residual term O1(h4) should be of the order of h5 instead of h4 because if the terms of h4 had been written out in (E1 + ) and (E1 − ), it can be seen that they would have canceled each other out by f(x + h) − f(xh). But for this calculation, it is left like that since the order of error estimation is not treated here (cf below).

Similarly, we have

f(x \pm 2h) = f(x) \pm 2h f'(x) + 2h^2 f''(x) \pm \frac{4h^3}{3} f^{(3)}(x) + O_{2\pm}(h^4). \qquad (E_{2\pm})

(E2 + ) − (E2 − ) gives us

f(x+2h) - f(x-2h) = 4hf'(x) + \frac{8h^3}{3}f^{(3)}(x) + O_2(h^4). \qquad (E_2)

In order to eliminate the terms of f(3)(x), calculate 8 \times (E_1) - (E_2)

8f(x+h) - 8f(x-h) - f(x+2h) + f(x-2h) = 12h f'(x) + O(h^4) \,

thus giving the formula as above.

[edit] Estimated error

The error in this approximation is of order h4. That can be seen from the expansion

 \frac{-f(x+2 h)+8 f(x+h)-8 f(x-h)+f(x-2h)}{12 h}=f'(x)-\frac{1}{30} f^{(5)}(x) h^4+O(h^5) [1]

which can be obtained by expanding the left-hand side in a Taylor series. Alternatively, apply Richardson extrapolation to the central difference approximation to f'(x) on grids with spacing 2h and h.

[edit] Higher derivatives

The centered difference formulas for five-point stencils approximating second, third, and fourth derivatives are

 \begin{align}
  f''(x)     &\approx \frac{-f(x+2 h)+16 f(x+h)-30 f(x) + 16 f(x-h) - f(x-2h)}{12 h^2}, \\
  f^{(3)}(x) &\approx \frac{f(x+2 h)-2 f(x+h) + 2 f(x-h) - f(x-2h)}{2 h^3}, \\
  f^{(4)}(x) &\approx \frac{f(x+2 h)-4 f(x+h)+6 f(x) - 4 f(x-h) + f(x-2h)}{h^4}.
\end{align}

[edit] Estimated errors

The errors in these approximations are O(h4), O(h2) and O(h2) respectively.[1]

[edit] Relationship to Lagrange interpolating polynomials

As an alternative to deriving the finite difference weights from the Taylor series, they may be obtained by differentiating the Lagrange polynomials

\ell_j(\xi) = \prod_{i=0,\, i\neq j}^{k} \frac{\xi-x_i}{x_j-x_i},

where the interpolation points are

 \begin{align}
x_0=x-2h,\quad x_1=x-h,\quad x_2=x,\quad x_3=x+h,\quad x_4=x+2h.
\end{align}

Then, the quartic polynomial p4(x) interpolating f(x) at these five points is

 \begin{align}
p_4(x) = \sum\limits_{j=0}^4 f(x_j) \ell_j(x)
\end{align}

and its derivative is

 \begin{align}
 p_4'(x) = \sum\limits_{j=0}^4 f(x_j) \ell'_j(x).
\end{align}

So, the finite difference approximation of f'(x) at the middle point x = x2 is

 \begin{align}
f'(x_2) = \ell_0'(x_2) f(x_0) + \ell_1'(x_2) f(x_1) + \ell_2'(x_2) f(x_2)  + \ell_3'(x_2) f(x_3) + \ell_4'(x_2) f(x_4) + O(h^4)  \end{align}

Evaluating the derivatives of the five Lagrange polynomials at x = x2 gives the same weights as above. This method can be more flexible as the extension to a non-uniform grid is quite straightforward.

[edit] Two dimensions

In two dimensions, if for example the size of the squares in the grid is h by h, the five point stencil of a point (x,y) in the grid is

\{(x-h, y), (x, y), (x+h, y), (x, y-h), (x, y+h)\}. \,

This stencil is often used to approximate the Laplacian of a function of two variables:

 \Delta f(x,y) \approx \frac{f(x-h,y) + f(x+h,y) + f(x,y-h) + f(x,y+h) - 4f(x,y)}{h^2}.

The error in this approximation is O(h2).[2]

[edit] Notes

  1. ^ a b Abramowitz & Stegun, Table 25.2
  2. ^ Abramowitz & Stegun, 25.3.30

[edit] References