TExcelTAD
Home › Calculus Essentials › Newton's Method

Newton's Method

Newton's method finds roots of equations, the xx where f(x)=0f(x) = 0, by repeatedly sliding down tangent lines. When it works, it is astonishingly fast: the number of correct digits roughly doubles every step. Square roots in hardware and libraries, the famous "fast inverse square root" in Quake III, and many solvers in science and finance are Newton's method in disguise.

The idea

Start with a guess x0x_0. The tangent line at (x0,f(x0))(x_0, f(x_0)) is y=f(x0)+f′(x0)(x−x0)y = f(x_0) + f'(x_0)(x - x_0). Where it crosses zero is usually a better guess:

xn+1=xn−f(xn)f′(xn)x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}

Repeat until the change is smaller than your tolerance. Each step replaces the curve by its tangent, which is accurate near the root.

Square roots by Newton

To compute a\sqrt{a}, solve f(x)=x2−a=0f(x) = x^2 - a = 0. With f′(x)=2xf'(x) = 2x:

xn+1=xn−xn2−a2xn=xn+a/xn2x_{n+1} = x_n - \frac{x_n^2 - a}{2x_n} = \frac{x_n + a/x_n}{2}

"Average your guess with aa divided by your guess." This is the Babylonian (Heron's) method, thousands of years older than calculus. Watch the correct digits double for 2\sqrt{2}:

python · try it yourself
from decimal import Decimal, getcontext

getcontext().prec = 60
a = Decimal(2)
true = a.sqrt()
x = Decimal(1)
for n in range(1, 7):
    x = (x + a / x) / 2
    err = abs(x - true)
    print(f"step {n}: {str(x)[:42]}  error {err:.1e}")
step 1: 1.5  error 8.6e-2
step 2: 1.4166666666666666666666666666666666666666  error 2.5e-3
step 3: 1.4142156862745098039215686274509803921568  error 2.1e-6
step 4: 1.4142135623746899106262955788901349101165  error 1.6e-12
step 5: 1.4142135623730950488016896235025302436149  error 9.0e-25
step 6: 1.4142135623730950488016887242096980785696  error 2.9e-49

The errors go about 10−1→10−3→10−6→10−12→10−24→10−4810^{-1} \to 10^{-3} \to 10^{-6} \to 10^{-12} \to 10^{-24} \to 10^{-48}: quadratic convergence. Formally, near a simple root en+1≈C⋅en2e_{n+1} \approx C \cdot e_n^2 with C=f′′(r)2f′(r)C = \frac{f''(r)}{2f'(r)}.

A general root finder

python · try it yourself
import math

def newton(f, df, x, tol=1e-12, max_steps=50):
    for step in range(1, max_steps + 1):
        dx = f(x) / df(x)
        x -= dx
        if abs(dx) < tol:
            return x, step
    raise RuntimeError("did not converge")

root, steps = newton(lambda x: x**3 - 2 * x - 5, lambda x: 3 * x**2 - 2, 2.0)
print(f"x^3 - 2x - 5 = 0:  x = {root:.15f}  ({steps} steps)")
root, steps = newton(lambda x: math.cos(x) - x, lambda x: -math.sin(x) - 1, 1.0)
print(f"cos x = x:         x = {root:.15f}  ({steps} steps)")

import sympy as sp
y = sp.symbols('y')
print("SymPy nsolve:      ", sp.nsolve(y**3 - 2 * y - 5, y, 2))
x^3 - 2x - 5 = 0:  x = 2.094551481542327  (5 steps)
cos x = x:         x = 0.739085133215161  (5 steps)
SymPy nsolve:       2.09455148154233

Compare with bisection from Continuity: it needed about 40 halvings for 12 digits on the same cos⁡x=x\cos x = x problem; Newton needs a handful of steps. The price is that Newton needs the derivative, and it can fail.

When Newton fails

Problem Example What happens
Flat tangent f′(xn)=0f'(x_n) = 0 division by zero, or a huge jump
Bad start x3−2x+2x^3 - 2x + 2 from x0=0x_0 = 0 cycles 0→1→0→1…0 \to 1 \to 0 \to 1 \ldots forever
Diverges x3\sqrt[3]{x} from any x0≠0x_0 \ne 0 each step doubles the distance from the root
Multiple root (x−1)2(x - 1)^2 still converges, but only linearly
python · try it yourself
x = 0.0
f = lambda x: x**3 - 2 * x + 2
df = lambda x: 3 * x**2 - 2
seq = [x]
for _ in range(6):
    x = x - f(x) / df(x)
    seq.append(x)
print("cycle:", seq)
x = 0.5
for _ in range(20):
    x = x - f(x) / df(x)
print("from 0.5 instead it settles at:", round(x, 10), " f =", f"{f(x):.1e}")
cycle: [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
from 0.5 instead it settles at: -1.7692923542  f = 0.0e+00

In practice, robust solvers combine Newton with a safe bracketing method (SciPy's brentq, or "safeguarded Newton"), see SciPy.

In software

  • Square roots and division in hardware: CPUs and GPUs compute 1/x1/x and 1/x1/\sqrt{x} by taking a rough table-based estimate and refining it with one or two Newton steps. Each step doubles the correct bits, so 12 bits become 24 (single precision) after one step.
  • The fast inverse square root: Quake III Arena (1999) needed 1/x1/\sqrt{x} to normalise vectors for lighting. Its famous routine reinterprets the float's bits as an integer, subtracts it (shifted) from the magic constant 0x5f3759df to get a crude estimate, then does one Newton step for f(y)=1/y2−xf(y) = 1/y^2 - x: y←y⋅(1.5−0.5⋅x⋅y2)y \leftarrow y \cdot (1.5 - 0.5 \cdot x \cdot y^2). The result is within about 0.2% of the true value. Modern CPUs have a dedicated instruction (rsqrtss), but the idea, a cheap guess plus Newton, is the same.
  • Implicit equations everywhere: finding the interest rate of a loan (IRR), the time a ray hits an implicit surface, implied volatility in option pricing, inverse kinematics and implicit physics integrators all solve f(x)=0f(x) = 0 with Newton-like iterations.
  • Optimisation: applying Newton to f′(x)=0f'(x) = 0 gives Newton's optimisation step x←x−f′(x)/f′′(x)x \leftarrow x - f'(x)/f''(x), the basis of second-order optimisers (Jacobian and Hessian).

Exercises

Exercise 1: One step by hand

Do one Newton step for f(x)=x2−10f(x) = x^2 - 10 starting at x0=3x_0 = 3.

Answer

x1=19/6≈3.1667x_1 = 19/6 \approx 3.1667 (true 10≈3.1623\sqrt{10} \approx 3.1623).

Solution
x1=x0+10/x02=3+10/32=19/32=196≈3.16667x_1 = \frac{x_0 + 10/x_0}{2} = \frac{3 + 10/3}{2} = \frac{19/3}{2} = \frac{19}{6} \approx 3.16667

Exercise 2: Cube roots

Derive the Newton iteration for a3\sqrt[3]{a} and use it to find 203\sqrt[3]{20} to 10 decimals.

Answer

xn+1=2xn+a/xn23x_{n+1} = \dfrac{2x_n + a/x_n^2}{3}; 203≈2.7144176166\sqrt[3]{20} \approx 2.7144176166

Solution
f(x)=x3−a,f′(x)=3x2xn+1=x−x3−a3x2=2x+a/x23\begin{aligned} &f(x) = x^3 - a, \quad f'(x) = 3x^2 \\ &x_{n+1} = x - \frac{x^3 - a}{3x^2} = \frac{2x + a/x^2}{3} \end{aligned}
python · try it yourself
a, x = 20.0, 3.0
for _ in range(6):
    x = (2 * x + a / x**2) / 3
print(f"{x:.10f}", f"{20 ** (1 / 3):.10f}")
2.7144176166 2.7144176166

Exercise 3: Inverse square root step

Show that a Newton step for f(y)=1/y2−xf(y) = 1/y^2 - x is y←y⋅(1.5−0.5⋅x⋅y2)y \leftarrow y \cdot (1.5 - 0.5 \cdot x \cdot y^2). Then start from the rough guess y=0.3y = 0.3 for x=10x = 10 and do two steps.

Answer

After two steps y≈0.316221y \approx 0.316221, close to 1/10≈0.3162281/\sqrt{10} \approx 0.316228.

Solution
f′(y)=−2y3y−f(y)f′(y)=y+(1y2−x)⋅y32=y+y2−x⋅y32=y⋅(1.5−0.5⋅x⋅y2)\begin{aligned} f'(y) &= -\frac{2}{y^3} \\ y - \frac{f(y)}{f'(y)} &= y + \left(\frac{1}{y^2} - x\right) \cdot \frac{y^3}{2} = y + \frac{y}{2} - \frac{x \cdot y^3}{2} = y \cdot (1.5 - 0.5 \cdot x \cdot y^2) \end{aligned}
python · try it yourself
x, y = 10.0, 0.3
for step in (1, 2):
    y = y * (1.5 - 0.5 * x * y * y)
    print(step, round(y, 6))
print("true", round(10 ** -0.5, 6))
1 0.315
2 0.316221
true 0.316228

The formula uses only multiplication, no division or square root, which is why it was so fast in 1999.

Exercise 4: Count the steps

How many Newton steps from x0=1x_0 = 1 does ex=3xe^x = 3x need to reach ∣Δx∣<10−12\lvert \Delta x \rvert < 10^{-12}, and which root does it find? (There are two roots.)

Answer

It converges to the smaller root x≈0.619061x \approx 0.619061 in 7 steps.

Solution
python · try it yourself
import math

x = 1.0
for step in range(1, 50):
    dx = (math.exp(x) - 3 * x) / (math.exp(x) - 3)
    x -= dx
    if abs(dx) < 1e-12:
        break
print(f"root {x:.6f} after {step} steps")
root 0.619061 after 7 steps

Starting at x0=2x_0 = 2 instead leads to the other root near 1.512.

Test yourself

What is the Newton update for solving f(x)=0f(x) = 0?

  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)⋅f′(x)x + f(x) \cdot f'(x)
  4. (x+f(x))/2(x + f(x))/2

Near a simple root, how does Newton's error behave?

  1. It halves each step
  2. It drops by a constant each step
  3. Roughly squares each step, doubling the correct digits
  4. It stays constant

What did the Quake III fast inverse square root use after its bit-level initial guess?

  1. A lookup table
  2. One Newton iteration
  3. Bisection
  4. A Taylor series to 10 terms