{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# 1-Dimensional Least Squares Fit\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import ndsplines\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom scipy import interpolate\n\nx = np.linspace(-3, 3, 50)\ny = np.exp(-x**2) + 0.1 * np.random.randn(50)\n\nt = [-1, 0, 1]\nk = 3\nt = np.r_[(x[0],)*(k+1),\n          t,\n          (x[-1],)*(k+1)]\n\nndspl = ndsplines.make_lsq_spline(x[:, None], y[:, None], [t], np.array([k]))\nispl = interpolate.make_lsq_spline(x, y, t, k)\n\nxs = np.linspace(-3, 3, 100)\nplt.figure()\nplt.plot(x, y, 'o', ms=5)\nplt.plot(xs, ndspl(xs).squeeze(), label='LSQ ND spline')\nplt.plot(xs, ispl(xs), '--', label='LSQ scipy.interpolate spline')\nplt.legend(loc='best')\nplt.show()\n\nprint(\"Computed coefficients close?\", np.allclose(ndspl.coefficients, ispl.c))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}