A4 · Lesson 43 · Lab: numerical methods for circuits
Solving, integrating and stepping circuits, and knowing when to trust the answer
A circuit simulator is lessons 36 to 42 bolted together: complex numbers for AC, Newton's method for diodes and transistors, stamped matrices for the network, integration for energy and charge, and time stepping for transients. Each piece can fail in its own way: Newton can wander, a step can be too big, a sum can miss half its area. This lab runs each one on a circuit with a known answer, so you can see it work, and sees one of them fail and fixes it.
Predict
Newton's method solves a 1 kΩ resistor and a silicon diode across 5 V, iterating on the diode voltage from a guess of 0.6 V. The answer is 0.692 V. About how many steps does it take?
About 30: 32 steps to twelve digits. The diode's current grows as , so at 0.6 V the curve is nearly flat and the tangent line overshoots to 1.36 V. From there each step only comes back by about = 26 mV, because the exponential is so steep. Newton's fast convergence only starts once it's close. Limiting each step to a couple of , or solving for the current instead, cuts this to a handful of steps.
Name it
The tools, and how each one fails
| Tool | Use | Watch for |
|---|---|---|
| Newton's method | nonlinear parts: diodes, transistors | overshoot on exponentials; limit steps or change variables |
Stamped matrices, np.linalg.solve | the network, DC or AC | a floating node makes the matrix singular |
| Complex arithmetic | AC steady state, | forgetting the 1j turns a reactance into a resistance |
| Trapezoid rule | charge, energy, averages | too few points on a fast decay |
| RK4 stepping | transients | a step too long for the fastest time constant |
Checking without knowing the answer
- Residuals: put the answer back into the equation it should satisfy.
- Step halving: run again with half the step; if the answer barely changes, the step was small enough. For a method of order the change is times the remaining error.
- A second method: bisection after Newton, an exact formula on a simpler case, energy conservation.
Worked example
Solve a 1 kΩ resistor and a diode ( A, = 25.85 mV) across 5 V three ways: Newton on the diode voltage from 0.6 V, the same with each step limited to , and Newton on the current using .
Show the solution
- Write the equation and its slope.
fis the diode current minus the resistor current, zero at the answer, andgis the same circuit written for the current. - Run the three versions. Plain Newton on the voltage, the same with each step limited to , and Newton on the current.
import math
Is, VT, Vs, R = 1e-14, 0.02585, 5.0, 1e3
f = lambda v: Is * (math.exp(v / VT) - 1) - (Vs - v) / R
df = lambda v: Is / VT * math.exp(v / VT) + 1 / R
def newton_v(v, limit=None):
for k in range(200):
step = f(v) / df(v)
if limit and abs(step) > limit:
step = math.copysign(limit, step)
v -= step
if abs(step) < 1e-12:
return v, k + 1
print(newton_v(0.6)) # (0.69249, 32)
print(newton_v(0.6, 2 * VT)) # (0.69249, 7)
g = lambda I: I * R + VT * math.log(I / Is + 1) - Vs
dg = lambda I: R + VT / (I + Is)
I = Vs / R
for n in range(1, 50):
step = g(I) / dg(I)
I -= step
if abs(step) < 1e-15:
break
print(I, n) # 0.0043075, 4
- Compare the results. All three land on 0.69249 V and 4.3075 mA.
- Compare the step counts. Plain Newton takes 32 steps, limiting the step to takes 7, and solving for the current takes 4.
- Why the current form is quickest. In terms of current the equation is gentle: a logarithm instead of an exponential. SPICE limits its junction steps for exactly this reason.
Answer all three give 0.69249 V and 4.3075 mA, in 32, 7 and 4 steps.
Worked example
A 1 kΩ resistor feeds a 100 nF capacitor, loaded by 10 kΩ, from 1 V at 1 kHz. Find the output by complex nodal analysis and check it against the impedance formula.
Show the solution
- Turn the source into a Norton current. The source becomes a current of in parallel with the 1 kΩ, leaving one node.
- Stamp the capacitor like a conductance. It adds to the node's 1 kΩ and 10 kΩ conductances.
import numpy as np
w = 2 * np.pi * 1e3
G = np.array([[1 / 1e3 + 1j * w * 100e-9 + 1 / 10e3]])
i = np.array([1 / 1e3]) # the source and 1 kΩ, as a Norton current
v = np.linalg.solve(G, i)[0]
print(abs(v), np.degrees(np.angle(v))) # 0.7894, -29.73
- Read the output.
- Check with the divider formula. , with the capacitor in parallel with the load, gives the same number, so the stamping is right.
Answer the output is 0.789 V at −29.7°.
Check
A resistor R = 220 Ω and a diode ( A, = 0.02585 V) are in series across Vs = 5 V. Write newton_v(v0, limit=None) from the first example, returning (v, steps). Set plain = newton_v(0.6), limited = newton_v(0.6, 2 * VT), and I to the diode current from the limited answer.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A C = 10 µF capacitor charged to V0 = 5 V discharges through 1 kΩ. Integrate the power from 0 to with the trapezoid rule on n = 160 intervals, and set E_trap, E_exact (the exact integral over the same interval, ) and rel to the relative error.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Integrate from to with RK4 (tau = 1), starting with 5 steps and doubling the number until two successive answers differ by less than tol = 1 × 10⁻⁶. Set n to the final number of steps, v_end to that answer, and err to its true error against .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
An RC ladder has R1 = 1000 Ω from a 1 V source to node 1, C1 = 47 nF from node 1 to ground, R2 = 1000 Ω from node 1 to node 2 and C2 = 22 nF from node 2 to ground. At f = 300 Hz, build the 2 × 2 complex nodal matrix (the source through R1 as a Norton current into node 1) and set v2 to the complex voltage at node 2. Then set formula to to compare.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A square wave switching between 0 and V = 5 V with period T = 1 ms and 50% duty drives an RC with tau = 1 ms. Step it exactly, with the input, using 1000 steps per period, for 40 periods from 0 V. Over the last period, set vpp to the capacitor voltage's peak-to-peak, and exact to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
An RK4 simulation has an error of 5 × 10⁻⁶ with some step. What error should it have with the step 2 times smaller?
Explain why
Newton's method converges in a handful of steps on most problems but took 32 on the diode. What is it about the diode's equation that slows it down, and why does solving for the current instead fix it?
Newton replaces the function by its tangent line and jumps to where the tangent crosses zero. The diode's current is exponential in its voltage, so at a voltage below the answer the curve is nearly flat and the tangent shoots far past it, to 1.36 V; up there the curve is so steep that each new tangent only reaches back about one thermal voltage, so the return takes dozens of steps. Written in terms of current, the diode voltage is a logarithm, a gently curving function whose tangent is a good guide everywhere, so Newton takes the direct route. The answer is the same; only the shape of the function Newton walks along changes.
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.