cmlabs.interpolate.lagrange

cmlabs.interpolate.lagrange(xvals, yvals, x)[source]

Lagrange interpolation polynomial value at \(x\).

\[\begin{split}\begin{align*} P_n(x) &= L_n(x) = \sum_{i=0}^{n} l_i(x) f(x_i) \\ l_i(x) &= \prod_{j=0, j \neq i}^{n} \frac{x - x_j}{x_i - x_j} \end{align*}\end{split}\]

where \(l_i(x)\) is the Lagrange basis polynomial for the i-th data point.

Return the value of the Lagrange polynomial that has degree n = len(xvals)-1

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

  • yvals (array_like, 1-D) – The y-coordinates of the data points, i.e., f(\(x\)).

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

Returns:

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

Return type:

float

Examples

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