Skip to main content

find_root

Function find_root 

Source
pub fn find_root(
    f: impl FnMut(f64) -> f64,
    a: f64,
    b: f64,
    fa: f64,
    fb: f64,
    tolerance: f64,
) -> Result<f64, RootError>
Expand description

A zero of f in [a, b] by Brent’s method, given f(a) and f(b) of opposite signs (or one of them zero).

Brent’s algorithm (R. P. Brent, Algorithms for Minimization without Derivatives, Prentice-Hall, 1973, ch. 4) combines bisection, the secant rule and inverse quadratic interpolation. It keeps a bracket [b, c] with |f(b)| ≤ |f(c)| and stops when |c − b|/2 ≤ 2ε|b| + tolerance/2. It converges superlinearly on smooth functions and falls back on bisection otherwise.

The result is the end of the final bracket on b’s side of the root: an evaluated point whose sign matches f(b), or a point where f is zero, within about twice the tolerance of the zero. Callers that stop at an event use it to stand past the crossing.

§Errors

RootError: a non-finite value, ends that don’t bracket, a bad tolerance, or no convergence in 500 iterations.