Function roots::find_root_secant [] [src]

pub fn find_root_secant<F: FloatType>(
    first: F,
    second: F,
    f: &Fn(F) -> F,
    convergency: &Convergency<F>
) -> Result<F, SearchError>

Find a root of the function f(x) = 0 using the secant method.

Pro

  • Simple
  • No need for initial bracketing
  • No need for derivative function

Contra

  • Impossible to predict which root will be found when many roots exist
  • Unstable convergency for non-trivial functions
  • Cannot continue when two consecutive iterations have the same value

Failures

ZeroDerivative

Two consecutive points have the same value. Algorithm cannot continue.

NoConvergency

Algorithm cannot find a root within the given number of iterations.

Examples

use roots::SimpleConvergency;
use roots::find_root_secant;

let f = |x| { 1f64*x*x - 1f64 };
let convergency = SimpleConvergency { eps:1e-15f64, max_iter:30 };

let root1 = find_root_secant(10f64, 0f64, &f, &convergency);
// Returns approximately Ok(1);

let root2 = find_root_secant(-10f64, 0f64, &f, &convergency);
// Returns approximately Ok(-1);