Path: blob/master/material/LagrangePoly.ipynb
934 views
Kernel: Python 3 (ipykernel)
We will use SymPy
In [3]:
In [4]:
Out[4]:
In [5]:
Out[5]:
In [6]:
Out[6]:
Implementation of the Lagrange interpolating polynomials and Lagrange polynomials in SymPy
In [7]:
Out[7]:
Overwriting LagrangePolynomial.py
In [8]:
Out[8]:
Populating the interactive namespace from numpy and matplotlib
/usr/local/lib/python3.9/dist-packages/IPython/core/magics/pylab.py:159: UserWarning: pylab import has clobbered these variables: ['cos', 'sin']
`%matplotlib` prevents importing * from pylab and numpy
warn("pylab import has clobbered these variables: %s" % clobbered +
Example of interpolation of tree points with a polynomial of degree 2
In [9]:
Out[9]:
Scipy
implementation of the Interpolation polynomial in the Lagrange form
In [11]:
Out[11]:
2
-0.005216 x - 0.1465 x + 8.486
Sympy
implementation of the Interpolation polynomial in the Lagrange form
In [12]:
Out[12]:
With this simpy implementation we can check expliclty that:
a) By using sympy
polynomials: LP.polyL
:
In [13]:
Out[13]:
In [14]:
Out[14]:
Convert to a Python function
In [15]:
In [16]:
Out[16]:
(0.0, 12.0)
Lagrange polynomial properties
$$L_{n,i}(x_i) = 1\,,\qquad\text{and}\,,\qquad L_{n,i}(x_j) = 0\quad\text{for $i\neq jParseError: KaTeX parse error: Expected 'EOF', got '}' at position 1: }̲$
As sympy
objects
In [38]:
Out[38]:
In [39]:
Out[39]:
In [40]:
Out[40]:
In [41]:
Out[41]:
As python
function
In [42]:
Out[42]:
0.9999999999999968 0.9999999999999951 0.9999999999999998
In [43]:
Out[43]:
0 1.000000e+00
1 -2.220446e-16
2 5.551115e-15
Name: X, dtype: float64
In [44]:
Out[44]:
0 -4.440892e-16
1 1.000000e+00
2 -2.042810e-14
Name: X, dtype: float64
In [45]:
Out[45]:
0 1.665335e-16
1 0.000000e+00
2 1.000000e+00
Name: X, dtype: float64
Actividad Fit a cuatro puntos, comprobando las propiedades del polinomio de Lagrange
In [ ]: