[Math] Numerically calculate the second “left hand” derivative

derivativesnumerical methods

The Problem

I have a series of measurements for which I have to calculate the first and second derivative numerically in a "live" fashion, i.e. using only previous data.

This is easy for the first derivative:

$f'(x) \approx \frac{f(x) – f(x-h)}{h}$

For the second derivative I use this formula:

$f''(x) \approx \frac{f(x) – 2f(x-h) + f(x-2h) }{h^2}$

The problem I have is that the results are a good estimate only for "previous" values of $x$.

I.e. $f'(x)$ yields a good estimate for $x-\frac12 h$ and
$f''(x)$ yields a good estimate for $x-h$. This is because the formulae I use are indeed the central formulae at these points,
e.g.

$f'(x + \frac12 h) = \frac{f(x+\frac h2)-f(x-\frac h2)}{2 \frac h2}$

The question

Is there a way to create a better estimate for the second derivation but still stick to using only "previous" points?

Comparison of approaches so far

I visualized the three approaches so far, using $f(x) = \operatorname{sin}(2x)$ as an example and $h = 0.16$:

  • orange diamond: based on $f''(x) \approx \frac{f(x) – 2f(x-h) + f(x-2h) }{h^2}$
  • pink cross: The points from orange diamond translated by $(-h,0)$
  • green circle: Using 4 points for approximation yielding $f''(x)\approx \frac{2 f(x) – 5f(x-h) + 4 f(x-2h)- f(x-3h)}{h^2}$ as suggested by gammatester

Note: If you do a linear extrapolation of $f''(x-2h)$ and $f''(x-h)$ (using a central differential approach) like $f''(x) = 2 f''(x-h) – f''(x-2h)$ as suggested by Hagen von Eitzen, you'll end up with the same formula as proposed by gammatester.

It seems to me that adding more points to the formulae not necessarily decreases the error, especially if the "scanning distance" $h$ is quite large.

visualization with $h = 0.16$

The same function with $h = 0.06$:

visualization with $h = 0.06$

The solution

I did take me some time but after I tested the higher accuracy formulae the wikipedia page gammatester mentioned, I found a formula that is quite accurate and only uses previously gathered data.

$f''(x) \approx \frac{ \frac{469}{90} f(x-0h) −\frac{223}{10} f(x-1h) + \frac{879}{20} f(x-2h) −\frac{949}{18} f(x-3h) + 41 f(x-4h) −\frac{201}{10} f(x-5h) +\frac{1019}{180} f(x-6h)− \frac{7}{10} f(x-7h)}{h^2} $

Best Answer

If you can use four points for the second derivative you have $$ f''(x)\approx \frac{2 f(x) - 5f(x-h) + 4 f(x-2h)- f(x-3h)}{h^2}$$ The error term is $$\frac{11}{12} f^{(4)}(\xi_0) h^2- f^{(5)}(\xi_1)h^3 + O(h^4)$$ with $x-3h \le \xi_0, \xi_1 \le x.$

The given formula can be found at many source, see e.g. the last one at Wikipedia; the error term can be computed by expanding the expression as a series in $h$.