pub fn newton<N, F, G, const S: usize>(
initial: &[N],
f: F,
jac: G,
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>,
G: FnMut(&[N]) -> SMatrix<N, S, S>,
Const<S>: DimMin<Const<S>, Output = Const<S>>,Expand description
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::{SVector, SMatrix};
use bacon_sci::roots::newton;
fn cubic(x: &[f64]) -> SVector<f64, 1> {
SVector::<f64, 1>::from_iterator(x.iter().map(|x| x.powi(3)))
}
fn cubic_deriv(x: &[f64]) -> SMatrix<f64, 1, 1> {
SMatrix::<f64, 1, 1>::from_iterator(x.iter().map(|x| 3.0*x.powi(2)))
}
//...
fn example() {
let solution = newton(&[0.1], cubic, cubic_deriv, 0.001, 1000).unwrap();
}