Why does the graph of the absolute error vs x for the three-point central difference formula have this shape?

92 Views Asked by At

Using the three-point central difference formula, $$f'(x) \approx \frac{f(x+h)-f(x-h)}{2h}$$ to approximate the $f'(x)$ of $f(x)= e^{-x} sin(x)$ for the interval $[0,15]$ using different step sizes ($h$) produces the following error vs $x$ graph.

I wanted to ask why there are these uniform 'bumps' in the error? where it dips all of a sudden for certain values of $x$. I've found lots of explanations going over plots of error vs $h$ but so far I haven't found anything for error vs $x$ graphs.

I'm guessing it's something to do with the rounding or truncation error suddenly decreasing? If so why at these $x$ points in particular?

Here's the python code for the $h=0.01$ line:

h = 0.01 # step size (h)
x = np.arange(0, 15, h)
g = np.exp(-x) * np.sin(x) # function f(x)

# find solutions to f'(x)
fn = (g[2:] - g[:-2]) / (2*h) # numerical solution
fa = np.cos(x) * np.exp(-x) - np.sin(x) * np.exp(-x) # analytical solution
err = np.abs(fa[1:-1] - fn)

plt.semilogy(x[1:-1], err, '.-')
1

There are 1 best solutions below

6
On BEST ANSWER

The method error in its leading term is for the central difference quotient $$ \frac{f(x+h)-f(x-h)}{2h}=f'(x)+\frac{h^2}{6}f'''(x)+O(h^4) $$ In the given case we get $$ f(x)=e^{-x}\sin x\\ f'(x)=e^{-x}(\cos x-\sin x)\\ f''(x)=-2e^{-x}\cos x\\ f'''(x)=2e^{-x}(\sin x+\cos x)=2^{3/2}e^{-x}\sin\left(x+\frac\pi4\right) $$ The error term will oscillate, changing sign. Close to its roots the error will be especially small. And indeed the dips in the graph are at about $\frac{3\pi}4\approx2\frac14$, $\frac{7\pi}{4}\approx 5\frac12$, $\frac{11\pi}{4}\approx8\frac12$, ...

The depth of the dips mainly depends on the sampling density, how close the samples get to the root of the error term.

Other sources of error contributions only become substantial at much smaller step sizes, down from $h=10^{-5}$ for the given situation.