cmlabs.interpolate.newton

cmlabs.interpolate.newton(xvals, x, yvals=None, coef=None)[source]

Newton interpolation polynomial value at \(x\).

\[\begin{split}\begin{gather} L_n(x) = \sum_{i=0}^{n} f(x_0, x_1, \ldots, x_i) \omega_i(x), \\ \omega_0(x) = 1, \quad \omega_1(x) = x - x_0, \quad \omega_i(x) = \prod_{j=0}^{i-1} (x - x_j), i \geq 2 \end{gather}\end{split}\]

where \(f(x_0, x_1, \ldots, x_i)\) is the divided difference of the function at the points \(x_0, x_1, \ldots, x_i\).

Parameters:
  • xvals (array_like, 1-D) – The sorted x-coordinates of the data points.

  • x (float) – The x-coordinate at which to evaluate the polynomial.

  • yvals (array_like, 1-D, optional) – The y-coordinates of the data points, i.e., f(\(x\)). Only used if coef is not provided.

  • coef (array_like, 1-D, optional) – The coefficients of the Newton polynomial. If not provided, the function will compute the divided differences list using the divided_differences function.

Returns:

res – The value of the polynomial at \(x\).

Return type:

float

Notes

The coef parameter is optional and can be used to provide the coefficients of the Newton polynomial. If not provided, the function will compute the divided differences list using the divided_differences function.

The divided differences list has the following structure:

\[[f(x_0), f(x_0, x_1), f(x_0, x_1, x_2), \ldots, f(x_0, x_1, \ldots, x_n)]\]

Use Horner’s method to compute the value of the polynomial at \(x\).

\[\begin{split}\begin{gather} P(x) = a_0 + (x - x_0)(a_1 + (x - x_1)(a_2 + \ldots)) \\ a_0 = f(x_0), \quad a_1 = f(x_0, x_1), \quad a_2 = f(x_0, x_1, x_2), \quad \ldots, \quad a_n = f(x_0, x_1, \ldots, x_n) \end{gather}\end{split}\]

Examples

>>> import numpy as np
>>> from cmlabs.interpolate import newton
>>> xvals = np.array([0, 2, 3, 5])
>>> yvals = np.array([1, 3, 2, 5])
>>> x = np.float32(1.5)
>>> newton(xvals, x, yvals=yvals)
3.3375