Forward and centered finite difference give same error plot: why?

781 Views Asked by At

Let us consider the following standard approximations of the first derivative of a function

$$FD = \frac{f(x+h)-f(x)}{h}$$

$$CFD = \frac{f(x+h)-f(x-h)}{2h}$$

The first is first order accurate, while the second it second order accurate.

Consider now $f=\sin(x)$ and $x =2\pi$. If we plot in loglog scale the error as a function of the stepsize $h$, we obtain the following graph, where it appears that the accuracy is the same.

error plot


My attempt to explain it

I look at the truncation error for the forward derivative:

$$\tau_1 = f''(x) \frac{h}{2} + f^{(3)}(x) \frac{h^2}{6} + O(h^3)$$

Looking at the plot, I would expect that the first term of the truncation error is $0$. So, I note that $f''(x)=-\sin(x)$, and for $x \approx 2\pi$ it is almost $0$. Therefore the leading error term is $-\cos(x) \frac{h^2}{2}$, which should motivate the second order accuracy.

Is it correct ?

3

There are 3 best solutions below

3
On BEST ANSWER

This particular case is simply because the approximations themselves are (modulo floating point error) exactly the same for the same value of $h$:

$$\frac{\sin(2\pi+h)-\sin(2\pi)}{h}=\frac{\sin(2\pi+h)}{h}$$

whereas

$$\frac{\sin(2\pi+h)-\sin(2\pi-h)}{2h}=\frac{2\sin(2\pi+h)}{2h}=\frac{\sin(2\pi+h)}{h}$$

because of the fact that $\sin$ is an odd function when reflected through $2\pi$. Numerically this won't be exactly true but it will be close enough that they'll be indistinguishable except maybe for extremely small $h$.

5
On

Finite differences suffer from two sources of errors:

  • truncation error (given by the Taylor series). It decreases with $h$.
  • evaluation error due to floating-point arithmetic. It goes to infinity when $h$ goes to 0.

That's the reason finite differences go nuts when $h$ goes to 0 (here around $10^{-5}$). For centered finite differences, we should definitely expect the same behavior, but different slopes.

Edit 2: for $\sin$, indeed the Taylor series indicates that the truncation error is already a $O(h^2)$.

0
On

I think the two forms turn out to be identical for $f = \sin$ and $x = 2\pi$, because $\sin(2\pi) = 0$ and $\sin(2\pi + h) = - \sin(2\pi - h)$.