{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Newton's Method ##\n", "\n", "Newton's Method is a technique for approximating a root to an equation of the form $f(x)=0$. What is required is an initial estimate for the root, called $x_1$, and an iterative formula:\n", "\n", "$$x_{n+1} = x_n - \\frac{f(x_n)}{f^{\\prime}(x_n)}$$\n", "\n", "This produces a sequence $x_1, x_2, x_3, \\ldots $ which converges to the root (hopefully).\n", "\n", "\n", "\n", "An implementation of Newton's Method is shown in the code block below. You can change the function $f$, and the initial\n", "guess $x_1$. Clicking evaluate will run two iterations of Newton's method and return the next two approximations. Feel free to add some more lines\n", "of code to find more iterations - just duplicate the last two lines of code and update the index:\n", "\n", "```python\n", "x4=NewtonIt(x3);\n", "print(x4);\n", "```\n", "\n", "### Run the calculations yourself...###" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "0.8700736957962091\n", "0.8712214142629191\n" ] } ], "source": [ "import math\n", "import numpy as np\n", "from sympy import *\n", "\n", "x = Symbol('x'); # declare the variable for function\n", "y = cos(x)+5*x-5; # change this to whatever function you like\n", "dy = y.diff(x); # python will compute the derivative of y\n", "f = lambdify(x, y, 'numpy') # convert y to a lambda function f so we can evaluate it as 'f(4)', etc.\n", "df = lambdify(x, dy, 'numpy') # convert dy to a lambda function df so we can evaluate it as 'df(4)', etc.\n", "def NewtonIt(x):\n", " return x-(f(x)/df(x)); # Newtons Iterative Formula which we are calling \"NewtonIt\"\n", "\n", "x1=1; # initial guess\n", "print(x1); # prints x1 in output window below\n", "x2=NewtonIt(x1); # one iteration of Newtons Method - output below\n", "print(x2);\n", "x3=NewtonIt(x2); # a second iteration of Newtons Method\n", "print(x3);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "