{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Newton's method in $n$ dimensions"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import numpy.linalg as la"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def f(xvec):\n",
        "    x, y = xvec\n",
        "    return np.array([\n",
        "        x + 2*y -2,\n",
        "        x**2 + 4*y**2 - 4\n",
        "        ])\n",
        "\n",
        "def Jf(xvec):\n",
        "    x, y = xvec\n",
        "    return np.array([\n",
        "        [1, 2],\n",
        "        [2*x, 8*y]\n",
        "        ])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Pick an initial guess."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.array([1, 2])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now implement Newton's method."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[  1.50295992e-16   1.00000000e+00]\n"
          ]
        }
      ],
      "source": [
        "x = x - la.solve(Jf(x), f(x))\n",
        "print(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Check if that's really a solution:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([ 0.,  0.])"
            ]
          },
          "execution_count": 30,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "f(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "* What's the cost of one iteration?\n",
        "* Is there still something like quadratic convergence?"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "--------------------\n",
        "Let's keep an error history and check."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 32,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "xtrue = np.array([0, 1])\n",
        "errors = []\n",
        "x = np.array([1, 2])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 41,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[  1.50295992e-16   1.00000000e+00]\n"
          ]
        }
      ],
      "source": [
        "x = x - la.solve(Jf(x), f(x))\n",
        "errors.append(la.norm(x-xtrue))\n",
        "print(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 42,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.931694990625\n",
            "0.211748861506\n",
            "0.0168589857887\n",
            "0.000125221235922\n",
            "7.01168369152e-09\n",
            "1.50295991741e-16\n",
            "1.50295991741e-16\n",
            "1.50295991741e-16\n",
            "1.50295991741e-16\n"
          ]
        }
      ],
      "source": [
        "for e in errors:\n",
        "    print(e)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 43,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.243934688455\n",
            "0.376001239529\n",
            "0.440570178174\n",
            "0.447163497456\n",
            "3.05705157878\n",
            "6.65353738591e+15\n",
            "6.65353738591e+15\n",
            "6.65353738591e+15\n"
          ]
        }
      ],
      "source": [
        "for i in range(len(errors)-1):\n",
        "    print(errors[i+1]/errors[i]**2)"
      ]
    }
  ],
  "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.5.1+"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}