{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# Minima and roots of a function\n",
    "\n",
    "Demos finding minima and roots of a function.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define the function\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "x = np.arange(-10, 10, 0.1)\n",
    "\n",
    "\n",
    "def f(x):\n",
    "    return x**2 + 10 * np.sin(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Find minima\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Global minima found [-1.30641113]\n",
      "Local minimum found 3.8374671194983834\n"
     ]
    }
   ],
   "source": [
    "import scipy as sp\n",
    "\n",
    "# Global optimization\n",
    "grid = (-10, 10, 0.1)\n",
    "xmin_global = sp.optimize.brute(f, (grid,))\n",
    "print(f\"Global minima found {xmin_global}\")\n",
    "\n",
    "# Constrain optimization\n",
    "xmin_local = sp.optimize.fminbound(f, 0, 10)\n",
    "print(f\"Local minimum found {xmin_local}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Root finding\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First root found [0.]\n",
      "Second root found [-2.47948183]\n"
     ]
    }
   ],
   "source": [
    "root = sp.optimize.root(f, 1)  # our initial guess is 1\n",
    "print(f\"First root found {root.x}\")\n",
    "root2 = sp.optimize.root(f, -2.5)\n",
    "print(f\"Second root found {root2.x}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot function, minima, and roots\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28876 characters]",
      "text/plain": [
       "<Figure size 600x400 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig = plt.figure(figsize=(6, 4))\n",
    "ax = fig.add_subplot(111)\n",
    "\n",
    "# Plot the function\n",
    "ax.plot(x, f(x), \"b-\", label=\"f(x)\")\n",
    "\n",
    "# Plot the minima\n",
    "xmins = np.array([xmin_global[0], xmin_local])\n",
    "ax.plot(xmins, f(xmins), \"go\", label=\"Minima\")\n",
    "\n",
    "# Plot the roots\n",
    "roots = np.array([root.x, root2.x])\n",
    "ax.plot(roots, f(roots), \"kv\", label=\"Roots\")\n",
    "\n",
    "# Decorate the figure\n",
    "ax.legend(loc=\"best\")\n",
    "ax.set_xlabel(\"x\")\n",
    "ax.set_ylabel(\"f(x)\")\n",
    "ax.axhline(0, color=\"gray\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
