{"worksheets": [{"cells": [{"source": ["# Newton's method in $n$ dimensions"], "metadata": {}, "cell_type": "markdown"}, {"metadata": {}, "input": ["\n", "import numpy as np\n", "import numpy.linalg as la"], "outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 2}, {"metadata": {}, "input": ["\n", "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", " ])"], "outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 3}, {"source": ["Pick an initial guess."], "metadata": {}, "cell_type": "markdown"}, {"metadata": {}, "input": ["x = np.array([1, 2])"], "outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 20}, {"source": ["Now implement Newton's method."], "metadata": {}, "cell_type": "markdown"}, {"metadata": {}, "input": ["x = x - la.solve(Jf(x), f(x))\n", "print(x)"], "outputs": [{"output_type": "stream", "stream": "stdout", "text": ["[ 1.50295992e-16 1.00000000e+00]\n"]}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 27}, {"source": ["Check if that's really a solution:"], "metadata": {}, "cell_type": "markdown"}, {"metadata": {}, "input": ["f(x)"], "outputs": [{"output_type": "pyout", "metadata": {}, "prompt_number": 30, "text": ["array([ 0., 0.])"]}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 30}, {"source": ["* What's the cost of one iteration?\n", "* Is there still something like quadratic convergence?"], "metadata": {}, "cell_type": "markdown"}, {"source": ["--------------------\n", "Let's keep an error history and check."], "metadata": {}, "cell_type": "markdown"}, {"metadata": {}, "input": ["xtrue = np.array([0, 1])\n", "errors = []\n", "x = np.array([1, 2])"], "outputs": [], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 32}, {"metadata": {}, "input": ["x = x - la.solve(Jf(x), f(x))\n", "errors.append(la.norm(x-xtrue))\n", "print(x)"], "outputs": [{"output_type": "stream", "stream": "stdout", "text": ["[ 1.50295992e-16 1.00000000e+00]\n"]}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 41}, {"metadata": {}, "input": ["for e in errors:\n", " print(e)"], "outputs": [{"output_type": "stream", "stream": "stdout", "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"]}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 42}, {"metadata": {}, "input": ["for i in range(len(errors)-1):\n", " print(errors[i+1]/errors[i]**2)"], "outputs": [{"output_type": "stream", "stream": "stdout", "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"]}], "collapsed": false, "language": "python", "cell_type": "code", "prompt_number": 43}], "metadata": {}}], "nbformat": 3, "metadata": {"name": "", "signature": "sha256:d9127f0cac2427ab0c9ed64c8560c0bb94956992eb741571d5b5e6cacc2393bb"}, "nbformat_minor": 0}