{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "6658ad79",
            "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": "dab7a79d",
            "cell_type": "markdown",
            "source": "Use the trapezoidal rule on evenly spaced points:\n",
            "metadata": {}
        },
        {
            "id": "57e922bc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nfrom scipy import integrate\nintegrate.trapezoid([1, 2, 3])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "4.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "65fb6805",
            "cell_type": "markdown",
            "source": "The spacing between sample points can be selected by either the\n``x`` or ``dx`` arguments:\n",
            "metadata": {}
        },
        {
            "id": "26111d75",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "integrate.trapezoid([1, 2, 3], x=[4, 6, 8])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "8.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "7bbba75e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "integrate.trapezoid([1, 2, 3], dx=2)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "8.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "274f7b45",
            "cell_type": "markdown",
            "source": "Using a decreasing ``x`` corresponds to integrating in reverse:\n",
            "metadata": {}
        },
        {
            "id": "2f27a47d",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "integrate.trapezoid([1, 2, 3], x=[8, 6, 4])",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "-8.0"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "8f64a521",
            "cell_type": "markdown",
            "source": "More generally ``x`` is used to integrate along a parametric curve. We can\nestimate the integral $\\int_0^1 x^2 = 1/3$ using:\n",
            "metadata": {}
        },
        {
            "id": "53641a3e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "x = np.linspace(0, 1, num=50)\ny = x**2\nintegrate.trapezoid(y, x)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "0.33340274885464394"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "464c18ee",
            "cell_type": "markdown",
            "source": "Or estimate the area of a circle, noting we repeat the sample which closes\nthe curve:\n",
            "metadata": {}
        },
        {
            "id": "8a94882f",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "theta = np.linspace(0, 2 * np.pi, num=1000, endpoint=True)\nintegrate.trapezoid(np.cos(theta), x=np.sin(theta))",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "3.141571941375841"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "49c241dd",
            "cell_type": "markdown",
            "source": "``trapezoid`` can be applied along a specified axis to do multiple\ncomputations in one call:\n",
            "metadata": {}
        },
        {
            "id": "c04428a8",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "a = np.arange(6).reshape(2, 3)\na",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([[0, 1, 2],\n       [3, 4, 5]])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "e5ad4987",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "integrate.trapezoid(a, axis=0)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1.5, 2.5, 3.5])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "cfea075c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "integrate.trapezoid(a, axis=1)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([2.,  8.])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}