AnyLearn
All lessons
Mathadvanced

Root Finding: Bisection, Newton, and Knowing When to Stop

Most equations have no formula for their solution, so computers chase roots with sequences. Compare bisection, secant, Newton and Brent's method by convergence order and guarantees, watch Newton fail, learn how accurate a root can be, and pick stopping tests that do not lie.

Updated · AI-authored, review-gated · how lessons are made

Progress1 / 11

An equation you cannot rearrange

Solve x=cos⁡xx = \cos x. No sequence of algebra isolates xx, and the same is true of Kepler's equation M=E−esin⁡EM = E - e \sin E, of the interest rate hidden inside a loan payment, and of almost every equation a model produces. What a computer can do instead is generate a sequence x0,x1,x2,…x_0, x_1, x_2, \dots that closes in on a number x⋆x^\star with f(x⋆)=0f(x^\star) = 0.

Every root finder in common use belongs to one of two families, and the split decides almost everything about how it behaves:

  • Bracketing methods start from an interval [a,b][a, b] where ff changes sign and never let the root escape it. They cannot fail on a continuous function, and they are slow.
  • Open methods start from a guess and extrapolate from local information such as a tangent line. They are fast near the root and can wander off, cycle, or divide by zero away from it.

Key idea: good library routines are hybrids. They take open-method steps when those steps look trustworthy and fall back to bracketing when they do not.

Sign in free to save your progress and earn certificates.

Full lesson text

All 11 steps on one page, for reading, reference, and search.

Show

1. An equation you cannot rearrange

Solve x=cos⁡xx = \cos x. No sequence of algebra isolates xx, and the same is true of Kepler's equation M=E−esin⁡EM = E - e \sin E, of the interest rate hidden inside a loan payment, and of almost every equation a model produces. What a computer can do instead is generate a sequence x0,x1,x2,…x_0, x_1, x_2, \dots that closes in on a number x⋆x^\star with f(x⋆)=0f(x^\star) = 0.

Every root finder in common use belongs to one of two families, and the split decides almost everything about how it behaves:

  • Bracketing methods start from an interval [a,b][a, b] where ff changes sign and never let the root escape it. They cannot fail on a continuous function, and they are slow.
  • Open methods start from a guess and extrapolate from local information such as a tangent line. They are fast near the root and can wander off, cycle, or divide by zero away from it.

Key idea: good library routines are hybrids. They take open-method steps when those steps look trustworthy and fall back to bracketing when they do not.

2. Bisection: a guarantee you can count

If ff is continuous and f(a)f(a), f(b)f(b) have opposite signs, the intermediate value theorem puts a root between them. Evaluate the midpoint, keep whichever half still shows a sign change, and repeat. After kk halvings the midpoint is within

∣xk−x⋆∣≤b−a2k+1|x_k - x^\star| \le \frac{b - a}{2^{k+1}}

of a root. The bound depends on nothing about ff except continuity, which is the whole appeal.

The cost is that the error shrinks by a fixed factor of two per step: linear convergence. One decimal digit needs log⁡210≈3.3\log_2 10 \approx 3.3 halvings, so going from an interval of width 1 to full double precision takes about 52 steps, because IEEE 754 double precision carries a 52-bit fraction and consecutive doubles in [1,2)[1, 2) are 2−522^{-52} apart. After that, the midpoint of two adjacent floats is one of them, and the method has nothing left to do.

In practice: bisection is the method you fall back on, not the one you start with. Its value is that it always terminates with a certified interval.

3. Newton: replace the curve by its tangent

Newton's method treats ff as its tangent line at the current guess and jumps to where that line crosses zero:

