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

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?

Name it

The tools, and how each one fails

ToolUseWatch for
Newton's methodnonlinear parts: diodes, transistorsovershoot on exponentials; limit steps or change variables
Stamped matrices, np.linalg.solvethe network, DC or ACa floating node makes the matrix singular
Complex arithmeticAC steady state, ZC=1/(jωC)forgetting the 1j turns a reactance into a resistance
Trapezoid rulecharge, energy, averagestoo few points on a fast decay
RK4 steppingtransientsa 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 p the change is 2p−1 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 (Is=10−14 A, VT = 25.85 mV) across 5 V three ways: Newton on the diode voltage from 0.6 V, the same with each step limited to 2VT, and Newton on the current using Vs=IR+VTln⁡(I/Is+1).

Show the solution
  1. Write the equation and its slope. f is the diode current minus the resistor current, zero at the answer, and g is the same circuit written for the current.
  2. Run the three versions. Plain Newton on the voltage, the same with each step limited to 2VT, 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
  1. Compare the results. All three land on 0.69249 V and 4.3075 mA.
  2. Compare the step counts. Plain Newton takes 32 steps, limiting the step to 2VT takes 7, and solving for the current takes 4.
  3. 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
  1. Turn the source into a Norton current. The source becomes a current of 1 V/1 kΩ in parallel with the 1 kΩ, leaving one node.
  2. Stamp the capacitor like a conductance. It adds jωC 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
  1. Read the output. v=0.6855−j0.3915V=0.789V∠−29.7°
  2. Check with the divider formula. Zp/(R+Zp), with Zp 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 (Is=10−14 A, VT = 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 v2/R from 0 to 5τ with the trapezoid rule on n = 160 intervals, and set E_trap, E_exact (the exact integral over the same interval, 12CV02(1−e−10)) 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 dv/dt=(1−v)/τ from v=0 to t=5 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 1−e−5/τ.

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 1/(1+jω(R1C1+R2C2+R1C2)−ω2R1R2C1C2) 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, v←u+(v−u)e−Δt/τ with u 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 Vtanh⁡(T/(4τ)).

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?

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.