[][src]Function bacon_sci::roots::bisection

pub fn bisection<N: RealField>(
    (left, right): (N, N),
    f: fn(_: N) -> N,
    tol: N,
    n_max: usize
) -> Result<N, String>

Use the bisection method to solve for a zero of an equation.

This function takes an interval and uses a binary search to find where in that interval a function has a root. The signs of the function at each end of the interval must be different

Returns

Ok(root) when a root has been found, Err on failure

Params

(left, right) The starting interval. f(left) * f(right) > 0.0

f The function to find the root for

tol The tolerance of the relative error between iterations.

n_max The maximum number of iterations to use.

Examples

use nalgebra::DVector;
use bacon_sci::roots::bisection;

fn cubic(x: f64) -> f64 {
  x*x*x
}
//...
fn example() {
  let solution = bisection((-1.0, 1.0), cubic, 0.001, 1000).unwrap();
}