pub fn lagrange<N: ComplexField + FromPrimitive + Copy>(
xs: &[N],
ys: &[N],
tol: N::RealField,
) -> Result<Polynomial<N>, String>
Expand description
Create a Lagrange interpolating polynomial.
Create an nth degree polynomial matching the n points (xs[i], ys[i]) using Neville’s iterated method for Lagrange polynomials. The result will match no derivatives.
§Examples
use bacon_sci::interp::lagrange;
use bacon_sci::polynomial::Polynomial;
fn example() {
let xs: Vec<_> = (0..10).map(|i| i as f64).collect();
let ys: Vec<_> = xs.iter().map(|x| x.cos()).collect();
let poly = lagrange(&xs, &ys, 1e-6).unwrap();
for x in xs {
assert!((x.cos() - poly.evaluate(x)).abs() < 0.00001);
}
}