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 problem | Recommended | Entry |
|---|---|---|
| A scalar equation with a known sign change | Brent’s method | Bracketing methods |
| A scalar equation, guaranteed worst case | Bisection | Bracketing methods |
| A scalar equation, an interval but no sign change yet | Bracket by expansion or scan, then Brent | Bracketing methods |
| A scalar equation, cheap derivative, good starting guess | Newton’s method | Newton and secant |
| A scalar equation, good starting guess, no derivative | Secant method | Newton and secant |
| All roots of a polynomial | Companion matrix eigenvalues | Polynomial roots |
| A system of equations with a Jacobian | Newton’s method with line search | Nonlinear systems |
| A system, expensive to evaluate, no Jacobian | Broyden’s method | Nonlinear systems |
| A large system from a discretised PDE (thousands of unknowns or more) | Jacobian-free Newton-Krylov, with a preconditioner | Nonlinear 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`.
| equation | root (brentq) | Brent | bisection | Ridders | false position | Newton | secant | spread of SciPy’s six methods |
|---|---|---|---|---|---|---|---|---|
| cos(x) = x | 0.7390851332151607 | 0 | 9e-16 | 2e-16 | 0 | 0 | 0 | 9e-16 |
| x^3 - 2x - 5 = 0 | 2.0945514815423270 | 0 | 4e-16 | 4e-16 | 0 | 2e-16 | 6e-16 | 4e-16 |
| Kepler: E - 0.9 sin E = 0.3 | 1.1035177203030870 | 0 | 0 | 0 | 0 | 0 | 8e-16 | 6e-16 |
| x e^-x = 0.1 | 0.1118325591589630 | 0 | 3e-15 | 1e-16 | 1e-16 | 0 | 1e-16 | 5e-15 |
| ln x + x^2 = 3 | 1.5921429370580940 | 0 | 4e-16 | 0 | 0 | 1e-16 | 0 | 6e-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.