.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/numerical_analysis/interpolation/plot_01_lagrange_newton.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_numerical_analysis_interpolation_plot_01_lagrange_newton.py: Lagrange vs. Newton divided-difference interpolation ====================================================== There is exactly one polynomial of degree :math:`\leq n` through :math:`n+1` distinct points, so :class:`~mathematicskit.numerical_analysis.systems.interpolation.LagrangeInterpolant` and :class:`~mathematicskit.numerical_analysis.systems.interpolation.NewtonDividedDifference` -- built from completely different representations (a sum of basis polynomials vs. nested divided differences) -- must produce identical curves. Both are also exact for any polynomial up to the interpolation degree, which this script verifies directly. .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import LagrangeInterpolant, NewtonDividedDifference .. GENERATED FROM PYTHON SOURCE LINES 21-23 Same data, two representations --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-44 .. code-block:: Python x = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) y = np.sin(x) + 0.5 * x p_lagrange = LagrangeInterpolant(x, y) p_newton = NewtonDividedDifference(x, y) x_fine = np.linspace(x.min(), x.max(), 300) y_lagrange = p_lagrange.evaluate(x_fine) y_newton = p_newton.evaluate(x_fine) print("max |Lagrange - Newton| over the fine grid:", np.max(np.abs(y_lagrange - y_newton))) fig, ax = plt.subplots(figsize=(6, 4)) ax.plot(x_fine, y_lagrange, color="steelblue", lw=2, label="Lagrange") ax.plot(x_fine, y_newton, "--", color="firebrick", lw=1.5, label="Newton divided-difference") ax.scatter(x, y, color="black", zorder=3, label="nodes") ax.set_title("Two representations of the same interpolating polynomial") ax.legend() fig.tight_layout() .. image-sg:: /api/gallery/numerical_analysis/interpolation/images/sphx_glr_plot_01_lagrange_newton_001.png :alt: Two representations of the same interpolating polynomial :srcset: /api/gallery/numerical_analysis/interpolation/images/sphx_glr_plot_01_lagrange_newton_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none max |Lagrange - Newton| over the fine grid: 8.881784197001252e-16 .. GENERATED FROM PYTHON SOURCE LINES 45-49 Exactness on a polynomial up to the interpolation degree ----------------------------------------------------------- A degree-4 polynomial through 5 nodes should be reproduced to floating-point precision. .. GENERATED FROM PYTHON SOURCE LINES 49-58 .. code-block:: Python x5 = np.linspace(0.0, 4.0, 5) y5 = 2.0 * x5**4 - 3.0 * x5**3 + x5 - 1.0 p5 = NewtonDividedDifference(x5, y5) x_test = np.linspace(0.0, 4.0, 50) expected = 2.0 * x_test**4 - 3.0 * x_test**3 + x_test - 1.0 print("max error vs. exact degree-4 polynomial:", np.max(np.abs(p5.evaluate(x_test) - expected))) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none max error vs. exact degree-4 polynomial: 5.684341886080802e-14 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.044 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_interpolation_plot_01_lagrange_newton.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_lagrange_newton.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_lagrange_newton.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_lagrange_newton.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_