{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "d54de201",
            "cell_type": "markdown",
            "source": "<div class='alert alert-warning'>\n\nSciPy's interactive examples with Jupyterlite are experimental and may not always work as expected. Execution of cells containing imports may result in large downloads (up to 60MB of content for the first import from SciPy). Load times when importing from SciPy may take roughly 10-20 seconds. If you notice any problems, feel free to open an [issue](https://github.com/scipy/scipy/issues/new/choose).\n\n</div>",
            "metadata": {}
        },
        {
            "id": "40d4b640",
            "cell_type": "markdown",
            "source": "In this example the cubic spline is used to interpolate a sampled sinusoid.\nYou can see that the spline continuity property holds for the first and\nsecond derivatives and violates only for the third derivative.\n",
            "metadata": {}
        },
        {
            "id": "0cf40177",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy.interpolate import CubicSpline\nimport matplotlib.pyplot as plt\nx = np.arange(10)\ny = np.sin(x)\ncs = CubicSpline(x, y)\nxs = np.arange(-0.5, 9.6, 0.1)\nfig, ax = plt.subplots(figsize=(6.5, 4))\nax.plot(x, y, 'o', label='data')\nax.plot(xs, np.sin(xs), label='true')\nax.plot(xs, cs(xs), label=\"S\")\nax.plot(xs, cs(xs, 1), label=\"S'\")\nax.plot(xs, cs(xs, 2), label=\"S''\")\nax.plot(xs, cs(xs, 3), label=\"S'''\")\nax.set_xlim(-0.5, 9.5)\nax.legend(loc='lower left', ncol=2)\nplt.show()",
            "outputs": []
        },
        {
            "id": "920a6418",
            "cell_type": "markdown",
            "source": "In the second example, the unit circle is interpolated with a spline. A\nperiodic boundary condition is used. You can see that the first derivative\nvalues, ds/dx=0, ds/dy=1 at the periodic point (1, 0) are correctly\ncomputed. Note that a circle cannot be exactly represented by a cubic\nspline. To increase precision, more breakpoints would be required.\n",
            "metadata": {}
        },
        {
            "id": "7492e4be",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "theta = 2 * np.pi * np.linspace(0, 1, 5)\ny = np.c_[np.cos(theta), np.sin(theta)]\ncs = CubicSpline(theta, y, bc_type='periodic')\nprint(\"ds/dx={:.1f} ds/dy={:.1f}\".format(cs(0, 1)[0], cs(0, 1)[1]))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "ds/dx=0.0 ds/dy=1.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "fafeb346",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xs = 2 * np.pi * np.linspace(0, 1, 100)\nfig, ax = plt.subplots(figsize=(6.5, 4))\nax.plot(y[:, 0], y[:, 1], 'o', label='data')\nax.plot(np.cos(xs), np.sin(xs), label='true')\nax.plot(cs(xs)[:, 0], cs(xs)[:, 1], label='spline')\nax.axes.set_aspect('equal')\nax.legend(loc='center')\nplt.show()",
            "outputs": []
        },
        {
            "id": "e3fcd931",
            "cell_type": "markdown",
            "source": "The third example is the interpolation of a polynomial y = x**3 on the\ninterval 0 <= x<= 1. A cubic spline can represent this function exactly.\nTo achieve that we need to specify values and first derivatives at\nendpoints of the interval. Note that y' = 3 * x**2 and thus y'(0) = 0 and\ny'(1) = 3.\n",
            "metadata": {}
        },
        {
            "id": "b6fc92a4",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "cs = CubicSpline([0, 1], [0, 1], bc_type=((1, 0), (1, 3)))\nx = np.linspace(0, 1)\nnp.allclose(x**3, cs(x))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}