.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_examples_1d-lsq.py:
===============================
1-Dimensional Least Squares Fit
===============================
.. image:: /auto_examples/images/sphx_glr_1d-lsq_001.png
:alt: 1d lsq
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
Computed coefficients close? False
|
.. code-block:: default
:lineno-start: 7
import ndsplines
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
x = np.linspace(-3, 3, 50)
y = np.exp(-x**2) + 0.1 * np.random.randn(50)
t = [-1, 0, 1]
k = 3
t = np.r_[(x[0],)*(k+1),
t,
(x[-1],)*(k+1)]
ndspl = ndsplines.make_lsq_spline(x[:, None], y[:, None], [t], np.array([k]))
ispl = interpolate.make_lsq_spline(x, y, t, k)
xs = np.linspace(-3, 3, 100)
plt.figure()
plt.plot(x, y, 'o', ms=5)
plt.plot(xs, ndspl(xs).squeeze(), label='LSQ ND spline')
plt.plot(xs, ispl(xs), '--', label='LSQ scipy.interpolate spline')
plt.legend(loc='best')
plt.show()
print("Computed coefficients close?", np.allclose(ndspl.coefficients, ispl.c))
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.181 seconds)
.. _sphx_glr_download_auto_examples_1d-lsq.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: 1d-lsq.py <1d-lsq.py>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: 1d-lsq.ipynb <1d-lsq.ipynb>`
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_