TExcelTAD
Home › Numerical Methods › Newton's Method

Newton's Method

Newton's method replaces ff 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 xkx_k is y=f(xk)+f′(xk)(x−xk)y = f(x_k) + f'(x_k)(x - x_k). Setting y=0y = 0:

xk+1=xk−f(xk)f′(xk).x_{k+1} = x_k - \frac{f(x_k)}{f'(x_k)}.
python · try it yourself
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 9×10−29 \times 10^{-2}, 2.5×10−32.5 \times 10^{-3}, 2×10−62 \times 10^{-6}, 2×10−122 \times 10^{-12}, then machine precision: each is roughly the square of the one before (times f′′2f′≈0.35\frac{f''}{2f'} \approx 0.35): quadratic convergence. (This is the Babylonian square-root method, xk+1=12(xk+2xk)x_{k+1} = \frac{1}{2}\left(x_k + \frac{2}{x_k}\right).)

Why quadratic

Expand f(r)=0f(r) = 0 around xkx_k: 0=f(xk)+f′(xk)(r−xk)+12f′′(ξ)(r−xk)20 = f(x_k) + f'(x_k)(r - x_k) + \frac{1}{2}f''(\xi)(r - x_k)^2. Divide by f′(xk)f'(x_k) and subtract from the Newton step:

ϵk+1=f′′(ξ)2f′(xk) ϵk2≈f′′(r)2f′(r) ϵk2.\epsilon_{k+1} = \frac{f''(\xi)}{2f'(x_k)}\,\epsilon_k^2 \approx \frac{f''(r)}{2f'(r)}\,\epsilon_k^2.

This needs f′(r)≠0f'(r) \ne 0 (a simple root) and a start close enough that the quadratic term dominates.

When Newton fails

python · try it yourself
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 x3−2x+2x^3 - 2x + 2 from x0=0x_0 = 0 the iterates bounce between 00 and 11 forever.
  • Divergence: for arctan⁡x\arctan x from x0=1.5x_0 = 1.5 each tangent overshoots further.
  • Multiple root: at a double root convergence is only linear with rate 12\frac{1}{2} (the errors halve). The fix, if the multiplicity mm is known, is xk+1=xk−mf(xk)f′(xk)x_{k+1} = x_k - m\frac{f(x_k)}{f'(x_k)}.

Safeguards

  • Bracketing: keep an interval with a sign change, and fall back to bisection when the Newton step leaves it (as rtsafe in classic libraries does).
  • Damping / line search: take xk+1=xk−αff′x_{k+1} = x_k - \alpha\frac{f}{f'} with α≤1\alpha \le 1 chosen so that ∣f∣\lvert f\rvert 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; without fprime it runs the secant method, and with fprime2 Halley's cubically convergent method.
  • Hardware and libraries compute 1a\frac{1}{a} and 1a\frac{1}{\sqrt{a}} 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 f(x)=x3−5f(x) = x^3 - 5 from x0=2x_0 = 2.

Answer

x1=74=1.75x_1 = \frac{7}{4} = 1.75.

Solution

x1=2−8−512=2−0.25=1.75x_1 = 2 - \frac{8 - 5}{12} = 2 - 0.25 = 1.75 (53≈1.70998\sqrt[3]{5} \approx 1.70998).

Exercise 2: Division without dividing

Show that Newton's method for f(x)=1x−af(x) = \frac{1}{x} - a needs no division, and give the iteration.

Answer

xk+1=xk(2−axk)x_{k+1} = x_k(2 - ax_k).

Solution

f′(x)=−1x2f'(x) = -\frac{1}{x^2}, so x−1/x−a−1/x2=x+x−ax2=x(2−ax)x - \frac{1/x - a}{-1/x^2} = x + x - ax^2 = x(2 - ax). It converges to 1a\frac{1}{a} for 0<x0<2a0 < x_0 < \frac{2}{a}.

Exercise 3: Modified Newton for a double root (code)

For f(x)=(x−1)2exf(x) = (x - 1)^2e^x, compare plain Newton and xk+1=xk−2f(xk)f′(xk)x_{k+1} = x_k - 2\frac{f(x_k)}{f'(x_k)} from x0=2x_0 = 2 after 6 steps.

Answer

Plain Newton's error is still about 4×10−24 \times 10^{-2}; the modified iteration hits the root exactly (error 0 in floating point).

Solution
python · try it yourself
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 f(x)=0f(x) = 0 is…

  1. x−f′(x)/f(x)x - f'(x)/f(x)
  2. x−f(x)/f′(x)x - f(x)/f'(x)
  3. x−f(x)x - f(x)
  4. (x+f(x))/2(x + f(x))/2

At a double root Newton's method converges…

  1. quadratically
  2. linearly
  3. cubically
  4. not at all

Near a simple root, if the error is 10−410^{-4} now, after one Newton step it is roughly…

  1. 10−510^{-5}
  2. 10−810^{-8} (times a constant)
  3. 10−410^{-4}
  4. 10−1610^{-16} always