xk+1=xk−f(xk)f′(xk)x_{k+1} = x_k - \frac{f(x_k)}{f'(x_k)}

Expanding ff around a simple root (one where f′(x⋆)≠0f'(x^\star) \ne 0) gives the error recursion

ek+1≈f′′(x⋆)2f′(x⋆) ek2,e_{k+1} \approx \frac{f''(x^\star)}{2 f'(x^\star)}\, e_k^2,

the standard result found in Süli and Mayers' An Introduction to Numerical Analysis. Squaring the error roughly doubles the number of correct digits per step, which is quadratic convergence: 3 digits, then 6, then 12, then the limit of the arithmetic.

The price is twofold. You need f′f', which may be expensive or unavailable, and the recursion only holds once eke_k is small. Far from the root the tangent can point anywhere. Newton's method viewed as an optimiser, with damping and Hessians, is covered in the lesson Newton's method and the interior point revolution; here the target is a zero, not a minimum.

4. Secant: Newton without the derivative

Replace f′(xk)f'(x_k) by the slope through the last two iterates:

xk+1=xk−f(xk) xk−xk−1f(xk)−f(xk−1)x_{k+1} = x_k - f(x_k)\,\frac{x_k - x_{k-1}}{f(x_k) - f(x_{k-1})}

The secant method converges with order φ=(1+5)/2≈1.618\varphi = (1 + \sqrt 5)/2 \approx 1.618 near a simple root, again per Süli and Mayers, so it is slower per step than Newton. Per function evaluation it is often faster, because each step costs one new value of ff while Newton costs ff and f′f'.

MethodOrder near a simple rootWork per stepGuaranteed to converge?Needs
Bisection1 (error halves)1 evaluation of ffYesA sign-change bracket
Secantabout 1.6181 evaluation of ffNoTwo starting points
Newton2ff and f′f'NoA derivative and a good guess
Brentsuperlinear when it can, bisection when it must1 evaluation of ffYesA sign-change bracket

The last row is the one libraries default to, and the next steps show why.

5. Watching the digits arrive

All three methods on x2−2=0x^2 - 2 = 0, printing the error after each of six steps:

import math

f  = lambda x: x*x - 2.0
df = lambda x: 2.0*x
root = math.sqrt(2.0)

def bisect(a, b, n):
    errs = []
    for _ in range(n):
        m = 0.5*(a + b)
        if f(a)*f(m) <= 0: b = m
        else: a = m
        errs.append(abs(0.5*(a+b) - root))
    return errs

def newton(x, n):
    errs = []
    for _ in range(n):
        x = x - f(x)/df(x)
        errs.append(abs(x - root))
    return errs

def secant(x0, x1, n):
    errs = []
    for _ in range(n):
        if f(x1) == f(x0): break
        x0, x1 = x1, x1 - f(x1)*(x1 - x0)/(f(x1) - f(x0))
        errs.append(abs(x1 - root))
    return errs

for name, e in [("bisection", bisect(1.0, 2.0, 6)),
                ("secant", secant(1.0, 2.0, 6)),
                ("newton", newton(2.0, 6))]:
    print(f"{name:10s}", " ".join(f"{v:.1e}" for v in e))

Output, as run:

bisection  1.6e-01 3.9e-02 2.3e-02 8.0e-03 7.7e-03 1.5e-04
secant     8.1e-02 1.4e-02 4.2e-04 2.1e-06 3.2e-10 2.2e-16
newton     8.6e-02 2.5e-03 2.1e-06 1.6e-12 0.0e+00 2.2e-16

Bisection's error wobbles because the midpoint can land close to the root by luck, but its bracket halves every time. Newton's exponents go 3, 6, 12: digits doubling. Secant's exponents grow by a factor near 1.6. Both fast methods then sit at 2.2×10−162.2 \times 10^{-16}, the spacing of doubles near 2\sqrt 2; no method can do better than the grid it computes on.

6. How Newton fails

Fast convergence is a local promise. Three ways it breaks, run as written:

from scipy.optimize import brentq, newton

# 1. A cycle: f(x) = x**3 - 2x + 2, started at 0
g, dg = (lambda x: x**3 - 2*x + 2), (lambda x: 3*x**2 - 2)
x = 0.0
path = [x]
for _ in range(4):
    x = x - g(x)/dg(x)
    path.append(x)
print("cycle:", path)

# 2. A double root: f(x) = (x - 1)**2, Newton from 2
h, dh = (lambda x: (x - 1)**2), (lambda x: 2*(x - 1))
x = 2.0
for k in range(1, 6):
    x = x - h(x)/dh(x)
    print(f"double root, step {k}: error {abs(x-1):.4f}")

# 3. Brent's method on the cycling function, with a bracket
r, info = brentq(g, -3, 0, full_output=True)
print("brentq root:", r, "| function calls:", info.function_calls)
cycle: [0.0, 1.0, 0.0, 1.0, 0.0]
double root, step 1: error 0.5000
double root, step 2: error 0.2500
double root, step 3: error 0.1250
double root, step 4: error 0.0625
double root, step 5: error 0.0312
brentq root: -1.7692923542386343 | function calls: 11

Started at 0, Newton on x3−2x+2x^3 - 2x + 2 bounces between 0 and 1 forever, never seeing the real root near −1.769-1.769. At a double root, f′f' vanishes along with ff, and convergence drops to linear with ratio one half. Brent's method, given a bracket, finds the root the cycle missed in 11 evaluations.

Gotcha: a near-zero f′(xk)f'(x_k) sends the next iterate far away. Production Newton codes damp the step or fall back to a bracket, rather than trusting the raw update.

7. Brent's method: the default for one equation

Brent's method keeps a sign-change bracket at all times, like bisection. At each step it proposes a fast step, inverse quadratic interpolation through three points or a secant step through two, and accepts it only if it lands inside the bracket and shrinks it enough. Otherwise it bisects. The result inherits bisection's guarantee and, on smooth functions, the secant method's speed.

SciPy's documentation for brentq calls it "generally considered the best of the rootfinding routines here" and, citing Brent's 1973 book, says convergence is guaranteed for functions computable within the bracket. Its documented requirements and defaults:

  • ff must be continuous and f(a)f(a), f(b)f(b) must have opposite signs.
  • Default xtol=2e-12, and rtol cannot be smaller than 4*np.finfo(float).eps.

In practice: scipy.optimize.newton silently runs the secant method when you pass no derivative, stops on step size, and its documentation says there is no guarantee a root was found. The same notes recommend brentq for one-dimensional problems once a bracket is known.

8. How accurate can a root be?

Suppose ff can only be evaluated with an error of size δ\delta, which is always true in floating point. Near a simple root f(x⋆+Δ)≈f′(x⋆)Δf(x^\star + \Delta) \approx f'(x^\star)\Delta, so every xx with ∣Δ∣≲δ/∣f′(x⋆)∣|\Delta| \lesssim \delta / |f'(x^\star)| looks like a root. A steep crossing is well conditioned; a shallow one is not. At a double root the linear term vanishes and 12f′′(x⋆)Δ2≈δ\tfrac{1}{2} f''(x^\star)\Delta^2 \approx \delta gives Δ≈2δ/∣f′′∣\Delta \approx \sqrt{2\delta / |f''|}. This is conditioning, as defined in Conditioning and Stability: Whose Fault Is the Error, applied to roots.

Predict first

With double precision, about 16 digits, how closely can any root finder pin down the double root of (x−1)2(x-1)^2 when it is computed as x*x - 2*x + 1?

p = lambda x: x*x - 2*x + 1          # (x - 1)**2, expanded
for d in (1e-6, 1e-7, 1e-8, 3e-9, 1e-9):
    print(f"p(1 + {d:.0e}) = {p(1 + d):.3e}")
p(1 + 1e-06) = 1.000e-12
p(1 + 1e-07) = 9.992e-15
p(1 + 1e-08) = 0.000e+00
p(1 + 3e-09) = 0.000e+00
p(1 + 1e-09) = 0.000e+00

9. Stopping: three tests, each covering a different failure

A root finder has to decide when to stop without knowing x⋆x^\star. Each available test catches a different failure, so robust codes combine them.

TestStops whenFooled by
Step size∣xk+1−xk∣≤xtol+rtol ∣xk∣\lvert x_{k+1} - x_k \rvert \le \text{xtol} + \text{rtol}\,\lvert x_k \rvertSlow linear convergence, where small steps still leave a large error
Residual∣f(xk)∣≤ftol\lvert f(x_k) \rvert \le \text{ftol}Scaling: multiply ff by 10610^{6} and the same xkx_k fails; a flat ff passes far from the root
Iteration capkk reaches a maximumNothing, but it only tells you the method did not converge

The step test with a relative term is the usual primary criterion, because it measures what you want, digits in xx, in a scale-free way. The residual test is a sanity check after the fact: plug the answer back in. The cap turns an infinite loop, like the 0 to 1 cycle above, into a reported failure.

Gotcha: an absolute tolerance alone such as xtol=1e-12 is meaningless for a root near 10810^{8}, where doubles are about 1.5×10−81.5 \times 10^{-8} apart and the test can never pass.

10. From one equation to many

For a system F(x)=0F(\mathbf{x}) = \mathbf{0} of nn equations in nn unknowns, brackets no longer exist, since there is no multidimensional intermediate value theorem to lean on. Newton generalises directly: the tangent line becomes the Jacobian matrix JJ, and each step solves a linear system.

J(xk) Δk=−F(xk),xk+1=xk+ΔkJ(\mathbf{x}_k)\,\boldsymbol{\Delta}_k = -F(\mathbf{x}_k), \qquad \mathbf{x}_{k+1} = \mathbf{x}_k + \boldsymbol{\Delta}_k

Two consequences follow. Quadratic convergence survives near a root where JJ is nonsingular. And every iteration now costs a linear solve, which for large nn dominates the cost of evaluating FF. That is why the next two lessons are about solving Ax=bA\mathbf{x} = \mathbf{b}: directly by factorisation, and iteratively when AA is too large to factor.

In practice: SciPy's scipy.optimize.root defaults to MINPACK's hybrid Powell method, which blends Newton steps with a trust region so that a bad Jacobian or a distant starting point does not throw the iterate away.

11. Choosing a root finder

The choice follows from two questions: is there one equation or many, and do you have a bracket or a derivative?

flowchart TD
  A["Need f(x) = 0"] --> B["One equation, sign-change bracket known"]
  A --> C["One equation, no bracket"]
  A --> D["System of n equations"]
  B --> E["Brent: scipy.optimize.brentq"]
  C --> F["Scan or plot to find a bracket"]
  F --> E
  C --> G["Newton or secant from a good guess, then verify"]
  D --> H["Newton with Jacobian solve, damped or trust region"]
  H --> I["Each step is a linear solve: LU or an iterative solver"]

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Bisection starts on an interval of width 1. Roughly how many halvings are needed to shrink the bracket below 1e-6?
    • About 6
    • About 60
    • About 20
    • About 1000
  2. Newton's method converges only linearly on f(x) = (x - 1)^2. Why?
    • The derivative vanishes at the root, so the quadratic error term disappears
    • The function is not continuous at the root
    • Newton's method always converges linearly without a line search
    • Rounding error dominates from the first step
  3. What does scipy.optimize.newton do if you call it without a derivative?
    • It raises an error
    • It runs bisection on [x0 - 1, x0 + 1]
    • It estimates f' with automatic differentiation
    • It runs the secant method
  4. A function can be evaluated only to within an error of 1e-16. Near a simple root its slope is 1e-4. About how precisely can any method locate the root?
    • To about 1e-16
    • To about 1e-12
    • To about 1e-8
    • To about 1e-20
  5. Why does Newton's method for a system of n equations make linear solvers central to its cost?
    • Every step must invert a scalar derivative
    • Every step solves J(x) times the step equals minus F(x)
    • Brackets must be maintained in n dimensions
    • The Jacobian must be diagonal for convergence

Related lessons

Math
advanced

Stiff Equations, Stability Regions, and Adaptive Step Size

Why an ODE solver can blow up at step sizes accuracy would never demand. Learn stability regions on the test equation, what makes a problem stiff, how implicit methods and A-stability fix it at the cost of Newton solves, how BDF and Radau escape Dahlquist's barrier, and how adaptive solvers choose each step.

12 steps·~18 min
Math
advanced

Iterative Solvers: From Jacobi to Preconditioned Conjugate Gradient

When a matrix is too large to factor, solve Ax = b by improving a guess. See why Jacobi stalls on fine grids, how Krylov methods and conjugate gradient use the whole history, why iterations scale with the square root of the condition number, what preconditioning buys, and how to set and check tolerances.

10 steps·~15 min
Math
advanced

Solving Ax = b Directly: LU, Pivoting, and Never Inverting

How a computer actually solves a linear system: Gaussian elimination as an LU factorisation, why you factor once and never invert, how a tiny pivot wrecks a harmless problem, what partial pivoting and the growth factor guarantee, when Cholesky applies, and where fill-in ends the direct approach.

10 steps·~15 min
Math
intermediate

Gradient descent: choosing the step and knowing the rate

Gradient descent is three lines of code and a hundred years of theory. This lesson derives why a safe step size is one over the smoothness constant, why the condition number governs everything, and why acceleration reaching order one over k squared is provably the best any first-order method can do.

11 steps·~17 min