Five-point stencil
{{Short description|A point and its four nearest neighbors}}
File:Five point stencil illustration.png
In numerical analysis, given a square grid in one or two dimensions, the five-point stencil of a point in the grid is a stencil 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.
In 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
:
=1D 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:{{cite book |title=Numerical Analysis |last=Sauer |first=Timothy |publisher=Pearson |year=2012 |isbn=978-0-321-78367-7 |pages=250}}
:
The center point f(x) itself is not involved, only the four neighboring points.
==Derivation==
This formula can be obtained by writing out the four Taylor series of and at the point , up to terms of h3 (or up to terms of h5 to get an error estimation as well), evaluating each series at and respectively (to get everything in common terms of ), and solving this system of four equations to get f ′(x). Actually, we have at points x + h and x − h:
:
Evaluating gives us
:
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 {{math|f(x + h) − f(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
:
and gives us
:
In order to eliminate the terms of ƒ(3)(x), calculate 8 × (E1) − (E2)
:
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.
==Error estimate==
The error in this approximation is of order h 4. That can be seen from the expansionAbramowitz & Stegun, Table 25.2
:
which can be obtained by expanding the left-hand side in a Taylor series. Alternatively, apply Richardson extrapolation to the central difference approximation to on grids with spacing 2h and h.
=1D higher-order derivatives=
The centered difference formulas for five-point stencils approximating second, third, and fourth derivatives are
:
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}
\\[1ex]
f^{(3)}(x) &\approx \frac{f(x+2 h)-2 f(x+h) + 2 f(x-h) - f(x-2h)}{2 h^3}
\\[1ex]
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}
The errors in these approximations are O(h{{i sup|4}}), O(h{{i sup|2}}) and O(h{{i sup|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
:
where the interpolation points are
:
x_0 = x-2h,\quad x_1 = x-h,\quad x_2 = x,\quad x_3 = x+h,\quad x_4 = x+2h.
Then, the quartic polynomial interpolating {{math|f(x)}} at these five points is
:
and its derivative is
:
So, the finite difference approximation of {{math|f ′(x)}} at the middle point {{math|1=x = x2}} is
:
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)
Evaluating the derivatives of the five Lagrange polynomials at {{math|1=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.
In 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
:
forming a pattern that is also called a quincunx. This stencil is often used to approximate the Laplacian of a function of two variables:
:
The error in this approximation is O(h 2),Abramowitz & Stegun, 25.3.30 which may be explained as follows:
From the 3 point stencils for the second derivative of a function with respect to x and y:
:
\frac{\partial ^2 f}{\partial x^2} &=
\frac{f\left(x + \Delta x,y\right) + f\left(x - \Delta x,y\right) - 2f(x,y)}{\Delta x^2} - 2\frac{f^{(4)}(x,y)}{4!} \Delta x^2 + \cdots \\[1ex]
\frac{\partial ^2 f}{\partial y^2} &=
\frac{f\left(x, y + \Delta y\right) + f\left(x, y - \Delta y\right) - 2f(x,y)}{\Delta y^2} - 2\frac{f^{(4)}(x,y)}{4!} \Delta y^2 + \cdots
\end{align}
If we assume :
:
\nabla^2 f &= \frac{\partial ^2 f}{\partial x^2} + \frac{\partial ^2 f}{\partial y^2} \\[1ex]
&= \frac{f\left(x + h,y\right) + f\left(x - h,y\right) + f\left(x, y + h\right) + f\left(x, y - h\right) - 4f(x,y)}{h^2} - 4 \frac{f^{(4)}(x,y)}{4!}h^2 + \cdots \\[1ex]
&= \frac{f\left(x + h,y\right) + f\left(x - h,y\right) + f\left(x, y + h\right) + f\left(x, y - h\right) - 4f(x,y)}{h^2} + O\left(h^2\right)\\
\end{align}
See also
- {{annotated link|Finite difference coefficient}}
- {{annotated link|Iterative Stencil Loops}}
- Nine-point stencil
- Stencil (numerical analysis)
- {{annotated link|Stencil jumping}}
References
{{Reflist}}
- {{citation |first1=Milton |last1=Abramowitz |author1-link=Milton Abramowitz |first2=Irene A. |last2=Stegun |author2-link=Irene Stegun |year=1970 |title=Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables |publisher=Dover}}. Ninth printing. Table 25.2.