CS 2073, Engineering Programming
|
Suppose a number x0 is known to be fairly close to the root. This means that y0 = f(x0) is fairly close to 0. The number x0 is the initial guess of the root. Then under most circumstances, Newton's method lets one calculate a new, much better, guess for the root.
Specifically, starting from the point (x0, y0) on the graph of the equation, let L be the line tangent to the graph at that point. You follow this line back to the x-axis, and the value of x where it intersects the x-axis is the new, better guess: x1.
Here is one way to calculate the equation of this line: a point (x, y) lies on this line, if the slope of the line through (x, y) and (x0, y0) is equal to f'(x0). But the slope of the line through two points is just the difference of the x values divided by the difference of the y values. Plugging this in, one gets the following equation for the line:
y - y0 -------- = f'(x0) x - x0
Rewriting in the form y = m*x + b gives:
y = f'(x0)*x + (y0 - x0*f'(x0)
Now where this line intersects the x-axis, one has y = 0. So setting y = 0, and set y0 = f(x0) in the equation, and solving for x, one gets:
x = x0 - f(x0)/f'(x0)
This value of x is the new, better approximation for the root, that was called x1 above, so we have the magic formula:
x1 = x0 - f(x0)/f'(x0) |
Plugging in f(x) = 2 - x2 and f'(x) = -2x into the magic formula, we get a specific formula for this example:
x1 = x0/2 + 1/x0 |
Here is a picture of this example:

Here is a table showing what happens when we use the magic formula over and over again,
| Newton's Method: f(x) = 2 - x2 (sqrt(2) in red) | |
|---|---|
| x0: Old guess | x1 = x0/2 + 1/x0: New guess |
| 1.000000000000000 | 1.500000000000000 = 3/2 |
| 1.500000000000000 | 1.416666666666666 = 17/12 |
| 1.416666666666666 | 1.414215686274509 = 577/408 |
| 1.414215686274509 | 1.414213562374689 = 665857/470832 |
| 1.414213562374689 | 1.414213562373095 = 886731088897/627013566048 |
So, put the formula into a loop. Start with x0 = 1. In the loop calculate the new, better value x1 = 1. Finally, set x0 = x1, so the next time the loop is executed, the new guess will be the old guess again.
You might try terminating the loop when fabs(x0 - x1) < 0.00000000001, perhaps using the break statement that we discussed in class.