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