{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f8630bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9751d328",
   "metadata": {},
   "source": [
    "# Floating Point\n",
    "\n",
    "**Double Precision Floating Point Number Representation**\n",
    "\n",
    "$$(-1)^s \\times 2^{c-1023} \\times (1+f)$$\n",
    "\n",
    "* $s$: sign bit\n",
    "* $c$: 11 bit exponent\n",
    "* $f$: 52 bit fraction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c0b5413",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "964d247f",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6f7aef85",
   "metadata": {},
   "source": [
    "# Root Finding\n",
    "\n",
    "When there is no or hard to find analytic solution.\n",
    "\n",
    "* General procedure of root finding\n",
    "\n",
    "\n",
    "## Incremental Search\n",
    "\n",
    "Detecting a sign change in the interval $[x, dx]$\n",
    "\n",
    "\n",
    "## Bracketing Method\n",
    "\n",
    "* there must be a solution in a given interval $[a,b]$\n",
    "* reducing the interval systematically\n",
    "* [Merit]Convergence to an exact solution\n",
    "\n",
    "Methods:\n",
    "* bisection\n",
    "* linear interpolation\n",
    "\n",
    "## Open Method\n",
    "\n",
    "* starting from an arbitrary point, find the root based on a regular iteration\n",
    "* [Risk] divergence\n",
    "* [Merit] faster than _bracketing_\n",
    "\n",
    "Methods:\n",
    "* fixed-point iter\n",
    "* Newton-Raphson method\n",
    "* secant method\n",
    "* Muller method\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2649c3d4",
   "metadata": {},
   "source": [
    "## Bisection Method\n",
    "based on the principle: **A solution must exists in $[x_1, x_2]$ if $f(x_1)f(x_2) < 0$**\n",
    "\n",
    "so interval reduces by half at each iteration.\n",
    "\n",
    "* simple\n",
    "* maximum error $<$ (initial interval/$2^n$)\n",
    "* slow converge\n",
    "* cannot cope with multiple roots"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef80f29d",
   "metadata": {},
   "source": [
    "## Linear Interpolation Method(False Position Method)\n",
    "\n",
    "An interval $[a_{n+1}, b_{n+1}]$, containing an approximation of $f(x) = 0$, is found from an interval $[a_n, b_n]$\n",
    "\n",
    "$$p_{n+1} = a_n - \\frac{f(a_n)(b_n-a_n)}{f(b_n) - f(a_n)}$$\n",
    "\n",
    "then set $$\\begin{cases}\n",
    "a_{n+1} = a_n, b_{n+1} = p_{n+1} &\\quad \\text{if} f(a_n)f(p_{n+1}) < 0\\\\\n",
    "a_{n+1} = p_{n+1}, b_{n+1} = b_n &\\quad \\text{else}\n",
    "\\end{cases}$$\n",
    "\n",
    "* faster convergence than bisection\n",
    "* convergence speed depends on **curvature**\n",
    "\n",
    "\n",
    "\n",
    "### Modified Linear Interpolation\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "856f5bbf",
   "metadata": {},
   "source": [
    "## Newton-Raphson Method\n",
    "\n",
    "\n",
    "* **fast convergence**: quadratic convergence\n",
    "* Inefficient if **the derivative calculation is complex**\n",
    "* Initial guess matter\n",
    "  * Cycling\n",
    "  * Wandering\n",
    "  * Overshooting(a root the methods get is not the root I wanted)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf78e848",
   "metadata": {},
   "source": [
    "## Secant Method\n",
    "\n",
    "$$p_{n+1} = p_n - \\frac{f(p_n)(p_n - p_{n-1})}{f(p_n) - f(p_{n-1})}$$\n",
    "\n",
    "Similar to linear interpolation but update rule is different.\n",
    "\n",
    "* fast convergence than **linear interpolation**\n",
    "* more stable than Newton-Raphson\n",
    "  * but convergence is slower than Newton-Raphson"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "167cae2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36440 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.linspace(0, 4)\n",
    "\n",
    "f = lambda t: t**2 - 5\n",
    "y = f(x)\n",
    "\n",
    "p = [0.5, 3.5]\n",
    "\n",
    "for _ in range(2):\n",
    "    p.append(p[-1] - f(p[-1]) * (p[-1] - p[-2]) / (f(p[-1]) - f(p[-2])))\n",
    "\n",
    "p = np.array(p)\n",
    "plt.plot(\n",
    "    p,\n",
    "    np.zeros_like(p),\n",
    "    \"ro\",\n",
    ")\n",
    "for i, p0 in enumerate(p):\n",
    "    plt.text(p0 + 0.05, 0.1, f\"$p_{i}$\", size=16, color=\"black\", va=\"bottom\")\n",
    "\n",
    "plt.plot(p, f(p), \"ro\")\n",
    "\n",
    "for pos in p:\n",
    "    _y = [0, f(pos)]\n",
    "    plt.vlines(pos, min(_y), max(_y), color=\"red\")\n",
    "\n",
    "for p0, p1 in zip(p[:-1], p[1:]):\n",
    "    plt.plot([p0, p1], [f(p0), f(p1)], color=\"green\")\n",
    "\n",
    "plt.title(\"Secant Method\")\n",
    "plt.plot(x, y, color=\"blue\")\n",
    "plt.hlines(0, 0, 4, color=\"black\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17e94fbf",
   "metadata": {},
   "source": [
    "## Fixed Point Iteration\n",
    "\n",
    "principle: $f(x) = 0 \\to x = g(x)$\n",
    "\n",
    "$$x_{k+1} = g(x_k)$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cc731ad",
   "metadata": {},
   "source": [
    "## Muller Method"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3702bdd",
   "metadata": {},
   "source": [
    "## Error Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0e38d45",
   "metadata": {},
   "source": [
    "## Accelerating Convergence\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e7717a7",
   "metadata": {},
   "source": [
    "## Fail-safe Methods\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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": 5
}
