A535 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 f(x)=tan−1⁡x, whose only root is 0. Newton's method from x0 draws a tangent and jumps to where it hits zero.
  • Start at x0 = 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 x0. When is it farther from 0 than x0 itself?

Predict

Newton's method finds 2 from x0=1. The error after one step is 0.086. About what is it after the next step?

Name it

The method

To solve f(x)=0, repeat

xn+1=xn−f(xn)f′(xn)

until the step |xn+1−xn| 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: |x| has grown absurdly large, or f′(x)=0 (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 en+1≈Cen2 with C=|f′′|2|f′| at the root: quadratic convergence. One way to see it in data is the ratio ln⁡en+1ln⁡en, which approaches 2 as the errors shrink. Bisection, by contrast, halves the error each step, a ratio en+1/en=12.

The diode circuit

A source Vs through a resistor R into a diode gives f(V)=Vs−VR−Is(eV/VT−1)=0. Its exponential makes a bad first guess dangerous: starting at 1 V, eV/VT is e38.7, 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 x2−2 from x0=1 and list the errors.

Show the solution
  1. Iterate and list the errors. Each row is one Newton step and its distance from 2:
nxnerror
11.58.6×10−2
21.416 6672.5×10−3
31.414 2162.1×10−6
41.414 2141.6×10−12
  1. Compare each error with the one before. Each is about 0.35 times the square of the one before, the constant 122.

Answer the errors are 8.6×10−2, 2.5×10−3, 2.1×10−6 and 1.6×10−12, 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 Is=10−14 A and VT = 25.85 mV. Solve for the diode's voltage by Newton's method from 0.8 V.

Show the solution
  1. The function. f(V)=5−V1000−10−14(eV/0.02585−1)
  2. Its derivative. f′(V)=−0.001−10−140.02585eV/0.02585
  3. The first steps. From 0.8 V the diode's current is enormous, so the first steps walk down by about one VT each, then converge fast once the current is in range.
  4. Result. After 10 steps the voltage settles at 0.692 49 V, with 4.31 mA flowing.
  5. Compare with bisection. Bisection on [0,1] 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 en+1≈Cen2 with C = 0.35. If en = 0.05, what is en+1?

Check

Newton's method on tan−1⁡x starts at x0 = 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 f′(x)=0, or if |x| exceeds 1010. 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 Ω, Is=10−14 A, VT = 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 x2−2 from 1 for four steps, recording the errors en=|xn−2| in a list e. Set orders to the list of ln⁡en+1/ln⁡en for consecutive pairs, and bisect_ratio to the error ratio of bisection, en+1/en, measured from two bisection steps on [1,2] (take e 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 tan−1⁡x 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 [a,b] where f 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 10−12. 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?

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.