[−][src]Function roots::find_root_secant
pub fn find_root_secant<F, Func>(
first: F,
second: F,
f: Func,
convergency: &mut dyn Convergency<F>
) -> Result<F, SearchError> where
F: FloatType,
Func: Fn(F) -> F,
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 mut convergency = SimpleConvergency { eps:1e-15f64, max_iter:30 }; let root1 = find_root_secant(10f64, 0f64, &f, &mut convergency); // Returns approximately Ok(1); let root2 = find_root_secant(-10f64, 0f64, &f, &mut 1e-15f64); // Returns approximately Ok(-1);