Newton's method · Python Programming Class 11–12 | StemPy Lab
Course 2.1·Lesson 2 of 5
Newton's method
Newton's method starts from a guess x, draws the tangent line to the graph at that point, and takes the place where the tangent meets the x axis as the next guess, x minus f of x over the slope f prime of x. Near a simple root the error is squared at every step, so the number of correct digits doubles, and the root of x squared minus 2 is found to full accuracy in 5 steps, where bisection needs about 39 for the same accuracy on a cubic. The method can fail: a start can send it to a cycle, as for x cubed minus 2x plus 2 from 0, and a zero slope makes a division by zero. A safe plan is to find a bracket first, or to use a start that is near the root.
about 40 min1 simulation2 practice questions
Lesson 2 of 5Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
After this lesson you can
explain Newton's method with a picture of a tangent,
write it as a short loop and use it to solve an equation,
compare its speed with bisection, and
say when it fails.
A better guess from a straight line
Bisection is safe but slow: it gains about one digit every three steps. Newton's method is much quicker. It uses what you learned in calculus: close to a point, a curve looks like its tangent line.
You have a guess x0 for the root. At that point, the curve has the value f(x0) and the slope f′(x0). The tangent line is a straight line, and you can find where a straight line meets the axis. That place is much closer to the root than x0 was, if the curve is not too wild. Take it as the next guess, x1, and repeat.
The graph of f of x equal to x squared minus 2 near its root, 1.414. An orange dot x0 on the curve at x equal to 1, where f is minus 1, has a tangent line that meets the x axis at 1.5. A second orange dot x1 on the curve above 1.5 has a tangent line that meets the axis at the green dot x2, close to 1.4167, which is almost on the root. A note says that the tangent meets the axis at the next guess.
The tangent at (x0,f(x0)) has the equation y=f(x0)+f′(x0)(x−x0). It meets the axis where y=0:
x1=x0−f′(x0)f(x0)
In general, the step is xn+1=xn−f′(xn)f(xn).
Square root of 2
For f(x)=x2−2, the derivative is f′(x)=2x. Start with x0=1.
Predict first. The error of the first guess is 0.41. How large will it be after 3 steps? Then run it.
Newton against bisection
Start far away and the first steps are big. Once close, each step about squares the error: 0.01 becomes 0.0001.
When Newton goes wrong
Newton's method has no guarantee, because the tangent can point anywhere.
More ways to fail: a start far from the root can send the guess to a distant part of the graph. A root where the slope is nearly zero makes the method slow. A function with a sharp bend can throw the guess far away.
A good plan is the one that professional programs use: bracket first (find the interval with a change of sign), and then use a method that is quick but cannot leave the bracket. The function brentq of SciPy, which you meet in lesson 5, is exactly such a mixture of bisection and faster methods.
Where does the derivative come from?
You can write it by hand, as we did. If the formula is hard, use a numerical derivative: f′(x)≈hf(x+h)−f(x) for a small h, such as 10−6. It is slightly less accurate, but it needs no work. (Chapter 4 explains derivatives by numbers in detail.)
Common mistakes
A wrong derivative. The method then converges badly or not at all. Test your derivative with a number.
A start that is far from the root. Look at the graph first.
Not checking the slope. If it is zero, stop.
Forgetting the limit on the number of steps. A loop can run for ever, as in the cycle.
Trusting the last number without a check. Put it in the equation.
Practice
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Warm-up. Open the lab Newton's method. You write the method with a limit of steps and a check for the zero slope.
Build. Open the problem Square roots by Newton and the problem When Newton fails.
Remember these
Newton's step is xn+1=xn−f(xn)/f′(xn): the tangent meets the axis.
Near a simple root, the number of correct digits doubles at every step.
It needs the derivative, and has no guarantee: cycles, zero slope and a far start are the ways to fail.
Put a limit on the number of steps, and check the answer.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
For f(x)=x2−9 and x0=4, what is x1?
What does "quadratic convergence" mean?
Why does Newton fail at x0=0 for f(x)=x2+1?
Why is Newton quicker than bisection, and what does it cost?
Enroll free in Solving Equations
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.