bisection

Function bisection 

Source
pub fn bisection<N, F>(
    (left, right): (N, N),
    f: F,
    tol: N,
    n_max: usize,
) -> Result<N, String>
where N: RealField + FromPrimitive + Copy, F: FnMut(N) -> N,
Expand description

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 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();
}