[][src]Function bacon_sci::roots::newton

pub fn newton<N: ComplexField>(
    initial: &[N],
    f: fn(_: &[N]) -> DVector<N>,
    f_deriv: fn(_: &[N]) -> DVector<N>,
    tol: <N as ComplexField>::RealField,
    n_max: usize
) -> Result<DVector<N>, String>

Use Newton's method to find a root of a vector function.

Using a vector function and its derivative, find a root based on an initial guess using Newton'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 guess 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

f_deriv Derivative of f

tol tolerance for error between iterations of Newton's method

n_max Maximum number of iterations

Examples

use nalgebra::DVector;
use bacon_sci::roots::newton;
fn cubic(x: &[f64]) -> DVector<f64> {
  DVector::from_iterator(x.len(), x.iter().map(|x| x.powi(3)))
}

fn cubic_deriv(x: &[f64]) -> DVector<f64> {
  DVector::from_iterator(x.len(), x.iter().map(|x| 3.0*x.powi(2)))
}
//...
fn example() {
  let solution = newton(&[0.1], cubic, cubic_deriv, 0.001, 1000).unwrap();
}