Numerical Differentiation

Since the derivative is defined in terms of a limit, a derivative can be approximated numerically just like a limit. With the typical difference quotient, \[ f'(x) = \lim\limits_{h \to 0} \frac{f(x\!+\!h)\!-\!f(x)}{h} \approx \frac{f(x\!+\!h)\!-\!f(x)}{h}\;\;\text{ for a small value of } h\,. \] A symmetric difference quotient provides a better approximation \(f'(x):\) \[ f'(x) \approx \frac{f(x\!+\!h)\!-\!f(x\!-\!h)}{2h}\;\;\text{ for a small value of } h\,.\] For an even better approximation to \(f'\) we need two key facts: there is a unique polynomial of degree \(n\) that passes through any (generic) collection of \(n\!-\!1\) points, and the derivative of a polynomial at a point can expressed (pre-computed) as a formula in terms of its coefficients. For example, for each \(x\) in the domain of \(f,\) the nine-point stencil around \(\bigl(x,f(x)\bigr),\)

\[ \bigl(x\!\!-\!\!4h, f(x\!\!-\!\!4h)\bigr) \quad \bigl(x\!\!-\!\!3h, f(x\!\!-\!\!3h)\bigr) \quad \bigl(x\!\!-\!\!2h, f(x\!\!-\!\!2h)\bigr) \quad \bigl(x\!\!-\!\!h, f(x\!\!-\!\!h)\bigr) \quad \bigl(x, f(x)\bigr) \quad \bigl(x\!\!+\!\!h, f(x\!\!+\!\!h)\bigr) \quad \bigl(x\!\!+\!\!2h, f(x\!\!+\!\!2h)\bigr) \quad \bigl(x\!\!+\!\!3h, f(x\!\!+\!\!3h)\bigr) \quad \bigl(x\!\!+\!\!4h, f(x\!\!+\!\!4h)\bigr)\,, \]
defines a unique eighth-degree polynomial that approximates \(f\) around \(x,\) which has a pre-computable derivative that approximates \(f'\,.\) \[f'(x) \approx \frac{3f\bigl(x\!-\!4h\bigr)\!-\!32f\bigl(x\!-\!3h\bigr)\!+\!168f\bigl(x\!-\!2h\bigr)\!-\!672f\bigl(x\!-\!h\bigr)\!+\!672f\bigl(x\!+\!h\bigr)\!-\!168f\bigl(x\!+\!2h\bigr)\!+\!32f\bigl(x\!+\!3h\bigr)\!-\!3f\bigl(x\!+\!4h\bigr)}{840h} \;\;\text{ for a small value of } h\,.\]