{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "13dec87c",
            "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": "a1885564",
            "cell_type": "markdown",
            "source": "**Evaluate a function on the points of a 3-D grid**\n\nAs a first example, we evaluate a simple example function on the points of\na 3-D grid:\n",
            "metadata": {}
        },
        {
            "id": "5e1df5b6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.interpolate import RegularGridInterpolator\nimport numpy as np\ndef f(x, y, z):\n    return 2 * x**3 + 3 * y**2 - z\nx = np.linspace(1, 4, 11)\ny = np.linspace(4, 7, 22)\nz = np.linspace(7, 9, 33)\nxg, yg ,zg = np.meshgrid(x, y, z, indexing='ij', sparse=True)\ndata = f(xg, yg, zg)",
            "outputs": []
        },
        {
            "id": "e3dfacb9",
            "cell_type": "markdown",
            "source": "``data`` is now a 3-D array with ``data[i, j, k] = f(x[i], y[j], z[k])``.\nNext, define an interpolating function from this data:\n",
            "metadata": {}
        },
        {
            "id": "199bcf9f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "interp = RegularGridInterpolator((x, y, z), data)",
            "outputs": []
        },
        {
            "id": "2c36bcf0",
            "cell_type": "markdown",
            "source": "Evaluate the interpolating function at the two points\n``(x,y,z) = (2.1, 6.2, 8.3)`` and ``(3.3, 5.2, 7.1)``:\n",
            "metadata": {}
        },
        {
            "id": "2ac00a68",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "pts = np.array([[2.1, 6.2, 8.3],\n                [3.3, 5.2, 7.1]])\ninterp(pts)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([ 125.80469388,  146.30069388])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "16604c65",
            "cell_type": "markdown",
            "source": "which is indeed a close approximation to\n",
            "metadata": {}
        },
        {
            "id": "e52bf63f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "f(2.1, 6.2, 8.3), f(3.3, 5.2, 7.1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(125.54200000000002, 145.894)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "dba51076",
            "cell_type": "markdown",
            "source": "**Interpolate and extrapolate a 2D dataset**\n\nAs a second example, we interpolate and extrapolate a 2D data set:\n",
            "metadata": {}
        },
        {
            "id": "409d9e19",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x, y = np.array([-2, 0, 4]), np.array([-2, 0, 2, 5])\ndef ff(x, y):\n    return x**2 + y**2",
            "outputs": []
        },
        {
            "id": "243c8ab9",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xg, yg = np.meshgrid(x, y, indexing='ij')\ndata = ff(xg, yg)\ninterp = RegularGridInterpolator((x, y), data,\n                                 bounds_error=False, fill_value=None)",
            "outputs": []
        },
        {
            "id": "c5e6997f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import matplotlib.pyplot as plt\nfig = plt.figure()\nax = fig.add_subplot(projection='3d')\nax.scatter(xg.ravel(), yg.ravel(), data.ravel(),\n           s=60, c='k', label='data')",
            "outputs": []
        },
        {
            "id": "43c37f6f",
            "cell_type": "markdown",
            "source": "Evaluate and plot the interpolator on a finer grid\n",
            "metadata": {}
        },
        {
            "id": "70c869ec",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "xx = np.linspace(-4, 9, 31)\nyy = np.linspace(-4, 9, 31)\nX, Y = np.meshgrid(xx, yy, indexing='ij')",
            "outputs": []
        },
        {
            "id": "33a99fc6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# interpolator\nax.plot_wireframe(X, Y, interp((X, Y)), rstride=3, cstride=3,\n                  alpha=0.4, color='m', label='linear interp')",
            "outputs": []
        },
        {
            "id": "5eda3cf6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "# ground truth\nax.plot_wireframe(X, Y, ff(X, Y), rstride=3, cstride=3,\n                  alpha=0.4, label='ground truth')\nplt.legend()\nplt.show()",
            "outputs": []
        },
        {
            "id": "b3d81faa",
            "cell_type": "markdown",
            "source": "Other examples are given\n:ref:`in the tutorial <tutorial-interpolate_regular_grid_interpolator>`.\n",
            "metadata": {}
        }
    ]
}