Supriya Ghosh (Editor)

Five point stencil

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Five-point stencil

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. It is an example for numerical differentiation.

Contents

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 2 h , x h , x , x + h , x + 2 h } .

First derivative

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

f ( x ) f ( x + 2 h ) + 8 f ( x + h ) 8 f ( x h ) + f ( x 2 h ) 12 h

Obtaining the formula

This formula can be obtained by writing out the four Taylor series of ƒ(x ± h) and ƒ(x ± 2h) up to terms of h 3 (or up to terms of h 5 to get an error estimation as well) and solving this system of four equations to get ƒ ′(x). Actually, we have at points x + h and x − h:

f ( x ± h ) = f ( x ) ± h f ( x ) + h 2 2 f ( x ) ± h 3 6 f ( 3 ) ( x ) + O 1 ± ( h 4 ) . ( E 1 ± ) .

Evaluating (E 1+) − (E 1−) gives us

f ( x + h ) f ( x h ) = 2 h f ( x ) + h 3 3 f ( 3 ) ( x ) + O 1 ( h 4 ) . ( E 1 ) .

Note that the residual term O1(h 4) should be of the order of h 5 instead of h 4 because if the terms of h 4 had been written out in (E 1+) and (E 1−), it can be seen that they would have canceled each other out by ƒ(x + h) − ƒ(x − h). 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 ± 2 h ) = f ( x ) ± 2 h f ( x ) + 4 h 2 2 ! f ( x ) ± 8 h 3 3 ! f ( 3 ) ( x ) + O 2 ± ( h 4 ) . ( E 2 ± )

and ( E 2 + ) ( E 2 ) gives us

f ( x + 2 h ) f ( x 2 h ) = 4 h f ( x ) + 8 h 3 3 f ( 3 ) ( x ) + O 2 ( h 4 ) . ( E 2 ) .

In order to eliminate the terms of ƒ (3)(x), calculate 8 × (E1) − (E2)

8 f ( x + h ) 8 f ( x h ) f ( x + 2 h ) + f ( x 2 h ) = 12 h f ( x ) + O ( h 4 )

thus giving the formula as above. Note: the coefficients of f in this formula, (8, -8,-1,1), represent a specific example of the more general Savitzky-Golay filter

Estimated error

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

f ( x + 2 h ) + 8 f ( x + h ) 8 f ( x h ) + f ( x 2 h ) 12 h = f ( x ) 1 30 f ( 5 ) ( x ) h 4 + O ( h 5 )

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.

Higher derivatives

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

f ( x ) f ( x + 2 h ) + 16 f ( x + h ) 30 f ( x ) + 16 f ( x h ) f ( x 2 h ) 12 h 2 f ( 3 ) ( x ) f ( x + 2 h ) 2 f ( x + h ) + 2 f ( x h ) f ( x 2 h ) 2 h 3 f ( 4 ) ( x ) f ( x + 2 h ) 4 f ( x + h ) + 6 f ( x ) 4 f ( x h ) + f ( x 2 h ) h 4

Estimated errors

The errors in these approximations are O(h 4), O(h 2) and O(h 2) respectively.

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

j ( ξ ) = i = 0 , i j k ξ x i x j x i ,

where the interpolation points are

x 0 = x 2 h , x 1 = x h , x 2 = x , x 3 = x + h , x 4 = x + 2 h .

Then, the quartic polynomial p 4 ( x ) interpolating ƒ(x) at these five points is

p 4 ( x ) = j = 0 4 f ( x j ) j ( x )

and its derivative is

p 4 ( x ) = j = 0 4 f ( x j ) j ( x ) .

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

f ( x 2 ) = 0 ( x 2 ) f ( x 0 ) + 1 ( x 2 ) f ( x 1 ) + 2 ( x 2 ) f ( x 2 ) + 3 ( x 2 ) f ( x 3 ) + 4 ( x 2 ) f ( x 4 ) + O ( h 4 )

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.

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 (xy) in the grid is

{ ( x h , y ) , ( x , y ) , ( x + h , y ) , ( x , y h ) , ( x , y + h ) } ,

forming a pattern that is also called a quincunx. This stencil is often used to approximate the Laplacian of a function of two variables:

Δ f ( x , y ) f ( x h , y ) + f ( x + h , y ) + f ( x , y h ) + f ( x , y + h ) 4 f ( x , y ) h 2 .

The error in this approximation is O(h 2), which may be explained as follows:

From the 3 point stencils for the second derivative of a function with respect to x and y:

2 f x 2 = f ( x + Δ x , y ) + f ( x Δ x , y ) 2 f ( x , y ) Δ x 2 2 f ( 4 ) ( x , y ) 4 ! Δ x 2 +

2 f y 2 = f ( x , y + Δ y ) + f ( x , y Δ y ) 2 f ( x , y ) Δ y 2 2 f ( 4 ) ( x , y ) 4 ! Δ y 2 +

If we assume Δ x = Δ y = h :

2 f = 2 f x 2 + 2 f y 2 = f ( x + h , y ) + f ( x h , y ) + f ( x , y + h ) + f ( x , y h ) 4 f ( x , y ) h 2 4 f ( 4 ) ( x , y ) 4 ! h 2 + = f ( x + h , y ) + f ( x h , y ) + f ( x , y + h ) + f ( x , y h ) 4 f ( x , y ) h 2 + O ( h 2 )

References

Five-point stencil Wikipedia