Newton’s method
import Pkg; Pkg.activate("/Users/driscoll/Documents/GitHub/fnc")
using FNCFunctions
using Plots
default(
titlefont=(11,"Helvetica"),
guidefont=(11,"Helvetica"),
linewidth = 2,
markersize = 3,
msa = 0,
size=(500,320),
label="",
html_output_format = "svg"
)
using PrettyTables, LaTeXStrings, Printf
using LinearAlgebraproject at `~/Documents/GitHub/fnc`
4.3. Newton’s method ¶ Newton’s method is the cornerstone of rootfinding. We introduce the key idea with an example in Example 4.3.1 .
Suppose we want to find a root of the function
f(x) = x * exp(x) - 2
using Plots
plot(f, 0, 1.5;
label="function", legend=:topleft,
grid=:y, ylim=[-2, 4], xlabel=L"x", ylabel=L"y")From the graph, it is clear that there is a root near x = 1 x=1 x = 1 . So we call that our initial guess, x 1 x_1 x 1 .
x₁ = 1
y₁ = f(x₁)
scatter!([x₁], [y₁], label="initial point")Next, we can compute the tangent line at the point ( x 1 , f ( x 1 ) ) \bigl(x_1,f(x_1)\bigr) ( x 1 , f ( x 1 ) ) , using the derivative.
df_dx(x) = exp(x) * (x + 1)
m₁ = df_dx(x₁)
tangent = x -> y₁ + m₁ * (x - x₁)
plot!(tangent, 0, 1.5, l=:dash, label="tangent line",
title="Tangent line approximation")In lieu of finding the root of f f f itself, we settle for finding the root of the tangent line approximation, which is trivial. Call this x 2 x_2 x 2 , our next approximation to the root.
@show x₂ = x₁ - y₁ / m₁
scatter!([x₂], [0], label="tangent root", title="First iteration")x₂ = x₁ - y₁ / m₁ = 0.8678794411714423 The residual (i.e., value of f f f ) is smaller than before, but not zero. So we repeat the process with a new tangent line based on the latest point on the curve.
plot(f, 0.82, 0.87;
label="function", legend=:topleft,
xlabel=L"x", ylabel=L"y",
title="Second iteration")
scatter!([x₂], [y₂], label="starting point")
m₂ = df_dx(x₂)
tangent = x -> y₂ + m₂ * (x - x₂)
plot!(tangent, 0.82, 0.87; l=:dash, label="tangent line")
@show x₃ = x₂ - y₂ / m₂
scatter!([x₃], [0], label="tangent root")x₃ = x₂ - y₂ / m₂ = 0.8527833734164099
Judging by the residual, we appear to be getting closer to the true root each time.
Using general notation, if we have a root approximation x k x_k x k , we can construct a linear model of f ( x ) f(x) f ( x ) using the classic formula for the tangent line of a differentiable function,
q ( x ) = f ( x k ) + f ′ ( x k ) ( x − x k ) . q(x) = f(x_k) + f'(x_k)(x-x_k). q ( x ) = f ( x k ) + f ′ ( x k ) ( x − x k ) . Finding the root of q ( x ) = 0 q(x)=0 q ( x ) = 0 is trivial. We define the next approximation by the condition q ( x k + 1 ) = 0 q(x_{k+1})=0 q ( x k + 1 ) = 0 , which leads to the following.
Given a function f f f , its derivative, f ′ f' f ′ , and an initial value x 1 x_1 x 1 , iteratively define
x k + 1 = x k − f ( x k ) f ′ ( x k ) , k = 1 , 2 , … . x_{k+1} = x_k - \frac{f(x_k)}{f'(x_k)}, \qquad k=1,2,\ldots. x k + 1 = x k − f ′ ( x k ) f ( x k ) , k = 1 , 2 , … . Return the sequence { x k } \{x_k\} { x k } .
4.3.1 Convergence ¶ The graphs of Example 4.3.1 suggest why the Newton iteration may converge to a root: any differentiable function looks more and more like its tangent line as we zoom in to the point of tangency. Yet it is far from clear that it must converge, or at what rate it will do so. The matter of the convergence rate is fairly straightforward to resolve. Define the error sequence
ϵ k = x k − r , k = 1 , 2 , … , \epsilon_k = x_k - r , \quad k=1,2,\ldots, ϵ k = x k − r , k = 1 , 2 , … , where r r r is the limit of the sequence and f ( r ) = 0 f(r)=0 f ( r ) = 0 . Exchanging x x x -values for ϵ \epsilon ϵ -values in (4.3.2) gives
ϵ k + 1 + r = ϵ k + r − f ( r + ϵ k ) f ′ ( r + ϵ k ) . \epsilon_{k+1}+r = \epsilon_k + r - \frac{f(r+\epsilon_k)}{f'(r+\epsilon_k)}. ϵ k + 1 + r = ϵ k + r − f ′ ( r + ϵ k ) f ( r + ϵ k ) . We assume that ∣ ϵ k ∣ → 0 |\epsilon_k|\to 0 ∣ ϵ k ∣ → 0 ; eventually, the errors remain as small as we please forever. Then a Taylor expansion of f f f about x = r x=r x = r gives
ϵ k + 1 = ϵ k − f ( r ) + ϵ k f ′ ( r ) + 1 2 ϵ k 2 f ′ ′ ( r ) + O ( ϵ k 3 ) f ′ ( r ) + ϵ k f ′ ′ ( r ) + O ( ϵ k 2 ) . \epsilon_{k+1} = \epsilon_k - \frac{ f(r) + \epsilon_kf'(r) + \frac{1}{2}\epsilon_k^2f''(r) +
O(\epsilon_k^3)}{ f'(r) + \epsilon_kf''(r) + O(\epsilon_k^2)}. ϵ k + 1 = ϵ k − f ′ ( r ) + ϵ k f ′′ ( r ) + O ( ϵ k 2 ) f ( r ) + ϵ k f ′ ( r ) + 2 1 ϵ k 2 f ′′ ( r ) + O ( ϵ k 3 ) . We use the fact that f ( r ) = 0 f(r)=0 f ( r ) = 0 and additionally assume now that r r r is a simple root, i.e., f ′ ( r ) ≠ 0 f'(r)\neq 0 f ′ ( r ) = 0 . Then
ϵ k + 1 = ϵ k − ϵ k [ 1 + 1 2 f ′ ′ ( r ) f ′ ( r ) ϵ k + O ( ϵ k 2 ) ] [ 1 + f ′ ′ ( r ) f ′ ( r ) ϵ k + O ( ϵ k 2 ) ] − 1 . \epsilon_{k+1} = \epsilon_k - \epsilon_k \left[ 1 + \dfrac{1}{2}\dfrac{f''(r)}{f'(r)} \epsilon_k
+ O(\epsilon_k^2)\right] \, \left[ 1 + \dfrac{f''(r)}{f'(r)}\epsilon_k + O(\epsilon_k^2)\right]^{-1}. ϵ k + 1 = ϵ k − ϵ k [ 1 + 2 1 f ′ ( r ) f ′′ ( r ) ϵ k + O ( ϵ k 2 ) ] [ 1 + f ′ ( r ) f ′′ ( r ) ϵ k + O ( ϵ k 2 ) ] − 1 . The series in the denominator is of the form 1 / ( 1 + z ) 1/(1+z) 1/ ( 1 + z ) . Provided ∣ z ∣ < 1 |z|<1 ∣ z ∣ < 1 , this is the limit of the geometric series 1 − z + z 2 − z 3 + ⋯ 1-z+z^2-z^3 + \cdots 1 − z + z 2 − z 3 + ⋯ . Keeping only the lowest-order terms, we derive
ϵ k + 1 = ϵ k − ϵ k [ 1 + 1 2 f ′ ′ ( r ) f ′ ( r ) ϵ k + O ( ϵ k 2 ) ] [ 1 − f ′ ′ ( r ) f ′ ( r ) ϵ k + O ( ϵ k 2 ) ] = 1 2 f ′ ′ ( r ) f ′ ( r ) ϵ k 2 + O ( ϵ k 3 ) . \begin{align*}
\epsilon_{k+1} &= \epsilon_k - \epsilon_k \left[ 1 + \dfrac{1}{2}\dfrac{f''(r)}{f'(r)} \epsilon_k + O(\epsilon_k^2) \right] \, \left[ 1 - \dfrac{f''(r)}{f'(r)}
\epsilon_k + O(\epsilon_k^2) \right]\\
&= \frac{1}{2}\, \frac{f''(r)}{f'(r)} \epsilon_k^2 + O(\epsilon_k^3).
\end{align*} ϵ k + 1 = ϵ k − ϵ k [ 1 + 2 1 f ′ ( r ) f ′′ ( r ) ϵ k + O ( ϵ k 2 ) ] [ 1 − f ′ ( r ) f ′′ ( r ) ϵ k + O ( ϵ k 2 ) ] = 2 1 f ′ ( r ) f ′′ ( r ) ϵ k 2 + O ( ϵ k 3 ) . Suppose a sequence x k x_k x k approaches limit x ∗ x^* x ∗ . If the error sequence ϵ k = x k − x ∗ \epsilon_k=x_k - x^* ϵ k = x k − x ∗ satisfies
lim k → ∞ ∣ ϵ k + 1 ∣ ∣ ϵ k ∣ 2 = L \lim_{k\to\infty} \frac{|\epsilon_{k+1}|}{|\epsilon_k|^2} = L k → ∞ lim ∣ ϵ k ∣ 2 ∣ ϵ k + 1 ∣ = L for a positive constant L L L , then the sequence has quadratic convergence to the limit.
Recall that linear convergence is identifiable by trending toward a straight line on a log-linear plot of the error. When the convergence is quadratic, no such straight line exists—the convergence keeps getting steeper. As a numerical test, note that ∣ ϵ k + 1 ∣ ≈ K ∣ ϵ k ∣ 2 |\epsilon_{k+1}|\approx K |\epsilon_{k}|^2 ∣ ϵ k + 1 ∣ ≈ K ∣ ϵ k ∣ 2 implies that as k → ∞ k\to\infty k → ∞ ,
log ∣ ϵ k + 1 ∣ ≈ 2 log ∣ ϵ k ∣ + log K , log ∣ ϵ k + 1 ∣ log ∣ ϵ k ∣ ≈ 2 + log K log ∣ ϵ k ∣ → 2. \begin{split}
\log |\epsilon_{k+1}| & \approx 2 \log |\epsilon_{k}| + \log K,\\
\frac{\log |\epsilon_{k+1}|}{\log |\epsilon_{k}|} &\approx 2 + \frac{\log K}{\log |\epsilon_{k}|} \to 2.
\end{split} log ∣ ϵ k + 1 ∣ log ∣ ϵ k ∣ log ∣ ϵ k + 1 ∣ ≈ 2 log ∣ ϵ k ∣ + log K , ≈ 2 + log ∣ ϵ k ∣ log K → 2. We again look at finding a solution of x e x = 2 x e^x=2 x e x = 2 near x = 1 x=1 x = 1 . To apply Newton’s method, we need to calculate values of both the residual function f f f and its derivative.
f(x) = x * exp(x) - 2;
df_dx(x) = exp(x) * (x + 1);
We don’t know the exact root, so we use nlsolve to determine a proxy for it.
using NLsolve
r = nlsolve(x -> f(x[1]), [1.0]).zero1-element Vector{Float64}:
0.852605502013726
We use x 1 = 1 x_1=1 x 1 = 1 as a starting guess and apply the iteration in a loop, storing the sequence of iterates in a vector.
x = [1; zeros(4)]
for k = 1:4
x[k+1] = x[k] - f(x[k]) / df_dx(x[k])
end
x5-element Vector{Float64}:
1.0
0.8678794411714423
0.8527833734164099
0.8526055263689221
0.852605502013726
Here is the sequence of errors.
5-element Vector{Float64}:
0.14739449798627402
0.015273939157716354
0.00017787140268388235
2.435519608212644e-8
0.0
Because the error reaches machine epsilon so rapidly, we’re going to use extended precision to allow us to take a few more iterations. We’ll take the last iteration as the most accurate root estimate.
A BigFloat uses 256 bits of precision, rather than 53 in Float64. But arithmetic is done by software emulation and is much slower.
x = [BigFloat(1); zeros(7)]
for k = 1:7
x[k+1] = x[k] - f(x[k]) / df_dx(x[k])
end
r = x[end]┌ Warning: Timed out waiting for `Base.active_repl_backend.ast_transforms` to become available. Autoloads will not work.
└ @ BasicAutoloads ~/.julia/packages/BasicAutoloads/08hIo/src/BasicAutoloads.jl:117
[ Info: If you have a slow startup file, consider moving `register_autoloads` to the end of it.
0.8526055020137254913464724146953174668984533001514035087721073946525150656742605
ϵ = @. Float64(x[1:end-1] - r)7-element Vector{Float64}:
0.14739449798627452
0.01527393915771683
0.00017787140268443004
2.435519656311045e-8
4.56680051680793e-16
1.6056572825272187e-31
1.9848810119594387e-62
The exponents in the scientific notation definitely suggest a squaring sequence. We can check the evolution of the ratio in (4.3.9) .
logerr = @. log10(abs(ϵ))
ratios = [NaN; [logerr[i+1] / logerr[i] for i in 1:length(logerr)-1]]
pretty_table( (iter=1:7, ϵ, logerr, ratios);
column_labels=["iteration", "error", "log error", "ratio"], backend=:html )The clear convergence to 2 above constitutes good evidence of quadratic convergence.
Let’s summarize the assumptions made to derive quadratic convergence as given by (4.3.7) :
The residual function f f f has to have enough continuous derivatives to make the Taylor series expansion valid. Often this is stated as f f f having sufficient smoothness . This is usually not a problem, but see Exercise 4.3.6 .
We required f ′ ( r ) ≠ 0 f'(r)\neq 0 f ′ ( r ) = 0 , meaning that r r r must be a simple root. See Exercise 4.3.7 to investigate what happens at a multiple root.
We assumed that the sequence converged, which is not easy to guarantee in any particular case. In fact,
finding a starting value from which the Newton iteration converges is often the most challenging part of a rootfinding problem. We will try to deal with this issue in Quasi-Newton methods .
4.3.2 Implementation ¶ Our implementation of Newton’s iteration is given in Function 4.3.1 . It accepts functions that evaluate f f f and f ′ f' f ′ and the starting value x 1 x_1 x 1 as input arguments. Beginning programmers are tempted to embed f f f and f ′ f' f ′ directly into the code, but there are two good reasons not to do so. First, each new rootfinding problem would require its own copy of the code, creating a lot of duplication. Second, you may want to try more than one rootfinding algorithm for a particular problem, and keeping the definition of the problem separate from the algorithm for its solution makes this task much easier.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
newton(f, df_dx, x₁ [;maxiter,f tol, xtol])
Use Newton's method to find a root of `f` starting from `x₁`, where
`df_dx` is the derivative of `f`. Returns a vector of root estimates.
The optional keyword parameters set the maximum number of iterations
and the stopping tolerance for values of `f` and changes in `x`.
"""
function newton(f, df_dx, x₁; maxiter = 40, ftol = 1e-13, xtol = 1e-13)
x = [float(x₁)]
y = f(x₁)
Δx = Inf # for initial pass below
k = 1
while (abs(Δx) > xtol) && (abs(y) > ftol)
dy_dx = df_dx(x[k])
Δx = -y / dy_dx # Newton step
push!(x, x[k] + Δx) # append new estimate
k += 1
y = f(x[k])
if k == maxiter
@warn "Maximum number of iterations reached."
break # exit loop
end
end
return x
end
Function 4.3.1 accepts keyword arguments . In the function declaration, these follow the semicolon, and when the function is called, they may be supplied as keyword=value in the argument list. Here, these arguments are also given default values by the assignments within the declaration. This arrangement is useful when there are multiple optional arguments, because the ordering of them doesn’t matter.
The break statement, seen here in line 25, causes an immediate exit from the innermost loop in which it is called. It is often used as a safety valve to escape an iteration that may not be able to terminate otherwise.
Function 4.3.1 also deals with a thorny practical issue: how to stop the iteration. It adopts a three-part criterion. First, it monitors the difference between successive root estimates, ∣ x k − x k − 1 ∣ |x_k-x_{k-1}| ∣ x k − x k − 1 ∣ , which is used as a stand-in for the unknown error ∣ x k − r ∣ |x_k-r| ∣ x k − r ∣ . In addition, it monitors the residual ∣ f ( x k ) ∣ |f(x_k)| ∣ f ( x k ) ∣ , which is equivalent to the backward error and more realistic to control in badly conditioned problems (see The rootfinding problem ). If either of these quantities is considered to be sufficiently small, the iteration ends. Finally, we need to protect against the possibility of a nonconvergent iteration, so the procedure terminates with a warning if a maximum number of iterations is exceeded.
Suppose we want to evaluate the inverse of the function h ( x ) = e x − x h(x)=e^x-x h ( x ) = e x − x . This means solving y = e x − x y=e^x-x y = e x − x for x x x when y y y is given, which has no elementary form. If a value of y y y is given numerically, though, we simply have a rootfinding problem for f ( x ) = e x − x − y f(x)=e^x-x-y f ( x ) = e x − x − y .
The enumerate function produces a pair of values for each iteration: a positional index and the corresponding contents.
g(x) = exp(x) - x
dg_dx(x) = exp(x) - 1
y = range(g(0), g(2), 200)
x = zeros(length(y))
for (i, y) in enumerate(y)
f(x) = g(x) - y
df_dx(x) = dg_dx(x)
r = FNC.newton(f, df_dx, y)
x[i] = r[end]
end
plot(g, 0, 2, aspect_ratio=1, label=L"g(x)")
plot!(y, x, label=L"g^{-1}(y)", title="Function and its inverse")
plot!(x -> x, 0, maximum(y), label="", l=(:dash, 1), color=:black)4.3.3 Exercises ¶ For each of Exercises 1–3, do the following steps.
(a) ✍ Rewrite the equation into the standard form for rootfinding, f ( x ) = 0 f(x) = 0 f ( x ) = 0 , and compute f ′ ( x ) f'(x) f ′ ( x ) .
(b) ⌨ Make a plot of f f f over the given interval and determine how many roots lie in the interval.
(c) ⌨ Like in Example 4.3.2 , find a reference value for each root using a native solver.
(d) ⌨ Use Function 4.3.1 to find each root.
(e) ⌨ For one of the roots, use the errors in the Newton sequence to determine numerically whether the convergence is roughly quadratic.
x 2 = e − x x^2=e^{-x} x 2 = e − x , over [ − 2 , 2 ] [-2,2] [ − 2 , 2 ]
2 x = tan x 2x = \tan x 2 x = tan x , over [ − 0.2 , 1.4 ] [-0.2,1.4] [ − 0.2 , 1.4 ]
sin ( π x ) = 2 x \sin(\pi x) = 2x sin ( π x ) = 2 x , over [ 0.1 , 2 ] [0.1, 2] [ 0.1 , 2 ]
⌨ Plot the function f ( x ) = x − 2 − sin x f(x)=x^{-2} - \sin x f ( x ) = x − 2 − sin x on the interval x ∈ [ 0.5 , 10 ] . x \in [0.5,10]. x ∈ [ 0.5 , 10 ] . For each initial value x 1 = 1 , x 1 = 2 , … , x 1 = 7 , x_1=1,\, x_1=2,\,\ldots,\, x_1=7, x 1 = 1 , x 1 = 2 , … , x 1 = 7 , apply Function 4.3.1 to f f f , and make a table showing x 1 x_1 x 1 and the resulting root found by the method. In which case does the iteration converge to a root other than the one closest to it? Use the plot to explain why that happened.
In the case of a multiple root, where f ( r ) = f ′ ( r ) = 0 f(r)=f'(r)=0 f ( r ) = f ′ ( r ) = 0 , the derivation of the quadratic error convergence leading up to (4.3.7) is invalid.
(a) ✍ Redo the derivation to show that in this circumstance and with f ′ ′ ( r ) ≠ 0 f''(r)\neq 0 f ′′ ( r ) = 0 , the error in general converges only linearly.
(b) ⌨ Use Function 4.3.1 to find a root of f ( x ) = e x + 1 − 2 − x f(x) = e^{x+1} - 2 - x f ( x ) = e x + 1 − 2 − x starting at x 1 = 0 , x_1=0, x 1 = 0 , and make a plot of the errors showing that the convergence is linear.
✍ In Function 4.3.1 and elsewhere, the actual error is not available, so we use ∣ x k − x k − 1 ∣ |x_k-x_{k-1}| ∣ x k − x k − 1 ∣ as an approximate indicator of error to determine when to stop the iteration. Find an example that foils this indicator; that is, a sequence { x k } \{x_k\} { x k } such that
lim k → ∞ ( x k − x k − 1 ) = 0 , \lim_{k\rightarrow \infty} (x_k-x_{k-1}) = 0, k → ∞ lim ( x k − x k − 1 ) = 0 , but { x k } \{x_k\} { x k } diverges. (Hint: You have seen such sequences in calculus.) While you do not need to show that the sequence is produced by Newton’s method for any particular f , f, f , this does justify the use of residual tolerances and safety valves in the code.