secant

Function secant 

Source
pub fn secant<N, F, const S: usize>(
    initial: &[N],
    func: F,
    h: <N as ComplexField>::RealField,
    tol: <N as ComplexField>::RealField,
    n_max: usize,
) -> Result<SVector<N, S>, String>
where N: ComplexField + FromPrimitive + Copy, <N as ComplexField>::RealField: FromPrimitive + Copy, F: FnMut(&[N]) -> SVector<N, S>, Const<S>: DimMin<Const<S>, Output = Const<S>>,
Expand description

Use secant method to find a root of a vector function.

Using a vector function and its derivative, find a root based on an initial guess and finite element differences using Broyden’s method.

§Returns

Ok(vec) on success, where vec is a vector input for which the function is zero. Err on failure.

§Params

initial Initial guesses of the root. Should be near actual root. Slice since this function finds roots of vector functions.

f Vector function for which to find the root

tol tolerance for error between iterations of Newton’s method

n_max Maximum number of iterations

§Examples

use nalgebra::SVector;
use bacon_sci::roots::secant;
fn cubic(x: &[f64]) -> SVector<f64, 1> {
  SVector::<f64, 1>::from_iterator(x.iter().map(|x| x.powi(3)))
}
//...
fn example() {
  let solution = secant(&[0.1], cubic, 0.1, 0.001, 1000).unwrap();
}