.. 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_02_hermite_interpolation.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_02_hermite_interpolation.py: Hermite interpolation: matching slopes as well as values =========================================================== Charles Hermite's 1878 interpolant matches both :math:`f(x_i)` and :math:`f'(x_i)`, so :math:`n + 1` nodes fix a polynomial of degree :math:`2n + 1`. On the same few nodes it follows :math:`\sin` far more closely than the Lagrange interpolant, which knows only the values. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import HermiteInterpolant, LagrangeInterpolant .. GENERATED FROM PYTHON SOURCE LINES 18-20 Four nodes on one period of :math:`\sin x` ---------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-45 .. code-block:: Python x = np.linspace(0.0, 2.0 * np.pi, 4) hermite = HermiteInterpolant(x, np.sin(x), dydx=np.cos(x)) lagrange = LagrangeInterpolant(x, np.sin(x)) t = np.linspace(0.0, 2.0 * np.pi, 400) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4.2)) ax1.plot(t, np.sin(t), "k", lw=2, label=r"$\sin x$") ax1.plot(t, hermite(t), "--", color="firebrick", label="Hermite (degree 7)") ax1.plot(t, lagrange(t), ":", color="steelblue", label="Lagrange (degree 3)") ax1.scatter(x, np.sin(x), color="black", zorder=3) for xi in x: ax1.plot([xi - 0.4, xi + 0.4], [np.sin(xi) - 0.4 * np.cos(xi), np.sin(xi) + 0.4 * np.cos(xi)], color="gray", lw=1) ax1.set_title("Values and slopes at the nodes") ax1.legend(fontsize=8) ax2.semilogy(t, np.abs(hermite(t) - np.sin(t)) + 1e-17, color="firebrick", label="Hermite") ax2.semilogy(t, np.abs(lagrange(t) - np.sin(t)) + 1e-17, color="steelblue", label="Lagrange") ax2.set_title("Absolute error") ax2.legend(fontsize=8) fig.tight_layout() print(f"max error: Hermite {np.max(np.abs(hermite(t) - np.sin(t))):.2e}, Lagrange {np.max(np.abs(lagrange(t) - np.sin(t))):.2e}") plt.show() .. image-sg:: /api/gallery/numerical_analysis/interpolation/images/sphx_glr_plot_02_hermite_interpolation_001.png :alt: Values and slopes at the nodes, Absolute error :srcset: /api/gallery/numerical_analysis/interpolation/images/sphx_glr_plot_02_hermite_interpolation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none max error: Hermite 1.88e-03, Lagrange 2.55e-01 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.081 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_interpolation_plot_02_hermite_interpolation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_hermite_interpolation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_hermite_interpolation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_hermite_interpolation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_