{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "d1c2118e",
            "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": "e34013e7",
            "cell_type": "markdown",
            "source": "Suppose we wish to find the root of the following function.\n",
            "metadata": {}
        },
        {
            "id": "4a666ae6",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "def f(x, c=5):\n    return x**3 - 2*x - c",
            "outputs": []
        },
        {
            "id": "199ce7e7",
            "cell_type": "markdown",
            "source": "First, we must find a valid bracket. The function is not monotonic,\nbut `bracket_root` may be able to provide a bracket.\n",
            "metadata": {}
        },
        {
            "id": "e464befe",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "from scipy.optimize import elementwise\nres_bracket = elementwise.bracket_root(f, 0)\nres_bracket.success",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "True"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1ccce00e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res_bracket.bracket",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(2.0, 4.0)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "1d1e3ad3",
            "cell_type": "markdown",
            "source": "Indeed, the values of the function at the bracket endpoints have\nopposite signs.\n",
            "metadata": {}
        },
        {
            "id": "be1bbb1b",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res_bracket.f_bracket",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(-1.0, 51.0)"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "aa87e5dc",
            "cell_type": "markdown",
            "source": "Once we have a valid bracket, `find_root` can be used to provide\na precise root.\n",
            "metadata": {}
        },
        {
            "id": "f525435c",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res_root = elementwise.find_root(f, res_bracket.bracket)\nres_root.x",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "2.0945514815423265"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "5cf2addf",
            "cell_type": "markdown",
            "source": "`bracket_root` and `find_root` accept arrays for most arguments.\nFor instance, to find the root for a few values of the parameter ``c``\nat once:\n",
            "metadata": {}
        },
        {
            "id": "55b13adc",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nc = np.asarray([3, 4, 5])\nres_bracket = elementwise.bracket_root(f, 0, args=(c,))\nres_bracket.bracket",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "(array([1., 1., 2.]), array([2., 2., 4.]))"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "59f1d0a0",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res_root = elementwise.find_root(f, res_bracket.bracket, args=(c,))\nres_root.x",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([1.8932892 , 2.        , 2.09455148])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}