
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_auto_examples/plot_boundary_conditions.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download__auto_examples_plot_boundary_conditions.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr__auto_examples_plot_boundary_conditions.py:


Fit boundary conditions
=======================

Show how changing the boundary conditions affect the fit of a 2D spline.

.. GENERATED FROM PYTHON SOURCE LINES 7-12

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np
    import splinebox








.. GENERATED FROM PYTHON SOURCE LINES 13-14

We start by creating some random data points generated from a sin curve with noise.

.. GENERATED FROM PYTHON SOURCE LINES 14-20

.. code-block:: Python


    np.random.seed(4)
    x = np.sort(np.random.rand(25) * 2 * np.pi)
    y = np.sin(x) + np.random.rand(len(x)) * 0.5
    data = np.stack([x, y], axis=-1)








.. GENERATED FROM PYTHON SOURCE LINES 21-23

To be able to compare the different boundary conditions, we create three separate spline objects
and fit each one to the data using a different boundary condition.

.. GENERATED FROM PYTHON SOURCE LINES 23-36

.. code-block:: Python


    M = 5
    basis_function = splinebox.B3()
    closed = False

    free_spline = splinebox.Spline(M, basis_function, closed)
    clamped_spline = splinebox.Spline(M, basis_function, closed)
    natural_spline = splinebox.Spline(M, basis_function, closed)

    free_spline.fit(data, boundary_condition="free")
    clamped_spline.fit(data, boundary_condition="clamped")
    natural_spline.fit(data, boundary_condition="natural")








.. GENERATED FROM PYTHON SOURCE LINES 37-39

We can verify that the first and second derivative are zero for the clamped
and natural boundary condition respectively.

.. GENERATED FROM PYTHON SOURCE LINES 39-48

.. code-block:: Python


    print("First derivative at ends of clamped spline")
    print(clamped_spline(0, derivative=1))
    print(clamped_spline(M - 1, derivative=1))

    print("Second derivative at ends of natural spline")
    print(natural_spline(0, derivative=2))
    print(natural_spline(M - 1, derivative=2))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    First derivative at ends of clamped spline
    [0. 0.]
    [0. 0.]
    Second derivative at ends of natural spline
    [0. 0.]
    [0. 0.]




.. GENERATED FROM PYTHON SOURCE LINES 49-50

Lastly, we plot the splines to see how the splines differ.

.. GENERATED FROM PYTHON SOURCE LINES 50-63

.. code-block:: Python


    t = np.linspace(0, M - 1, 1000)
    free_points = free_spline(t)
    clamped_points = clamped_spline(t)
    natural_points = natural_spline(t)

    plt.scatter(data[:, 0], data[:, 1], color="black")
    plt.plot(free_points[:, 0], free_points[:, 1], label="free", color="blue")
    plt.plot(clamped_points[:, 0], clamped_points[:, 1], label="clamped", color="orange")
    plt.plot(natural_points[:, 0], natural_points[:, 1], label="natural", color="green")
    plt.legend()
    plt.show()




.. image-sg:: /_auto_examples/images/sphx_glr_plot_boundary_conditions_001.png
   :alt: plot boundary conditions
   :srcset: /_auto_examples/images/sphx_glr_plot_boundary_conditions_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 64-67

Zooming in on the right end of the spline,
reveals that the spline with free boundary conditions exhibits an
undesired sharp curve.

.. GENERATED FROM PYTHON SOURCE LINES 67-76

.. code-block:: Python


    plt.scatter(data[:, 0], data[:, 1], color="black")
    plt.plot(free_points[:, 0], free_points[:, 1], label="free", color="blue")
    plt.plot(clamped_points[:, 0], clamped_points[:, 1], label="clamped", color="orange")
    plt.plot(natural_points[:, 0], natural_points[:, 1], label="natural", color="green")
    plt.xlim(5.8, 6.3)
    plt.ylim(-0.2, 0.4)
    plt.legend()
    plt.show()



.. image-sg:: /_auto_examples/images/sphx_glr_plot_boundary_conditions_002.png
   :alt: plot boundary conditions
   :srcset: /_auto_examples/images/sphx_glr_plot_boundary_conditions_002.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.137 seconds)


.. _sphx_glr_download__auto_examples_plot_boundary_conditions.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_boundary_conditions.ipynb <plot_boundary_conditions.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_boundary_conditions.py <plot_boundary_conditions.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_boundary_conditions.zip <plot_boundary_conditions.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
