Convergence of finite differences¶
Let’s observe the convergence of the forward-difference formula applied to the function \(\sin(e^{x+1})\) at \(x=0\).
using FundamentalsNumericalComputation
f = x -> sin( exp(x+1) );
FD1 = [ (f(h)-f(0))/h for h in [0.1,0.05,0.025] ]
3-element Array{Float64,1}:
-2.737868275809363
-2.6127952856136947
-2.5464969752366606
It’s not clear that the sequence is converging. As predicted, however, the errors are cut approximately by a factor of 2 when \(h\) is divided by 2.
exact_value = cos(exp(1))*exp(1)
err = @. exact_value - FD1
pretty_table( [ [0.1,0.05,0.025] err],["h","error in FD1"],backend=:html)
h | error in FD1 |
---|---|
0.1 | 0.25951854285412823 |
0.05 | 0.1344455526584598 |
0.025 | 0.06814724228142577 |
Asymptotically as \(h\to0\), the error is proportional to \(h\).