{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# A demo of 1D interpolation\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37492 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 38256 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 57964 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Generate data\n",
    "import numpy as np\n",
    "\n",
    "rng = np.random.default_rng(27446968)\n",
    "measured_time = np.linspace(0, 2 * np.pi, 20)\n",
    "function = np.sin(measured_time)\n",
    "noise = rng.normal(loc=0, scale=0.1, size=20)\n",
    "measurements = function + noise\n",
    "\n",
    "# Smooth the curve and interpolate at new times\n",
    "import scipy as sp\n",
    "\n",
    "smoothing_spline = sp.interpolate.make_smoothing_spline(measured_time, measurements)\n",
    "interpolation_time = np.linspace(0, 2 * np.pi, 200)\n",
    "smooth_results = smoothing_spline(interpolation_time)\n",
    "\n",
    "# Plot the data, the interpolant, and the original function\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.figure(figsize=(6, 4))\n",
    "plt.plot(measured_time, measurements, \".\", ms=6, label=\"measurements\")\n",
    "plt.plot(interpolation_time, smooth_results, label=\"smoothing spline\")\n",
    "plt.plot(interpolation_time, np.sin(interpolation_time), \"--\", label=\"underlying curve\")\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "# Fit the data exactly\n",
    "interp_spline = sp.interpolate.make_interp_spline(measured_time, function)\n",
    "interp_results = interp_spline(interpolation_time)\n",
    "\n",
    "# Plot the data, the interpolant, and the original function\n",
    "plt.figure(figsize=(6, 4))\n",
    "plt.plot(measured_time, function, \".\", ms=6, label=\"measurements\")\n",
    "plt.plot(interpolation_time, interp_results, label=\"interpolating spline\")\n",
    "plt.plot(interpolation_time, np.sin(interpolation_time), \"--\", label=\"underlying curve\")\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "# Plot interpolant, its derivative, and its antiderivative\n",
    "plt.figure(figsize=(6, 4))\n",
    "t = interpolation_time\n",
    "plt.plot(t, interp_spline(t), label=\"spline\")\n",
    "plt.plot(t, interp_spline.derivative()(t), label=\"derivative\")\n",
    "plt.plot(t, interp_spline.antiderivative()(t) - 1, label=\"antiderivative\")\n",
    "\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
