Notebook for 2023-03-22
A test problem
A bisection routine
Bisection is pretty bulletproof. It is also slow, and does not generalize well to systems of nonlinear equations.
bisect (generic function with 1 method)
Unguarded Newton
The unguarded Newton iteration is
$$x_{k+1} = x_k-\frac{f(x_k)}{f'(x_k)}$$
unguarded_newton (generic function with 1 method)
The problem with unguarded Newton is that it only converges from points that are close enough to the solution. A nice example is what happens with using Newton to find a zero of $\tan^{-1}(x)$. Start too far from the zero at the origin, and the iteration swings wildly back and forth between large positive values and large negative values until it swings all the way out of the range of the floating point numbers and starts producing NaN results.
Guarding Newton
In the arctangent example, Newton is always stepping in the right direction – it just moves by the wrong amount. We get around this with a line search strategy. The simplest such strategy (which we will improve on later) is to cut the step size in half every time a proposed step reduces $|f(x)|$.
newton_ls (generic function with 1 method)
Secant iteration
Despite fast local convergence, Newton's iteration is not as robust as bisection, even with line search. It also has the annoying property that we have to remember how to compute derivatives!
Secant iteration uses a secant approximation to the function rather than a tangent approximation to the function. The secant approximation of $f$ through evaluations at $a$ and $b$ is
$$s(x) = f(a) + \frac{f(b)-f(a)}{b-a} (x-a)$$
and setting the secant to zero gives
$$x = \frac{a f(b) - b f(a)}{f(b)-f(a)}$$
If $f(a) f(b) < 0$, then $x$ always lies in the interval $[a,b]$. Hence, we can use the secant intersection point in place of the midpoint for a bisection-like iteration; this is the method of false position (or regula falsi). It can run slower than bisection in some cases, though typically not.
A variety of faster methods combine bisection with secant iteration (or some other polynomial interpolant) in some way.
We will just show the most straightforward version of secant iteration with no safeguarding.
secant (generic function with 1 method)
Using fzero
The Roots
package includes functions find_zero
and fzero
that includes a variety of root-finding algorithms. The package is quite robust, and includes a number of algorithms. Given an initial bracket, the default is slow-but-reliable bisection; but one can also specify methods like the A42
algorithm, which combines bisection with an interpolation-based method.
0.6180339887498948
One thing that find_zero
does not include is the type of monitor functionality that we have used in our home-grown codes above. However, it is easy to add this as a wrapper around an existing function.