.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/numerical_analysis/splines/plot_01_cubic_spline.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_splines_plot_01_cubic_spline.py: Natural and clamped cubic splines ==================================== A cubic spline is a piecewise cubic through the data with continuous value, first, and second derivatives across nodes. A **natural** spline sets the curvature to zero at both ends; a **clamped** spline instead matches prescribed end-slopes. This script fits both to the same data and compares them against a single high-degree interpolating polynomial, which oscillates far more between nodes -- the practical motivation for splines over one global polynomial. .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.numerical_analysis import CubicSpline, LagrangeInterpolant .. GENERATED FROM PYTHON SOURCE LINES 21-23 Fit natural and clamped splines to the same data ---------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-43 .. code-block:: Python x = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -0.9, 0.0]) natural = CubicSpline(x, y, boundary="natural") clamped = CubicSpline(x, y, boundary="clamped", fpa=0.5, fpb=0.5) global_poly = LagrangeInterpolant(x, y) x_fine = np.linspace(x.min(), x.max(), 400) fig, ax = plt.subplots(figsize=(7, 4.5)) ax.plot(x_fine, natural.evaluate(x_fine), color="steelblue", label="natural spline") ax.plot(x_fine, clamped.evaluate(x_fine), color="seagreen", label="clamped spline") ax.plot(x_fine, global_poly.evaluate(x_fine), "--", color="firebrick", label="degree-6 global polynomial") ax.scatter(x, y, color="black", zorder=3, label="nodes") ax.set_ylim(-3, 3) ax.set_title("Splines stay well-behaved; one global polynomial can oscillate") ax.legend(fontsize=8) fig.tight_layout() .. image-sg:: /api/gallery/numerical_analysis/splines/images/sphx_glr_plot_01_cubic_spline_001.png :alt: Splines stay well-behaved; one global polynomial can oscillate :srcset: /api/gallery/numerical_analysis/splines/images/sphx_glr_plot_01_cubic_spline_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-46 Natural boundary condition: zero curvature at both ends ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-51 .. code-block:: Python second_derivs = natural.second_derivative_at_nodes() print("S''(x_0), S''(x_n) for the natural spline:", second_derivs[0], second_derivs[-1]) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none S''(x_0), S''(x_n) for the natural spline: 0.0 8.881784197001252e-16 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.038 seconds) .. _sphx_glr_download_api_gallery_numerical_analysis_splines_plot_01_cubic_spline.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_cubic_spline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_cubic_spline.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_cubic_spline.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_