Newton's Method
Newton's method finds roots of equations, the where , 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 . The tangent line at is . Where it crosses zero is usually a better guess:
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 , solve . With :
"Average your guess with divided by your guess." This is the Babylonian (Heron's) method, thousands of years older than calculus. Watch the correct digits double for :
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 : quadratic convergence. Formally, near a simple root with .
A general root finder
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 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 | division by zero, or a huge jump | |
| Bad start | from | cycles forever |
| Diverges | from any | each step doubles the distance from the root |
| Multiple root | still converges, but only linearly |
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 and 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 to normalise vectors for lighting. Its famous routine reinterprets the float's bits as an integer, subtracts it (shifted) from the magic constant
0x5f3759dfto get a crude estimate, then does one Newton step for : . 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 with Newton-like iterations.
- Optimisation: applying Newton to gives Newton's optimisation step , the basis of second-order optimisers (Jacobian and Hessian).
Exercises
Exercise 1: One step by hand
Do one Newton step for starting at .
Answer
(true ).
Solution
Exercise 2: Cube roots
Derive the Newton iteration for and use it to find to 10 decimals.
Answer
;
Solution
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 is . Then start from the rough guess for and do two steps.
Answer
After two steps , close to .
Solution
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 does need to reach , and which root does it find? (There are two roots.)
Answer
It converges to the smaller root in 7 steps.
Solution
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 instead leads to the other root near 1.512.
Test yourself
What is the Newton update for solving ?
Near a simple root, how does Newton's error behave?
- It halves each step
- It drops by a constant each step
- Roughly squares each step, doubling the correct digits
- It stays constant
What did the Quake III fast inverse square root use after its bit-level initial guess?
- A lookup table
- One Newton iteration
- Bisection
- A Taylor series to 10 terms