Bellaard.com

# Four-point stencil for 3D gradient

By: Gijs Bellaard

Suppose you have some sufficiently smooth black-box function $$f: \mathbb{R}^3 \rightarrow \mathbb{R}$$ of which you want to know the gradient at the point $$x$$. We introduce the following shorthand: $f = f(x,y,z)$ $f_{i,j,k} = f(x+ih,y+jh,z+kh)$ where $$h$$ is the spacing. Then the gradient at the point of interest is: $\nabla f \approx \frac{1}{4h} \left( \begin{bmatrix} 1\\ -1\\ -1 \end{bmatrix} f_{ 1,-1,-1} + \begin{bmatrix} -1\\ -1\\ 1 \end{bmatrix} f_{-1,-1, 1} + \begin{bmatrix} -1\\ 1\\ -1 \end{bmatrix} f_{-1, 1,-1} + \begin{bmatrix} 1\\ 1\\ 1 \end{bmatrix} f_{ 1, 1, 1} \right)$ with an error of $$\mathcal{O}(h^2)$$.

This stencil is better than the naive method of performing central difference on all axes. The latter needs 6 evaluations while the former need only 4, whilst sacrificing no accuracy!