Newton's Method
Newton's method replaces by its tangent line at the current guess and jumps to the tangent's root. Near a simple root it doubles the number of correct digits every step, which makes it the workhorse for nonlinear equations, optimization and implicit ODE solvers. Far from the root it can fail, so practical codes combine it with safeguards.
The iteration
The tangent at is . Setting :
import numpy as np
def newton(f, df, x, tol=1e-14, maxit=50):
history = [x]
for _ in range(maxit):
step = f(x) / df(x)
x = x - step
history.append(x)
if abs(step) < tol * max(1, abs(x)):
break
return x, history
r, hist = newton(lambda x: x**2 - 2, lambda x: 2 * x, 1.0)
for k, xk in enumerate(hist):
print(f"k = {k}: x = {xk:.16f}, error {abs(xk - np.sqrt(2)):.1e}")k = 0: x = 1.0000000000000000, error 4.1e-01
k = 1: x = 1.5000000000000000, error 8.6e-02
k = 2: x = 1.4166666666666667, error 2.5e-03
k = 3: x = 1.4142156862745099, error 2.1e-06
k = 4: x = 1.4142135623746899, error 1.6e-12
k = 5: x = 1.4142135623730951, error 0.0e+00
k = 6: x = 1.4142135623730949, error 2.2e-16
The errors go , , , , then machine precision: each is roughly the square of the one before (times ): quadratic convergence. (This is the Babylonian square-root method, .)
Why quadratic
Expand around : . Divide by and subtract from the Newton step:
This needs (a simple root) and a start close enough that the quadratic term dominates.
When Newton fails
import numpy as np
def newton_steps(f, df, x, n):
out = [x]
for _ in range(n):
x = x - f(x) / df(x)
out.append(x)
return [round(float(v), 4) for v in out]
print("cycle :", newton_steps(lambda x: x**3 - 2*x + 2, lambda x: 3*x**2 - 2, 0.0, 6))
print("overshoot:", newton_steps(np.arctan, lambda x: 1 / (1 + x * x), 1.5, 5))
print("double :", newton_steps(lambda x: (x - 1)**2, lambda x: 2 * (x - 1), 2.0, 6))cycle : [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
overshoot: [1.5, -1.6941, 2.3211, -5.1141, 32.2957, -1575.317]
double : [2.0, 1.5, 1.25, 1.125, 1.0625, 1.0312, 1.0156]
- Cycling: for from the iterates bounce between and forever.
- Divergence: for from each tangent overshoots further.
- Multiple root: at a double root convergence is only linear with rate (the errors halve). The fix, if the multiplicity is known, is .
Safeguards
- Bracketing: keep an interval with a sign change, and fall back to bisection when the Newton step leaves it (as
rtsafein classic libraries does). - Damping / line search: take with chosen so that decreases.
- Good starting guesses from physics, a coarse grid, or a few bisection steps.
In software
scipy.optimize.newton(f, x0, fprime=df)runs Newton; withoutfprimeit runs the secant method, and withfprime2Halley's cubically convergent method.- Hardware and libraries compute and by a few Newton steps from a table-based guess (the famous "fast inverse square root" in Quake III uses one Newton step).
- Implicit ODE solvers solve a nonlinear system by Newton at every time step (Stiff Equations).
Exercises
Exercise 1: One step by hand
Do one Newton step for from .
Answer
.
Solution
().
Exercise 2: Division without dividing
Show that Newton's method for needs no division, and give the iteration.
Answer
.
Solution
, so . It converges to for .
Exercise 3: Modified Newton for a double root (code)
For , compare plain Newton and from after 6 steps.
Answer
Plain Newton's error is still about ; the modified iteration hits the root exactly (error 0 in floating point).
Solution
import numpy as np
f = lambda x: (x - 1) ** 2 * np.exp(x)
df = lambda x: (x - 1) * (x + 1) * np.exp(x)
for m in (1, 2):
x = 2.0
for _ in range(6):
if f(x) == 0:
break
x = x - m * f(x) / df(x)
print(f"m = {m}: error {abs(x - 1):.1e}")m = 1: error 3.7e-02
m = 2: error 0.0e+00
Test yourself
Newton's step for is…
At a double root Newton's method converges…
- quadratically
- linearly
- cubically
- not at all
Near a simple root, if the error is now, after one Newton step it is roughly…
- (times a constant)
- always