Convergence of the secant method¶

We check the convergence of the secant method from the previous example.

using FundamentalsNumericalComputation

f = x -> x*exp(x) - 2;

x = FNC.secant(f,1,0.5)

9-element Array{Float64,1}:
1.0
0.5
0.8103717749522766
0.8656319273409482
0.85217802207241
0.8526012320981393
0.8526055034192025
0.8526055020137209
0.8526055020137254


We don’t know the exact root, so we use nlsolve to get a substitute.

r = nlsolve(x->f(x[1]),[1.]).zero

1-element Array{Float64,1}:
0.8526055020137259


Here is the sequence of errors.

err = @. r - x

9-element Array{Float64,1}:
-0.14739449798627413
0.35260550201372587
0.04223372706144923
-0.013026425327222313
0.000427479941315867
4.269915586552209e-6
-1.4054766239723904e-9
4.9960036108132044e-15
4.440892098500626e-16


It’s not so easy to see the convergence rate by looking at these numbers. But we can check the ratios of the log of successive errors.

logerr = @. log(abs(err))
ratios = @. logerr[2:end] / logerr[1:end-1]

8-element Array{Float64,1}:
0.544438628027792
3.0358017547194502
1.3716940021941613
1.7871469297605422
1.5937804750422417
1.6485786973526697
1.6155775318340528
1.0735000904878467


It seems to be heading toward a constant ratio of about 1.6 before it bumps up against machine precision.