A5 · Lesson 35 · Lab: Newton's method in code
A root finder you can trust, and the one case it can't handle
Every circuit simulator, at its heart, solves equations with Newton's method: guess, follow the tangent, repeat. It's fast because the number of correct digits roughly doubles at each step. It also fails in recognizable ways when started badly. In this lab you'll write a Newton solver that knows when to stop and when to give up, use it on a diode circuit, measure how fast it converges, and watch it fail on a function where the tangent points the wrong way.
- The curve is , whose only root is 0. Newton's method from draws a tangent and jumps to where it hits zero.
- Start at = 1 and the steps close in on 0. Start beyond about 1.39 and they overshoot farther each time.
- The readout is the first Newton step from your . When is it farther from 0 than itself?
Predict
Newton's method finds from . The error after one step is 0.086. About what is it after the next step?
About 0.0025. Newton's method converges quadratically: each error is about times the square of the last, here . The next error is about , then : the correct digits double each step.
Name it
The method
To solve , repeat
until the step is below a tolerance.
When to stop, and when to give up
A good solver has three ways out:
- converged: the step is smaller than the tolerance;
- too many steps: it hasn't converged after a maximum count, so something is wrong;
- blowing up: has grown absurdly large, or (a flat tangent never reaches zero).
Returning None for the last two, instead of a wrong number, lets the caller try something else, such as bisection.
How fast
Near a simple root, the errors satisfy with at the root: quadratic convergence. One way to see it in data is the ratio , which approaches 2 as the errors shrink. Bisection, by contrast, halves the error each step, a ratio .
The diode circuit
A source through a resistor into a diode gives . Its exponential makes a bad first guess dangerous: starting at 1 V, is , and the first step lands far away. Starting near 0.6 to 0.8 V, Newton converges in a handful of steps.
Worked example
Run Newton's method on from and list the errors.
Show the solution
- Iterate and list the errors. Each row is one Newton step and its distance from :
| error | ||
|---|---|---|
| 1 | 1.5 | |
| 2 | 1.416 667 | |
| 3 | 1.414 216 | |
| 4 | 1.414 214 |
- Compare each error with the one before. Each is about times the square of the one before, the constant .
Answer the errors are , , and , each about 0.35 times the square of the last.
Worked example
A 5 V source drives a 1 kΩ resistor and a diode with A and = 25.85 mV. Solve for the diode's voltage by Newton's method from 0.8 V.
Show the solution
- The function.
- Its derivative.
- The first steps. From 0.8 V the diode's current is enormous, so the first steps walk down by about one each, then converge fast once the current is in range.
- Result. After 10 steps the voltage settles at 0.692 49 V, with 4.31 mA flowing.
- Compare with bisection. Bisection on needs about 40 steps for the same precision.
Answer the diode's voltage is 0.692 49 V, with 4.31 mA flowing.
Check
Near a root, Newton's errors follow with = 0.35. If = 0.05, what is ?
Check
Newton's method on starts at = 1. Where does the first step land?
Check
Write newton(f, df, x0, tol=1e-12, maxit=50) returning (root, steps). Stop when a step is smaller than tol. Return (None, steps) if it runs out of steps, if , or if exceeds . Set r = newton(lambda x: x*x - 2, lambda x: 2*x, 1.0).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use your newton (copied into the starter) to find a diode's voltage: a source Vs = 5 V through R = 470 Ω, A, = 0.025 85 V, starting from 0.8 V. Set Vd to the voltage and I to the current through the resistor.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Measure the order of convergence. Run Newton on from 1 for four steps, recording the errors in a list e. Set orders to the list of for consecutive pairs, and bisect_ratio to the error ratio of bisection, , measured from two bisection steps on (take as half the bracket width).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Find where Newton's method on stops working. Write converges(x0) returning True if newton (in the starter) finds the root 0 from x0. Then bisect on x0 between 1 and 2 (40 halvings) to set edge to the starting point where it switches from converging to diverging, and ok = converges(x0) for x0 = 1.3.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write safe_newton(f, df, a, b, x0): keep a bracket where changes sign. Take a Newton step from the current point; if it lands outside the bracket (or the slope is zero), take the bracket's midpoint instead. Then shrink the bracket to the half with the sign change containing the new point, and stop when a step is below . Return the root.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Newton's method roughly doubles the correct digits each step near a root, yet a simulator sometimes fails to converge. How can both be true?
The doubling is a local promise: it holds once the guess is close enough to the root that the function looks like its tangent line over the distance still to go. Far from the root, the tangent can be a poor guide. A nearly flat tangent sends the next guess far away, as with beyond about 1.39, where every step overshoots more than the last. An exponential like a diode's makes this worse: from a guess a few tenths of a volt too high, the predicted current is astronomically large and the first steps are tiny or wild. So simulators combine Newton's speed near the answer with safeguards for getting there: limiting how far one step can move a junction voltage, falling back to bisection when a bracket is known, or ramping the sources up from zero so each solve starts near the last answer.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.