{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Newton method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Plots.PlotlyBackend()"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Plots\n",
    "plotly()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Newton-Raphson in optimization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Newton-Raphson iteration\n",
    "$$\n",
    "x_{k+1} = x_k-\\frac{f'(x_k)}{f''(x_k)}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Newton (generic function with 5 methods)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function Newton(f::Function, df::Function, d2f:: Function,\n",
    "        xstart::Float64, verbose::Bool = false, store=false,\n",
    "        δ::Float64 = 1e-6, nmax::Int64 = 1000)\n",
    "    k = 1\n",
    "    x = xstart\n",
    "    if (store)\n",
    "        iter = [ f(x) x ]\n",
    "    end\n",
    "    if (verbose)\n",
    "        fx = f(x)\n",
    "        println(\"$k. x = $x, f(x) = $fx\")\n",
    "    end\n",
    "    dfx = df(x)\n",
    "    while (abs(dfx) > δ && k < nmax)\n",
    "        k += 1\n",
    "        dfx = df(x)\n",
    "        x = x-dfx/d2f(x)\n",
    "        if (store)\n",
    "            iter = [iter ; f(x) x]\n",
    "        end\n",
    "        if (verbose)\n",
    "            fx = f(x)\n",
    "            println(\"$k. x = $x, f(x) = $fx\")\n",
    "        end\n",
    "    end\n",
    "    \n",
    "    if (store)\n",
    "        return iter\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "d2func (generic function with 1 method)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func(x) = -10x^2 + 4sin(x) + x^4\n",
    "dfunc(x) = -20x + 4cos(x) + 4x^3\n",
    "d2func(x) = -20 - 4sin(x) + 12x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "    <script type=\"text/javascript\">\n",
       "        requirejs([\"https://cdn.plot.ly/plotly-latest.min.js\"], function(p) {\n",
       "            window.Plotly = p\n",
       "        });\n",
       "    </script>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "y1",
         "line": {
          "color": "rgba(0, 154, 250, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "y1",
         "showlegend": true,
         "text": [
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -3.9804683619604235,
          -3.9609367239208475,
          -3.9414050858812715,
          -3.921873447841695,
          -3.8315743496931103,
          -3.7412752515445256,
          -3.650976153395941,
          -3.560677055247356,
          -3.4703779570987714,
          -3.3800788589501867,
          -3.2897797608016024,
          -3.1994806626530177,
          -3.1452146685157665,
          -3.0909486743785157,
          -3.0366826802412645,
          -2.9824166861040133,
          -2.873884697829511,
          -2.765352709555009,
          -2.6735902947347254,
          -2.5818278799144414,
          -2.490065465094158,
          -2.3983030502738742,
          -2.2986233476393028,
          -2.1989436450047313,
          -2.09926394237016,
          -1.9995842397355885,
          -1.8157925017292817,
          -1.6320007637229748,
          -1.2379124287480812,
          -0.7813224452917418,
          -0.5784407982017852,
          -0.37555915111182847,
          -0.19765022939503418,
          -0.019741307678239915,
          0.1742271727821579,
          0.3681956532425557,
          0.5633169416325262,
          0.7584382300224968,
          0.9741697655088655,
          1.1899013009952342,
          1.395164413476991,
          1.6004275259587477,
          1.8138535370247761,
          2.0272795480908044,
          2.2069387887127827,
          2.386598029334761,
          2.4962698139737993,
          2.605941598612837,
          2.715613383251875,
          2.8252851678909128,
          2.9283798801756014,
          3.0314745924602904,
          3.1345693047449794,
          3.237664017029668,
          3.320246878917019,
          3.402829740804371,
          3.4854126026917225,
          3.5679954645790737,
          3.650578326466425,
          3.7331611883537765,
          3.815744050241128,
          3.8983269121284794,
          3.9237451840963598,
          3.9491634560642397,
          3.9745817280321196
         ],
         "xaxis": "x1",
         "y": [
          95.57068924077711,
          92.17793554104404,
          88.848258955441,
          85.58097347544287,
          71.26711745296939,
          58.20605924442765,
          46.33324026578646,
          35.585847500618186,
          25.902793118747283,
          17.224693039604603,
          9.49384461496139,
          2.6542036128923456,
          -1.0501757878344762,
          -4.463903882547825,
          -7.598300519747369,
          -10.464479178962222,
          -15.435615879023132,
          -19.462165397524657,
          -22.190147955114384,
          -24.348940627516882,
          -25.984583247586535,
          -27.141564378064906,
          -27.906018674364113,
          -28.209361890887887,
          -28.10256609845294,
          -27.63455296066911,
          -25.980692806749293,
          -23.53293461765564,
          -16.75635436151979,
          -8.548856200545135,
          -5.4208608164005945,
          -2.8577242239059775,
          -1.174593420907728,
          -0.08285714217549191,
          0.39075860402451323,
          0.10242871217026896,
          -0.9365897260378291,
          -2.6702432912172065,
          -5.280510735337142,
          -8.44064794132971,
          -11.737573169564511,
          -15.054831152627731,
          -18.193694192680145,
          -20.617222110757687,
          -21.7656762034124,
          -21.774653549118796,
          -21.077919648282972,
          -19.750959894997727,
          -17.708632629818638,
          -14.862029335930586,
          -11.370048424992106,
          -7.00567322756001,
          -1.6862179519381044,
          4.673763933657142,
          10.57822841409704,
          17.253430330823136,
          24.747004363093055,
          33.10765346801526,
          42.38513423493106,
          52.63024266676888,
          63.89480048528554,
          76.23164205353964,
          80.25293204280305,
          84.3824868886756,
          88.62190410531181
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        }
       ],
       "layout": {
        "annotations": [],
        "height": 400,
        "legend": {
         "bgcolor": "rgba(255, 255, 255, 1.000)",
         "bordercolor": "rgba(0, 0, 0, 1.000)",
         "font": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tracegroupgap": 0,
         "x": 1,
         "y": 1
        },
        "margin": {
         "b": 20,
         "l": 0,
         "r": 0,
         "t": 20
        },
        "paper_bgcolor": "rgba(255, 255, 255, 1.000)",
        "plot_bgcolor": "rgba(255, 255, 255, 1.000)",
        "showlegend": true,
        "width": 600,
        "xaxis": {
         "anchor": "y1",
         "domain": [
          0.05100612423447069,
          0.9934383202099737
         ],
         "gridcolor": "rgba(0, 0, 0, 0.100)",
         "gridwidth": 0.5,
         "linecolor": "rgba(0, 0, 0, 1.000)",
         "mirror": false,
         "range": [
          -4.2191198646601995,
          4.213233230731896
         ],
         "showgrid": true,
         "showline": true,
         "showticklabels": true,
         "tickangle": 0,
         "tickcolor": "rgb(0, 0, 0)",
         "tickfont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tickmode": "array",
         "ticks": "inside",
         "ticktext": [
          "-4",
          "-2",
          "0",
          "2",
          "4"
         ],
         "tickvals": [
          -4,
          -2,
          0,
          2,
          4
         ],
         "title": "",
         "titlefont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 15
         },
         "type": "-",
         "visible": true,
         "zeroline": false,
         "zerolinecolor": "rgba(0, 0, 0, 1.000)"
        },
        "yaxis": {
         "anchor": "x1",
         "domain": [
          0.03762029746281716,
          0.9901574803149606
         ],
         "gridcolor": "rgba(0, 0, 0, 0.100)",
         "gridwidth": 0.5,
         "linecolor": "rgba(0, 0, 0, 1.000)",
         "mirror": false,
         "range": [
          -31.92276342483784,
          99.28409077472706
         ],
         "showgrid": true,
         "showline": true,
         "showticklabels": true,
         "tickangle": 0,
         "tickcolor": "rgb(0, 0, 0)",
         "tickfont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tickmode": "array",
         "ticks": "inside",
         "ticktext": [
          "-25",
          "0",
          "25",
          "50",
          "75"
         ],
         "tickvals": [
          -25,
          0,
          25,
          50,
          75
         ],
         "title": "",
         "titlefont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 15
         },
         "type": "-",
         "visible": true,
         "zeroline": false,
         "zerolinecolor": "rgba(0, 0, 0, 1.000)"
        }
       }
      },
      "text/html": [
       "    <div id=\"a0210728-17d8-4d71-9cf9-a04163e71732\" style=\"width:600px;height:400px;\"></div>\n",
       "    <script>\n",
       "    PLOT = document.getElementById('a0210728-17d8-4d71-9cf9-a04163e71732');\n",
       "    Plotly.plot(PLOT, [{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],\"x\":[-3.9804683619604235,-3.9609367239208475,-3.9414050858812715,-3.921873447841695,-3.8315743496931103,-3.7412752515445256,-3.650976153395941,-3.560677055247356,-3.4703779570987714,-3.3800788589501867,-3.2897797608016024,-3.1994806626530177,-3.1452146685157665,-3.0909486743785157,-3.0366826802412645,-2.9824166861040133,-2.873884697829511,-2.765352709555009,-2.6735902947347254,-2.5818278799144414,-2.490065465094158,-2.3983030502738742,-2.2986233476393028,-2.1989436450047313,-2.09926394237016,-1.9995842397355885,-1.8157925017292817,-1.6320007637229748,-1.2379124287480812,-0.7813224452917418,-0.5784407982017852,-0.37555915111182847,-0.19765022939503418,-0.019741307678239915,0.1742271727821579,0.3681956532425557,0.5633169416325262,0.7584382300224968,0.9741697655088655,1.1899013009952342,1.395164413476991,1.6004275259587477,1.8138535370247761,2.0272795480908044,2.2069387887127827,2.386598029334761,2.4962698139737993,2.605941598612837,2.715613383251875,2.8252851678909128,2.9283798801756014,3.0314745924602904,3.1345693047449794,3.237664017029668,3.320246878917019,3.402829740804371,3.4854126026917225,3.5679954645790737,3.650578326466425,3.7331611883537765,3.815744050241128,3.8983269121284794,3.9237451840963598,3.9491634560642397,3.9745817280321196],\"showlegend\":true,\"mode\":\"lines\",\"name\":\"y1\",\"zmin\":-0.1,\"legendgroup\":\"y1\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[95.57068924077711,92.17793554104404,88.848258955441,85.58097347544287,71.26711745296939,58.20605924442765,46.33324026578646,35.585847500618186,25.902793118747283,17.224693039604603,9.49384461496139,2.6542036128923456,-1.0501757878344762,-4.463903882547825,-7.598300519747369,-10.464479178962222,-15.435615879023132,-19.462165397524657,-22.190147955114384,-24.348940627516882,-25.984583247586535,-27.141564378064906,-27.906018674364113,-28.209361890887887,-28.10256609845294,-27.63455296066911,-25.980692806749293,-23.53293461765564,-16.75635436151979,-8.548856200545135,-5.4208608164005945,-2.8577242239059775,-1.174593420907728,-0.08285714217549191,0.39075860402451323,0.10242871217026896,-0.9365897260378291,-2.6702432912172065,-5.280510735337142,-8.44064794132971,-11.737573169564511,-15.054831152627731,-18.193694192680145,-20.617222110757687,-21.7656762034124,-21.774653549118796,-21.077919648282972,-19.750959894997727,-17.708632629818638,-14.862029335930586,-11.370048424992106,-7.00567322756001,-1.6862179519381044,4.673763933657142,10.57822841409704,17.253430330823136,24.747004363093055,33.10765346801526,42.38513423493106,52.63024266676888,63.89480048528554,76.23164205353964,80.25293204280305,84.3824868886756,88.62190410531181],\"type\":\"scatter\",\"hoverinfo\":\"text\"}], {\"showlegend\":true,\"xaxis\":{\"showticklabels\":true,\"gridwidth\":0.5,\"tickvals\":[-4.0,-2.0,0.0,2.0,4.0],\"visible\":true,\"ticks\":\"inside\",\"range\":[-4.2191198646601995,4.213233230731896],\"domain\":[0.05100612423447069,0.9934383202099737],\"tickmode\":\"array\",\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"showgrid\":true,\"title\":\"\",\"mirror\":false,\"tickangle\":0,\"showline\":true,\"gridcolor\":\"rgba(0, 0, 0, 0.100)\",\"titlefont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":15},\"tickcolor\":\"rgb(0, 0, 0)\",\"ticktext\":[\"-4\",\"-2\",\"0\",\"2\",\"4\"],\"zeroline\":false,\"type\":\"-\",\"tickfont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"zerolinecolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y1\"},\"paper_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"annotations\":[],\"height\":400,\"margin\":{\"l\":0,\"b\":20,\"r\":0,\"t\":20},\"plot_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"yaxis\":{\"showticklabels\":true,\"gridwidth\":0.5,\"tickvals\":[-25.0,0.0,25.0,50.0,75.0],\"visible\":true,\"ticks\":\"inside\",\"range\":[-31.92276342483784,99.28409077472706],\"domain\":[0.03762029746281716,0.9901574803149606],\"tickmode\":\"array\",\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"showgrid\":true,\"title\":\"\",\"mirror\":false,\"tickangle\":0,\"showline\":true,\"gridcolor\":\"rgba(0, 0, 0, 0.100)\",\"titlefont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":15},\"tickcolor\":\"rgb(0, 0, 0)\",\"ticktext\":[\"-25\",\"0\",\"25\",\"50\",\"75\"],\"zeroline\":false,\"type\":\"-\",\"tickfont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"zerolinecolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x1\"},\"legend\":{\"tracegroupgap\":0,\"bordercolor\":\"rgba(0, 0, 0, 1.000)\",\"bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"font\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"y\":1.0,\"x\":1.0},\"width\":600});\n",
       "    </script>\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot(func, -4.0, 4.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we start close enough to the global optimum, we are to find it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. x = -3.0, f(x) = -9.564480032239473\n",
      "2. x = -2.413309150943051, f(x) = -26.983281625936534\n",
      "3. x = -2.2051283480850294, f(x) = -28.202989762523757\n",
      "4. x = -2.1772606979392806, f(x) = -28.219314490011172\n",
      "5. x = -2.1767739238815578, f(x) = -28.21931925035788\n",
      "6. x = -2.1767737764195543, f(x) = -28.21931925035831\n",
      "7. x = -2.176773776419541, f(x) = -28.219319250358318\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "7×2 Array{Float64,2}:\n",
       "  -9.56448  -3.0    \n",
       " -26.9833   -2.41331\n",
       " -28.203    -2.20513\n",
       " -28.2193   -2.17726\n",
       " -28.2193   -2.17677\n",
       " -28.2193   -2.17677\n",
       " -28.2193   -2.17677"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x0 = -3.0\n",
    "iter = Newton(func, dfunc, d2func, x0, true, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7×2 Array{Float64,2}:\n",
       "  -9.56448  -3.0    \n",
       " -26.9833   -2.41331\n",
       " -28.203    -2.20513\n",
       " -28.2193   -2.17726\n",
       " -28.2193   -2.17677\n",
       " -28.2193   -2.17677\n",
       " -28.2193   -2.17677"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-9.564480032239473"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot(func, -3.2, -2.0, label=\"Function\")\n",
    "x = -3.0\n",
    "m(y) = func(x)+dfunc(x)*(y-x)+0.5*d2func(x)*(y-x)^2\n",
    "m(-3.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "Function",
         "line": {
          "color": "rgba(0, 154, 250, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "Function",
         "showlegend": true,
         "text": [
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -3.194140508588127,
          -3.188281017176254,
          -3.1341015582871035,
          -3.079922099397953,
          -3.047362502915602,
          -3.0148029064332516,
          -2.987274181987166,
          -2.959745457541081,
          -2.899937635960338,
          -2.844800114558446,
          -2.785686864312212,
          -2.7171983667937614,
          -2.6563338726667745,
          -2.6029611961517363,
          -2.5447706520136166,
          -2.4862342654966256,
          -2.4215148048507147,
          -2.3599358711061877,
          -2.295908067786379,
          -2.242010295599786,
          -2.176207224816363,
          -2.1143503974455498,
          -2.015250963180728
         ],
         "xaxis": "x1",
         "y": [
          2.2764504411054105,
          1.8653139061064365,
          -1.772564930883476,
          -5.123046392768444,
          -7.002981857884379,
          -8.785581235899016,
          -10.218565147960106,
          -11.58508580363862,
          -14.331606241084529,
          -16.603863675117395,
          -18.77587491889644,
          -20.96759073108167,
          -22.63816225898873,
          -23.899776823449784,
          -25.069910978625018,
          -26.04216029859623,
          -26.89173645183085,
          -27.49372569643106,
          -27.920155164142425,
          -28.131560822618503,
          -28.219312808519756,
          -28.143110001421935,
          -27.730104135942153
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        },
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "Model",
         "line": {
          "color": "rgba(227, 111, 71, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "Model",
         "showlegend": true,
         "text": [
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -3.194140508588127,
          -3.188281017176254,
          -3.1341015582871035,
          -3.079922099397953,
          -3.047362502915602,
          -3.0148029064332516,
          -2.987274181987166,
          -2.959745457541081,
          -2.899937635960338,
          -2.844800114558446,
          -2.8152434894353293,
          -2.785686864312212,
          -2.7514426155529867,
          -2.7171983667937614,
          -2.6563338726667745,
          -2.6029611961517363,
          -2.5447706520136166,
          -2.4862342654966256,
          -2.45387453517367,
          -2.4215148048507147,
          -2.390725337978451,
          -2.3599358711061877,
          -2.295908067786379,
          -2.242010295599786,
          -2.209108760208075,
          -2.176207224816363,
          -2.1452788111309564,
          -2.1143503974455498,
          -2.064800680313139,
          -2.015250963180728,
          -2.007625481590364
         ],
         "xaxis": "x1",
         "y": [
          2.192076376229654,
          1.7883899277129252,
          -1.800229472828307,
          -5.128875479546678,
          -7.004191585546769,
          -8.785618066409116,
          -10.218541802931533,
          -11.584348660245798,
          -14.320342568247238,
          -16.56203490974674,
          -17.652849921372727,
          -18.666295526776565,
          -19.743723233556732,
          -20.717294157911592,
          -22.19134542822623,
          -23.213960383124068,
          -24.041408223188082,
          -24.571203330929716,
          -24.733830828714282,
          -24.80371784112284,
          -24.78411427715187,
          -24.680552359253603,
          -24.196356737841814,
          -23.507312528487223,
          -22.96022751740477,
          -22.317270479720943,
          -21.625451714752636,
          -20.84891511090899,
          -19.4282603545808,
          -17.790164348114445,
          -17.518762097659163
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        },
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "Newton step",
         "line": {
          "color": "rgba(62, 164, 78, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "Newton step",
         "showlegend": true,
         "text": [
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -3,
          -2.413309150943051
         ],
         "xaxis": "x1",
         "y": [
          -9.564480032239473,
          -26.983281625936534
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        },
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "",
         "line": {
          "color": "rgba(195, 113, 210, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "",
         "showlegend": false,
         "text": [
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -3,
          -3
         ],
         "xaxis": "x1",
         "y": [
          -3261.685090166276,
          3235.7422277988617
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        },
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "",
         "line": {
          "color": "rgba(172, 142, 24, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "",
         "showlegend": false,
         "text": [
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -2.413309150943051,
          -2.413309150943051
         ],
         "xaxis": "x1",
         "y": [
          -3261.685090166276,
          3235.7422277988617
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        }
       ],
       "layout": {
        "annotations": [],
        "height": 400,
        "legend": {
         "bgcolor": "rgba(255, 255, 255, 1.000)",
         "bordercolor": "rgba(0, 0, 0, 1.000)",
         "font": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tracegroupgap": 0,
         "x": 1,
         "y": 1
        },
        "margin": {
         "b": 20,
         "l": 0,
         "r": 0,
         "t": 20
        },
        "paper_bgcolor": "rgba(255, 255, 255, 1.000)",
        "plot_bgcolor": "rgba(255, 255, 255, 1.000)",
        "showlegend": true,
        "width": 600,
        "xaxis": {
         "anchor": "y1",
         "domain": [
          0.05100612423447069,
          0.9934383202099737
         ],
         "gridcolor": "rgba(0, 0, 0, 0.100)",
         "gridwidth": 0.5,
         "linecolor": "rgba(0, 0, 0, 1.000)",
         "mirror": false,
         "range": [
          -3.22973595939806,
          -1.972030030780431
         ],
         "showgrid": true,
         "showline": true,
         "showticklabels": true,
         "tickangle": 0,
         "tickcolor": "rgb(0, 0, 0)",
         "tickfont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tickmode": "array",
         "ticks": "inside",
         "ticktext": [
          "-3.00",
          "-2.75",
          "-2.50",
          "-2.25",
          "-2.00"
         ],
         "tickvals": [
          -3,
          -2.75,
          -2.5,
          -2.25,
          -2
         ],
         "title": "",
         "titlefont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 15
         },
         "type": "-",
         "visible": true,
         "zeroline": false,
         "zerolinecolor": "rgba(0, 0, 0, 1.000)"
        },
        "yaxis": {
         "anchor": "x1",
         "domain": [
          0.03762029746281716,
          0.9901574803149606
         ],
         "gridcolor": "rgba(0, 0, 0, 0.100)",
         "gridwidth": 0.5,
         "linecolor": "rgba(0, 0, 0, 1.000)",
         "mirror": false,
         "range": [
          -29.134185706008513,
          3.1913233385941657
         ],
         "showgrid": true,
         "showline": true,
         "showticklabels": true,
         "tickangle": 0,
         "tickcolor": "rgb(0, 0, 0)",
         "tickfont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tickmode": "array",
         "ticks": "inside",
         "ticktext": [
          "-25",
          "-20",
          "-15",
          "-10",
          "-5",
          "0"
         ],
         "tickvals": [
          -25,
          -20,
          -15,
          -10,
          -5,
          0
         ],
         "title": "",
         "titlefont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 15
         },
         "type": "-",
         "visible": true,
         "zeroline": false,
         "zerolinecolor": "rgba(0, 0, 0, 1.000)"
        }
       }
      },
      "text/html": [
       "    <div id=\"bf205d57-e686-4aa5-bad4-89debdf3a1d7\" style=\"width:600px;height:400px;\"></div>\n",
       "    <script>\n",
       "    PLOT = document.getElementById('bf205d57-e686-4aa5-bad4-89debdf3a1d7');\n",
       "    Plotly.plot(PLOT, [{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],\"x\":[-3.194140508588127,-3.188281017176254,-3.1341015582871035,-3.079922099397953,-3.047362502915602,-3.0148029064332516,-2.987274181987166,-2.959745457541081,-2.899937635960338,-2.844800114558446,-2.785686864312212,-2.7171983667937614,-2.6563338726667745,-2.6029611961517363,-2.5447706520136166,-2.4862342654966256,-2.4215148048507147,-2.3599358711061877,-2.295908067786379,-2.242010295599786,-2.176207224816363,-2.1143503974455498,-2.015250963180728],\"showlegend\":true,\"mode\":\"lines\",\"name\":\"Function\",\"zmin\":-0.1,\"legendgroup\":\"Function\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[2.2764504411054105,1.8653139061064365,-1.772564930883476,-5.123046392768444,-7.002981857884379,-8.785581235899016,-10.218565147960106,-11.58508580363862,-14.331606241084529,-16.603863675117395,-18.77587491889644,-20.96759073108167,-22.63816225898873,-23.899776823449784,-25.069910978625018,-26.04216029859623,-26.89173645183085,-27.49372569643106,-27.920155164142425,-28.131560822618503,-28.219312808519756,-28.143110001421935,-27.730104135942153],\"type\":\"scatter\",\"hoverinfo\":\"text\"},{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],\"x\":[-3.194140508588127,-3.188281017176254,-3.1341015582871035,-3.079922099397953,-3.047362502915602,-3.0148029064332516,-2.987274181987166,-2.959745457541081,-2.899937635960338,-2.844800114558446,-2.8152434894353293,-2.785686864312212,-2.7514426155529867,-2.7171983667937614,-2.6563338726667745,-2.6029611961517363,-2.5447706520136166,-2.4862342654966256,-2.45387453517367,-2.4215148048507147,-2.390725337978451,-2.3599358711061877,-2.295908067786379,-2.242010295599786,-2.209108760208075,-2.176207224816363,-2.1452788111309564,-2.1143503974455498,-2.064800680313139,-2.015250963180728,-2.007625481590364],\"showlegend\":true,\"mode\":\"lines\",\"name\":\"Model\",\"zmin\":-0.1,\"legendgroup\":\"Model\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[2.192076376229654,1.7883899277129252,-1.800229472828307,-5.128875479546678,-7.004191585546769,-8.785618066409116,-10.218541802931533,-11.584348660245798,-14.320342568247238,-16.56203490974674,-17.652849921372727,-18.666295526776565,-19.743723233556732,-20.717294157911592,-22.19134542822623,-23.213960383124068,-24.041408223188082,-24.571203330929716,-24.733830828714282,-24.80371784112284,-24.78411427715187,-24.680552359253603,-24.196356737841814,-23.507312528487223,-22.96022751740477,-22.317270479720943,-21.625451714752636,-20.84891511090899,-19.4282603545808,-17.790164348114445,-17.518762097659163],\"type\":\"scatter\",\"hoverinfo\":\"text\"},{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null],\"x\":[-3.0,-2.413309150943051],\"showlegend\":true,\"mode\":\"lines\",\"name\":\"Newton step\",\"zmin\":-0.1,\"legendgroup\":\"Newton step\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(62, 164, 78, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[-9.564480032239473,-26.983281625936534],\"type\":\"scatter\",\"hoverinfo\":\"text\"},{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null],\"x\":[-3.0,-3.0],\"showlegend\":false,\"mode\":\"lines\",\"name\":\"\",\"zmin\":-0.1,\"legendgroup\":\"\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(195, 113, 210, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[-3261.685090166276,3235.7422277988617],\"type\":\"scatter\",\"hoverinfo\":\"text\"},{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null],\"x\":[-2.413309150943051,-2.413309150943051],\"showlegend\":false,\"mode\":\"lines\",\"name\":\"\",\"zmin\":-0.1,\"legendgroup\":\"\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(172, 142, 24, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[-3261.685090166276,3235.7422277988617],\"type\":\"scatter\",\"hoverinfo\":\"text\"}], {\"showlegend\":true,\"xaxis\":{\"showticklabels\":true,\"gridwidth\":0.5,\"tickvals\":[-3.0,-2.75,-2.5,-2.25,-2.0],\"visible\":true,\"ticks\":\"inside\",\"range\":[-3.22973595939806,-1.972030030780431],\"domain\":[0.05100612423447069,0.9934383202099737],\"tickmode\":\"array\",\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"showgrid\":true,\"title\":\"\",\"mirror\":false,\"tickangle\":0,\"showline\":true,\"gridcolor\":\"rgba(0, 0, 0, 0.100)\",\"titlefont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":15},\"tickcolor\":\"rgb(0, 0, 0)\",\"ticktext\":[\"-3.00\",\"-2.75\",\"-2.50\",\"-2.25\",\"-2.00\"],\"zeroline\":false,\"type\":\"-\",\"tickfont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"zerolinecolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y1\"},\"paper_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"annotations\":[],\"height\":400,\"margin\":{\"l\":0,\"b\":20,\"r\":0,\"t\":20},\"plot_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"yaxis\":{\"showticklabels\":true,\"gridwidth\":0.5,\"tickvals\":[-25.0,-20.0,-15.0,-10.0,-5.0,0.0],\"visible\":true,\"ticks\":\"inside\",\"range\":[-29.134185706008513,3.1913233385941657],\"domain\":[0.03762029746281716,0.9901574803149606],\"tickmode\":\"array\",\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"showgrid\":true,\"title\":\"\",\"mirror\":false,\"tickangle\":0,\"showline\":true,\"gridcolor\":\"rgba(0, 0, 0, 0.100)\",\"titlefont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":15},\"tickcolor\":\"rgb(0, 0, 0)\",\"ticktext\":[\"-25\",\"-20\",\"-15\",\"-10\",\"-5\",\"0\"],\"zeroline\":false,\"type\":\"-\",\"tickfont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"zerolinecolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x1\"},\"legend\":{\"tracegroupgap\":0,\"bordercolor\":\"rgba(0, 0, 0, 1.000)\",\"bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"font\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"y\":1.0,\"x\":1.0},\"width\":600});\n",
       "    </script>\n"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot!(m, -3.2, -2.0, label=\"Model\")\n",
    "plot!(iter[1:2,2], iter[1:2,1], label=\"Newton step\")\n",
    "vline!([iter[1,2] iter[2,2]], label = \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "data": [
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "Function",
         "line": {
          "color": "rgba(0, 154, 250, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "Function",
         "showlegend": true,
         "text": [
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -3.194140508588127,
          -3.188281017176254,
          -3.1341015582871035,
          -3.079922099397953,
          -3.047362502915602,
          -3.0148029064332516,
          -2.987274181987166,
          -2.959745457541081,
          -2.899937635960338,
          -2.844800114558446,
          -2.785686864312212,
          -2.7171983667937614,
          -2.6563338726667745,
          -2.6029611961517363,
          -2.5447706520136166,
          -2.4862342654966256,
          -2.4215148048507147,
          -2.3599358711061877,
          -2.295908067786379,
          -2.242010295599786,
          -2.176207224816363,
          -2.1143503974455498,
          -2.015250963180728
         ],
         "xaxis": "x1",
         "y": [
          2.2764504411054105,
          1.8653139061064365,
          -1.772564930883476,
          -5.123046392768444,
          -7.002981857884379,
          -8.785581235899016,
          -10.218565147960106,
          -11.58508580363862,
          -14.331606241084529,
          -16.603863675117395,
          -18.77587491889644,
          -20.96759073108167,
          -22.63816225898873,
          -23.899776823449784,
          -25.069910978625018,
          -26.04216029859623,
          -26.89173645183085,
          -27.49372569643106,
          -27.920155164142425,
          -28.131560822618503,
          -28.219312808519756,
          -28.143110001421935,
          -27.730104135942153
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        },
        {
         "colorbar": {
          "title": ""
         },
         "hoverinfo": "text",
         "legendgroup": "Newton steps",
         "line": {
          "color": "rgba(227, 111, 71, 1.000)",
          "dash": "solid",
          "shape": "linear",
          "width": 1
         },
         "mode": "lines",
         "name": "Newton steps",
         "showlegend": true,
         "text": [
          null,
          null,
          null,
          null,
          null,
          null,
          null
         ],
         "type": "scatter",
         "x": [
          -3,
          -2.413309150943051,
          -2.2051283480850294,
          -2.1772606979392806,
          -2.1767739238815578,
          -2.1767737764195543,
          -2.176773776419541
         ],
         "xaxis": "x1",
         "y": [
          -9.564480032239473,
          -26.983281625936534,
          -28.202989762523757,
          -28.219314490011172,
          -28.21931925035788,
          -28.21931925035831,
          -28.219319250358318
         ],
         "yaxis": "y1",
         "zmax": 0.1,
         "zmin": -0.1
        }
       ],
       "layout": {
        "annotations": [],
        "height": 400,
        "legend": {
         "bgcolor": "rgba(255, 255, 255, 1.000)",
         "bordercolor": "rgba(0, 0, 0, 1.000)",
         "font": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tracegroupgap": 0,
         "x": 1,
         "y": 1
        },
        "margin": {
         "b": 20,
         "l": 0,
         "r": 0,
         "t": 20
        },
        "paper_bgcolor": "rgba(255, 255, 255, 1.000)",
        "plot_bgcolor": "rgba(255, 255, 255, 1.000)",
        "showlegend": true,
        "width": 600,
        "xaxis": {
         "anchor": "y1",
         "domain": [
          0.05100612423447069,
          0.9934383202099737
         ],
         "gridcolor": "rgba(0, 0, 0, 0.100)",
         "gridwidth": 0.5,
         "linecolor": "rgba(0, 0, 0, 1.000)",
         "mirror": false,
         "range": [
          -3.229507194950349,
          -1.979884276818506
         ],
         "showgrid": true,
         "showline": true,
         "showticklabels": true,
         "tickangle": 0,
         "tickcolor": "rgb(0, 0, 0)",
         "tickfont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tickmode": "array",
         "ticks": "inside",
         "ticktext": [
          "-3.00",
          "-2.75",
          "-2.50",
          "-2.25",
          "-2.00"
         ],
         "tickvals": [
          -3,
          -2.75,
          -2.5,
          -2.25,
          -2
         ],
         "title": "",
         "titlefont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 15
         },
         "type": "-",
         "visible": true,
         "zeroline": false,
         "zerolinecolor": "rgba(0, 0, 0, 1.000)"
        },
        "yaxis": {
         "anchor": "x1",
         "domain": [
          0.03762029746281716,
          0.9901574803149606
         ],
         "gridcolor": "rgba(0, 0, 0, 0.100)",
         "gridwidth": 0.5,
         "linecolor": "rgba(0, 0, 0, 1.000)",
         "mirror": false,
         "range": [
          -29.134192341102228,
          3.1913235318493225
         ],
         "showgrid": true,
         "showline": true,
         "showticklabels": true,
         "tickangle": 0,
         "tickcolor": "rgb(0, 0, 0)",
         "tickfont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 11
         },
         "tickmode": "array",
         "ticks": "inside",
         "ticktext": [
          "-25",
          "-20",
          "-15",
          "-10",
          "-5",
          "0"
         ],
         "tickvals": [
          -25,
          -20,
          -15,
          -10,
          -5,
          0
         ],
         "title": "",
         "titlefont": {
          "color": "rgba(0, 0, 0, 1.000)",
          "family": "sans-serif",
          "size": 15
         },
         "type": "-",
         "visible": true,
         "zeroline": false,
         "zerolinecolor": "rgba(0, 0, 0, 1.000)"
        }
       }
      },
      "text/html": [
       "    <div id=\"f9d22777-0a8e-44b2-95a3-553bf2f4ddcd\" style=\"width:600px;height:400px;\"></div>\n",
       "    <script>\n",
       "    PLOT = document.getElementById('f9d22777-0a8e-44b2-95a3-553bf2f4ddcd');\n",
       "    Plotly.plot(PLOT, [{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],\"x\":[-3.194140508588127,-3.188281017176254,-3.1341015582871035,-3.079922099397953,-3.047362502915602,-3.0148029064332516,-2.987274181987166,-2.959745457541081,-2.899937635960338,-2.844800114558446,-2.785686864312212,-2.7171983667937614,-2.6563338726667745,-2.6029611961517363,-2.5447706520136166,-2.4862342654966256,-2.4215148048507147,-2.3599358711061877,-2.295908067786379,-2.242010295599786,-2.176207224816363,-2.1143503974455498,-2.015250963180728],\"showlegend\":true,\"mode\":\"lines\",\"name\":\"Function\",\"zmin\":-0.1,\"legendgroup\":\"Function\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(0, 154, 250, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[2.2764504411054105,1.8653139061064365,-1.772564930883476,-5.123046392768444,-7.002981857884379,-8.785581235899016,-10.218565147960106,-11.58508580363862,-14.331606241084529,-16.603863675117395,-18.77587491889644,-20.96759073108167,-22.63816225898873,-23.899776823449784,-25.069910978625018,-26.04216029859623,-26.89173645183085,-27.49372569643106,-27.920155164142425,-28.131560822618503,-28.219312808519756,-28.143110001421935,-27.730104135942153],\"type\":\"scatter\",\"hoverinfo\":\"text\"},{\"xaxis\":\"x1\",\"colorbar\":{\"title\":\"\"},\"yaxis\":\"y1\",\"text\":[null,null,null,null,null,null,null],\"x\":[-3.0,-2.413309150943051,-2.2051283480850294,-2.1772606979392806,-2.1767739238815578,-2.1767737764195543,-2.176773776419541],\"showlegend\":true,\"mode\":\"lines\",\"name\":\"Newton steps\",\"zmin\":-0.1,\"legendgroup\":\"Newton steps\",\"zmax\":0.1,\"line\":{\"color\":\"rgba(227, 111, 71, 1.000)\",\"shape\":\"linear\",\"dash\":\"solid\",\"width\":1},\"y\":[-9.564480032239473,-26.983281625936534,-28.202989762523757,-28.219314490011172,-28.21931925035788,-28.21931925035831,-28.219319250358318],\"type\":\"scatter\",\"hoverinfo\":\"text\"}], {\"showlegend\":true,\"xaxis\":{\"showticklabels\":true,\"gridwidth\":0.5,\"tickvals\":[-3.0,-2.75,-2.5,-2.25,-2.0],\"visible\":true,\"ticks\":\"inside\",\"range\":[-3.229507194950349,-1.979884276818506],\"domain\":[0.05100612423447069,0.9934383202099737],\"tickmode\":\"array\",\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"showgrid\":true,\"title\":\"\",\"mirror\":false,\"tickangle\":0,\"showline\":true,\"gridcolor\":\"rgba(0, 0, 0, 0.100)\",\"titlefont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":15},\"tickcolor\":\"rgb(0, 0, 0)\",\"ticktext\":[\"-3.00\",\"-2.75\",\"-2.50\",\"-2.25\",\"-2.00\"],\"zeroline\":false,\"type\":\"-\",\"tickfont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"zerolinecolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"y1\"},\"paper_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"annotations\":[],\"height\":400,\"margin\":{\"l\":0,\"b\":20,\"r\":0,\"t\":20},\"plot_bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"yaxis\":{\"showticklabels\":true,\"gridwidth\":0.5,\"tickvals\":[-25.0,-20.0,-15.0,-10.0,-5.0,0.0],\"visible\":true,\"ticks\":\"inside\",\"range\":[-29.134192341102228,3.1913235318493225],\"domain\":[0.03762029746281716,0.9901574803149606],\"tickmode\":\"array\",\"linecolor\":\"rgba(0, 0, 0, 1.000)\",\"showgrid\":true,\"title\":\"\",\"mirror\":false,\"tickangle\":0,\"showline\":true,\"gridcolor\":\"rgba(0, 0, 0, 0.100)\",\"titlefont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":15},\"tickcolor\":\"rgb(0, 0, 0)\",\"ticktext\":[\"-25\",\"-20\",\"-15\",\"-10\",\"-5\",\"0\"],\"zeroline\":false,\"type\":\"-\",\"tickfont\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"zerolinecolor\":\"rgba(0, 0, 0, 1.000)\",\"anchor\":\"x1\"},\"legend\":{\"tracegroupgap\":0,\"bordercolor\":\"rgba(0, 0, 0, 1.000)\",\"bgcolor\":\"rgba(255, 255, 255, 1.000)\",\"font\":{\"color\":\"rgba(0, 0, 0, 1.000)\",\"family\":\"sans-serif\",\"size\":11},\"y\":1.0,\"x\":1.0},\"width\":600});\n",
       "    </script>\n"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plot(func, -3.2, -2.0, label=\"Function\")\n",
    "plot!(iter[:,2], iter[:,1], label=\"Newton steps\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. x = -4.0, f(x) = 99.02720998123172\n",
      "2. x = -2.942938833739946, f(x) = -12.3872911024757\n",
      "3. x = -2.3879767853105314, f(x) = -27.24370765699775\n",
      "4. x = -2.199836344922852, f(x) = -28.20853988049366\n",
      "5. x = -2.177097516735148, f(x) = -28.219317146168752\n",
      "6. x = -2.1767738416160567, f(x) = -28.21931925035823\n",
      "7. x = -2.176773776419543, f(x) = -28.219319250358318\n",
      "8. x = -2.1767737764195405, f(x) = -28.219319250358314\n"
     ]
    }
   ],
   "source": [
    "x0 = -4.0\n",
    "Newton(func, dfunc, d2func, x0, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. x = -2.2, f(x) = -28.208385615278356\n",
      "2. x = -2.177102076816536, f(x) = -28.219317086469566\n",
      "3. x = -2.176773843465362, f(x) = -28.219319250358225\n",
      "4. x = -2.1767737764195436, f(x) = -28.219319250358314\n",
      "5. x = -2.176773776419541, f(x) = -28.219319250358318\n"
     ]
    }
   ],
   "source": [
    "x0 = -2.2\n",
    "Newton(func, dfunc, d2func, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, if the method converges, it converges to a point where the derivative is equal to zero. This could be a local maximum!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = 1.0\n",
    "Newton(func, dfunc, d2func, x0, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "α = 0.1\n",
    "x = x0-α*dfunc(x0)/d2func(x0)\n",
    "α = 0.001\n",
    "x = x0-α*dfunc(x0)/d2func(x0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfunc(x0)/d2func(x0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfunc(x0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also converge to local, but not global, minimum."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "x0 = 2.0\n",
    "Newton(func, dfunc, d2func, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Secant method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "function Secant(f::Function, df::Function, x0::Float64, x1::Float64,\n",
    "                verbose::Bool = false, δ::Float64 = 1e-6, nmax::Int64 = 1000)\n",
    "    k = 1\n",
    "    x = x0\n",
    "    y = x1\n",
    "    if (x0 == x1)\n",
    "        println(\"x0 must different from x1\")\n",
    "        return\n",
    "    end\n",
    "    if (verbose)\n",
    "        println(\"0. x0 = $x0, f($x0) = $(f(x0))\")\n",
    "        println(\"1. x1 = $x1, f($x1) = $(f(x1))\")\n",
    "    end\n",
    "    dfx = df(x)\n",
    "    dfy = df(y)\n",
    "    while (abs(dfy) > δ && k < nmax)\n",
    "        k += 1\n",
    "        t = y\n",
    "        y = y-(x-y)/(dfx-dfy)*dfy\n",
    "        x = t\n",
    "        if (verbose)\n",
    "            println(\"$k. x = $y, f(x) = $(f(y))\")\n",
    "        end\n",
    "        dfx = dfy\n",
    "        dfy = df(y)        \n",
    "    end\n",
    "    \n",
    "    return y\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Secant(func, dfunc, 1.0, 2.0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Root finding using Newton method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Iteration\n",
    "$$\n",
    "x_{k+1} = x_k-\\frac{f(x)}{f'(x)}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x) = x-2sin(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df(x) = 1-2cos(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plot(f, -5.0, 5.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "We are looking for a zero of $f(x)$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "function NewtonRoot(f::Function, df::Function, xstart::Float64,\n",
    "        verbose::Bool = false, δ::Float64 = 1e-6, nmax::Int64 = 1000)\n",
    "    k = 1\n",
    "    x = xstart\n",
    "    fx = f(x)\n",
    "    if (verbose)\n",
    "        println(\"$k. x = $x, f(x) = $fx\")\n",
    "    end\n",
    "    while (abs(fx) > δ && k < nmax)\n",
    "        k += 1\n",
    "        dfx = df(x)\n",
    "        x = x-fx/df(x)\n",
    "        fx = f(x)\n",
    "        if (verbose)\n",
    "            println(\"$k. x = $x, f(x) = $fx\")\n",
    "        end\n",
    "    end\n",
    "    \n",
    "    return x\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = 1.1\n",
    "NewtonRoot(f, df, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It works, but we were close to a disaster! Observe that"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df(1.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "The curve is nearly flat, and we move to a distant point. Luckily we come back, but using many iterations. Consider now the starting point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = π/3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The derivative at this point is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df(x0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Newton method now gives"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NewtonRoot(f, df, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are less lucky! In fact, at $\\pi/3$,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df(π/3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Newton recurrence is under trouble as we have a division by zero. The function however proceeds as due to numerical errors, we have avoided the division by zero, but the method diverges, as $x \\rightarrow -\\infty$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Take now a point with a significant derivative."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = 4.0\n",
    "NewtonRoot(f, df, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The method now converges very quickly, even if the starting point was further from the solution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the $x-2\\sin x = 0$ is equivalent to $\\frac{1}{\\sin x} - \\frac{2}{x} = 0$ if we require that $x \\ne k\\pi$, $k \\in \\mathcal{Z}$. The function shape is nevertheless quite different around the function zero."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g(x) = 1/sin(x) - 2/x\n",
    "plot(g, 0.1, π-0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "The derivative is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dg(x) = 2/(x*x)-cos(x)/(sin(x)^2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = 1.1\n",
    "NewtonRoot(g, dg, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now observe a fast convergence, mainly due to the fact that the function does not present flat parts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x) = exp(x/2)-x-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df(x) = 0.5*exp(x/2)-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = 1.0\n",
    "NewtonRoot(f, df, x0, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Roots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = fzero(df,0.0, 5.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = y\n",
    "NewtonRoot(f, df, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cycles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider now the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "h(x) = x^3 - 2*x + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot(h, -2.5, 1.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dh(x) = 3x^2-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "x0 = 0.0\n",
    "NewtonRoot(h, dh, x0, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ratio(x) = h(x)/dh(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ratio(0.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ratio(1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The method cycles! However, if we change the starting point, we can converge."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x0 = 0.5\n",
    "NewtonRoot(h, dh, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Application: computation of the square root of a positive number."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The square root of a real number can be computed using the Newton method. More precisely, if we look for the square root of $s$, we can rewrite the problem as the computation of a zero of the function\n",
    "$$\n",
    "f(x) = x^2 - s\n",
    "$$\n",
    "Indeed, if $f(x) = 0$, then $s = x^2$ or $x = \\sqrt{s}$. Develop the Newton recurrence for this problem."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Choosing a good starting point can however be an issue. (Wikipedia) With $s$ expressed in scientific notation as $a\\times 10^{2n}$ where $1\\leq a<100$ and n is an integer, $\\sqrt{s} \\approx \\sqrt{a} 10^n$. The starting point is often computed as\n",
    "$$\n",
    "\\sqrt{s} =\n",
    "\\begin{cases}\n",
    "2\\times10^n & \\mbox{ if } a < 10\\\\\n",
    "6\\times10^n & \\mbox{ if } a \\geq 10\n",
    "\\end{cases}\n",
    "$$\n",
    "The factors two and six are used because they approximate the geometric means of the lowest and highest possible values with the given number of digits:\n",
    "$$\n",
    "\\sqrt{\\sqrt{1}\\sqrt{10}} = 10^{\\frac{1}{4}} \\approx 2\n",
    "$$\n",
    "and\n",
    "$$\n",
    "\\sqrt{\\sqrt{10}\\sqrt{100}} = 10^{\\frac{3}{4}} \\approx 6\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sq(x,s) = x*x-s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dsq(x) = 2*x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sqs(x) = x*x-s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 6.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = NewtonRoot(sqs, dsq, 2.0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can check the solution as"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = NewtonRoot(sqs, dsq, 2.0, true, 1e-8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = NewtonRoot(sqs, dsq, 2.0, true, 1e-10)\n",
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = NewtonRoot(sqs, dsq, 2.0, true, 1e-12)\n",
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = NewtonRoot(sqs, dsq, 2.0, true, 1e-14)\n",
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = NewtonRoot(sqs, dsq, 2.0, true, 1e-16)\n",
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = NewtonRoot(sqs, dsq, 2.0, true, 1e-15)\n",
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "6-x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eps()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 25\n",
    "x = NewtonRoot(sqs, dsq, 6.0, true, 1e-15)\n",
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 400\n",
    "x = NewtonRoot(sqs, dsq, 6.0, true, 1e-15)\n",
    "x^2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.1.1",
   "language": "julia",
   "name": "julia-1.1"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.1.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
