Next: Python code Up: The Code Previous: The Code

## Pseudocode

First we write pseudocode for this problem.
 ``` Prompt for the stopping tolerance. (See the discussion above.) Prompt for and read the starting x. Begin loop. Allow N iterations before giving up. Evaluate f(old x) and f'(old x). Compute a new x = old x - f(old x)/f'(old x) (Newton-Raphson formula) If the change in x is less than the tolerance, write answer and quit. Otherwise, continue looping. End loop. Write an error message: method did not converge in N steps. ```

For clarity we have indented the portion of the code inside the loop. A tricky part of the code is to keep track of the values of the old and new values of , since at some point the new replaces the old , and we must be sure to start off with the correct . And to compute the change in , we need to have the old and new values available. So it is helpful to plan this out carefully in the pseudocode by making it explicit:
 ``` Prompt for the stopping tolerance. (See the discussion above.) Prompt for and read the starting x. Call it pnew for now. Begin loop. Allow N iterations before giving up. Set p = pnew. Evaluate f(p) and f'(p). Compute pnew = p - f(p)/f'(p) (Newton-Raphson formula) If |pnew - p| is less than the tolerance, write answer and quit. Otherwise, continue looping. End loop. Write an error message. ```

Notice that `p` is our ``old'' and `pnew` is our ``new'' . We chose to convert new into old at the beginning of the loop. (It is also possible to do it at the end of the loop block with some adjustments in the rest of the code.) Then to make the first iteration of the loop work correctly, we had to put the starting value in `pnew` at the outset.

It is always a good idea to ``play computer'' with the code or pseudocode, going through it step-by-step by hand to verify that the logic is correct.

Next: Python code Up: The Code Previous: The Code
Carleton DeTar 2018-01-29