scipy 1.8.0 Pypi GitHub Homepage
Other Docs
ParametersReturnsBackRef
lagrange(x, w)

Given two 1-D arrays x and :None:None:`w,` returns the Lagrange interpolating polynomial through the points (x, w) .

Warning: This implementation is numerically unstable. Do not expect to be able to use more than about 20 points even if they are chosen optimally.

Parameters

x : array_like

x represents the x-coordinates of a set of datapoints.

w : array_like

w represents the y-coordinates of a set of datapoints, i.e., f(x).

Returns

lagrange : `numpy.poly1d` instance

The Lagrange interpolating polynomial.

Return a Lagrange interpolating polynomial.

Examples

Interpolate $f(x) = x^3$ by 3 points.

>>> from scipy.interpolate import lagrange
... x = np.array([0, 1, 2])
... y = x**3
... poly = lagrange(x, y)

Since there are only 3 points, Lagrange polynomial has degree 2. Explicitly, it is given by

$$$$
>>> from numpy.polynomial.polynomial import Polynomial
... Polynomial(poly.coef[::-1]).coef array([ 0., -2., 3.])
>>> import matplotlib.pyplot as plt
... x_new = np.arange(0, 2.1, 0.1)
... plt.scatter(x, y, label='data')
... plt.plot(x_new, Polynomial(poly.coef[::-1])(x_new), label='Polynomial')
... plt.plot(x_new, 3*x_new**2 - 2*x_new + 0*x_new,
...  label=r"$3 x^2 - 2 x$", linestyle='-.')
... plt.legend()
... plt.show()
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.interpolate._interpolate.lagrange

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/interpolate/_interpolate.py#25
type: <class 'function'>
Commit: