Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Root finding

To solve an equation is to find a root: a value with . Every equation can be put in this form, by moving everything to one side, and very few can be solved by a formula. The orbit of a planet at a given time (Kepler’s equation ), the frequencies at which a beam vibrates (), the interest rate that values a bond, the steady state of a chemical reaction or an electrical circuit, the step of an implicit ODE solver: all are root-finding problems, solved by iteration.

The central questions

  • One variable or many? For a scalar equation, a root can be trapped between two points where has opposite signs, and the trap can be tightened until the root is found to full precision, with a guarantee. For a system of equations in unknowns there is no such trap, and the methods are generalisations of Newton’s.
  • Is a bracket available? If you know and with and of opposite signs, a bracketing method is guaranteed to converge to a sign change, provided is continuous on the bracket. If you know only a starting guess, the faster open methods can converge spectacularly or diverge.
  • Is the derivative available? Newton’s method uses and converges quadratically; the secant method and Brent’s method use only values of and are almost as fast.
  • Is a polynomial? Then all its roots, complex ones included, can be found at once as the eigenvalues of a matrix: see polynomial roots.

The speed of an iterative method is described by its order of convergence: if the errors satisfy , the method has order . Order 1 (linear convergence) gains a fixed number of digits per step; order 2 (quadratic) doubles the number of correct digits per step.

Figure 4.1. Convergence of the root finders.

Choosing a method

Your problemRecommendedEntry
A scalar equation with a known sign changeBrent’s methodBracketing methods
A scalar equation, guaranteed worst caseBisectionBracketing methods
A scalar equation, an interval but no sign change yetBracket by expansion or scan, then BrentBracketing methods
A scalar equation, cheap derivative, good starting guessNewton’s methodNewton and secant
A scalar equation, good starting guess, no derivativeSecant methodNewton and secant
All roots of a polynomialCompanion matrix eigenvaluesPolynomial roots
A system of equations with a JacobianNewton’s method with line searchNonlinear systems
A system, expensive to evaluate, no JacobianBroyden’s methodNonlinear systems
A large system from a discretised PDE (thousands of unknowns or more)Jacobian-free Newton-Krylov, with a preconditionerNonlinear systems

Testing against SciPy

The references come from SciPy 1.17.1 and NumPy 2.4, written by book/generator/data/make_root_finding_refs.py. For scalar equations the table gives each library method’s relative difference from the root found by optimize.brentq; the last column shows how far SciPy’s own six methods (brentq, bisect, ridder, toms748, and newton with and without a derivative) spread among themselves:

Table 4.1. Scalar root finders against SciPy's `brentq`.

equationroot (brentq)BrentbisectionRiddersfalse positionNewtonsecantspread of SciPy’s six methods
cos(x) = x0.739085133215160709e-162e-160009e-16
x^3 - 2x - 5 = 02.094551481542327004e-164e-1602e-166e-164e-16
Kepler: E - 0.9 sin E = 0.31.1035177203030870000008e-166e-16
x e^-x = 0.10.111832559158963003e-151e-161e-1601e-165e-15
ln x + x^2 = 31.592142937058094004e-16001e-1606e-16

Polynomial roots are compared with numpy.roots, which also uses companion-matrix eigenvalues, and systems with MINPACK’s hybrid method in optimize.fsolve:

Table 4.2. Polynomial roots and nonlinear systems against SciPy.

problem (SciPy reference)largest difference in the roots
Wilkinson-like, roots 1 to 10 (numpy.roots)4e-9; from the exact roots, library 3e-10 and NumPy 3e-9, and the library’s Newton-polished real roots 1e-10
x^5 - x - 1 (numpy.roots)3e-15
(x - 1)^2 (x + 2) (x^2 + 1) (numpy.roots)6e-10
degree 12, coefficients 1 to 13 (numpy.roots)1e-15
system: circle and exponential (optimize.fsolve)Newton 0, Broyden 2e-16
system: three trigonometric equations (optimize.fsolve)Newton 1e-16, Broyden 1e-16
system: Broyden tridiagonal, 5 variables (optimize.fsolve)Newton 1e-16, Broyden 1e-16

Roots of polynomials like Wilkinson’s are ill-conditioned. Here the coefficients are integers, exact in double precision, but the rounding inside the companion-matrix eigenvalue computation acts like a relative perturbation of about in them, which moves the roots by up to . The library’s eigenvalues happen to land about ten times closer than NumPy’s here. Newton polishing in polynomial_roots gains only a little more, because near these roots the polynomial itself cannot be evaluated more accurately than that in double precision. The double root of is accurate only to about , which bounds the third row.

In the library

The root finding crate is lib_jsl_root_finding (API reference). The scalar methods are plain functions taking a closure f: Fn(f64) -> f64, the interval or starting points, and a tolerance, and return Result<f64, ErrorsJSL>: an error when the inputs are invalid (no sign change) or the iteration fails. The system solvers take F: Fn(&[f64]) -> Vec<f64> and return a SystemRootResult recording the solution, the residual, the work done and whether it converged.