{
    "nbformat": 4,
    "nbformat_minor": 5,
    "metadata": {
        "kernelspec": {
            "display_name": "Python",
            "language": "python",
            "name": "python"
        },
        "language_info": {
            "name": "python"
        }
    },
    "cells": [
        {
            "id": "59f2cf72",
            "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": "ede98d31",
            "cell_type": "markdown",
            "source": "Example 1: seek the minimum value of the expression\n``a*u**2 + b*u*v + c*v**2 + d*u + e*v + f`` for given values\nof the parameters and an initial guess ``(u, v) = (0, 0)``.\n",
            "metadata": {}
        },
        {
            "id": "761fe948",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "import numpy as np\nargs = (2, 3, 7, 8, 9, 10)  # parameter values\ndef f(x, *args):\n    u, v = x\n    a, b, c, d, e, f = args\n    return a*u**2 + b*u*v + c*v**2 + d*u + e*v + f\ndef gradf(x, *args):\n    u, v = x\n    a, b, c, d, e, f = args\n    gu = 2*a*u + b*v + d     # u-component of the gradient\n    gv = b*u + 2*c*v + e     # v-component of the gradient\n    return np.asarray((gu, gv))\nx0 = np.asarray((0, 0))  # Initial guess.\nfrom scipy import optimize\nres1 = optimize.fmin_cg(f, x0, fprime=gradf, args=args)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Optimization terminated successfully.\n         Current function value: 1.617021\n         Iterations: 4\n         Function evaluations: 8\n         Gradient evaluations: 8"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "fe6fce6e",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res1",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([-1.80851064, -0.25531915])"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "2ed6a6e3",
            "cell_type": "markdown",
            "source": "Example 2: solve the same problem using the `minimize` function.\n(This `myopts` dictionary shows all of the available options,\nalthough in practice only non-default values would be needed.\nThe returned value will be a dictionary.)\n",
            "metadata": {}
        },
        {
            "id": "2605db01",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "opts = {'maxiter' : None,    # default value.\n        'disp' : True,    # non-default value.\n        'gtol' : 1e-5,    # default value.\n        'norm' : np.inf,  # default value.\n        'eps' : 1.4901161193847656e-08}  # default value.\nres2 = optimize.minimize(f, x0, jac=gradf, args=args,\n                         method='CG', options=opts)",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "Optimization terminated successfully.\n        Current function value: 1.617021\n        Iterations: 4\n        Function evaluations: 8\n        Gradient evaluations: 8"
                    },
                    "execution_count": null
                }
            ]
        },
        {
            "id": "05cd2775",
            "cell_type": "code",
            "metadata": {},
            "execution_count": null,
            "source": "res2.x  # minimum found",
            "outputs": [
                {
                    "output_type": "execute_result",
                    "metadata": {},
                    "data": {
                        "text/plain": "array([-1.80851064, -0.25531915])"
                    },
                    "execution_count": null
                }
            ]
        }
    ]
}