NEWTON

In review

This page runs a BASIC program that uses Newton’s method to solve

\[ g(x) = 4x^3 + 3x^2 + 2x + 1 = 0. \]

The program is rewritten in Python and runs right here in your browser. Edit the code and press Run to run it again; Reset brings back the original program.

Starting from a guess, Newton’s method follows the tangent line to the graph of g down to the x-axis and takes the place where it crosses as the next guess:

\[ x_{\text{new}} = x - \frac{g(x)}{g'(x)}. \]

In the program, g is the value of the function, gprime is its derivative \(12x^2 + 6x + 2\), and x = x - g / gprime is the Newton step. Each line of output shows the step number n and the current guess x, starting from start = -1. By step 6 the guess has settled at \(-0.605829586188268\), and the remaining steps don’t change it. As with the Babylonian method, the number of correct digits roughly doubles with each step once the guess is close.

Try other values of start. Since \(g'(x) = 12x^2 + 6x + 2\) is never zero, this cubic has only one root, so every start leads to the same answer, though some take longer. Try changing g and gprime to solve a different equation; just be sure gprime really is the derivative of g, or the method will wander.

Note that the loop runs FOR n = 0 TO numberofsteps, which is 9 passes, starting at 0; in Python that is range(0, numberofsteps + 1). BASIC’s ^ becomes **.

Notes from the textbook

Add notes here.

 1  DEFDBL A-Z
 2  start = -1
 3  numberofsteps = 8
 4  x = start
 5  FOR n = 0 TO numberofsteps
 6          PRINT n, x
 7          g = 4 * x ^ 3 + 3 * x ^ 2 + 2 * x + 1
 8          gprime = 12 * x ^ 2 + 6 * x + 2
 9          x = x - g / gprime
10  NEXT n

Output

Everything the program prints appears here, one line for each print(...), like BASIC's PRINT. Numbers printed on the same line are lined up in columns.

Loading Python. The first run takes a few seconds.

Python code

Press Run (or Ctrl/⌘ + Enter) to run the program again. Press Esc, then Tab, to leave the editor.

Loading Python…