A1353 of 55
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A13 · Lesson 53 · Project: a nonlinear circuit simulator

Nodal analysis, Newton's method and implicit time steps, assembled into a small SPICE

Every method in the second half of this course meets in a circuit simulator. Nodal analysis turns a circuit into a sparse linear system. Diodes make it nonlinear, so each solve becomes a Newton iteration with companion models and step limiting. Capacitors make it dynamic, and because real circuits are stiff, time steps are implicit: backward Euler turns each capacitor into another companion model. In this project you'll build all three layers in a hundred lines of Python and use them to simulate a half-wave rectifier with a smoothing capacitor, the first circuit in most power supplies.

Predict

A 5 V, 50 Hz sine drives a diode through 10 Ω into 100 µF in parallel with 1 kΩ. About what's the capacitor's peak voltage once it has charged?

Name it

Layer 1: linear nodal analysis

Number the nodes 1 to n, with 0 as ground. A resistor R between a and b adds 1/R to Gaa and Gbb and subtracts it from Gab and Gba (skipping ground's row and column). A current source pushing I into node a adds I to ia. Solve Gv=i. A voltage source with a series resistor is entered as its Norton equivalent: a current V/R into its node and R to ground.

Layer 2: diodes by Newton's method

A diode from a to b with voltage V=va−vb carries Is(eV/nVT−1). Linearized at the current guess, it's a conductance gd in parallel with a current source Ieq=Id−gdV (lesson 34). Each iteration stamps the resistors, sources and the diodes' companion models, solves, and repeats until the change is below a tolerance. To keep the exponential from overflowing, limit each iteration's change of every node voltage to 0.1 V.

Layer 3: transients by backward Euler

A capacitor C between a and b, with step h and previous voltage Vn, becomes a conductance C/h in parallel with a current source CVn/h pushing current toward a (lesson 43). At each time step, update the sources to tn+1, run the Newton loop of layer 2 with the capacitors' companion models included, and store the result.

Data used in the tests

Is=10−14 A, nVT = 0.02585 V. The two-diode circuit of lesson 34 settles at V1=0.691903 V, V2=0.594530 V.

Worked example

Write the nodal matrix for 1 kΩ from node 1 to ground, 2 kΩ from node 1 to node 2, and 3 kΩ from node 2 to ground.

Show the solution
  1. Stamp each resistor. Each resistor contributes to the diagonal of each node it touches and to the off-diagonals between them; ground's row and column are left out.
  2. The matrix. G=(11000+12000−12000−1200012000+13000)

Answer the diagonal entries are 11000+12000 and 12000+13000 S, and both off-diagonal entries are −12000 S.

Worked example

A capacitor of 100 µF at 4 V is simulated with h = 0.1 ms. What's its companion model?

Show the solution
  1. The conductance. C/h=10−4/10−4=1S
  2. The current source. In parallel sits a source of CVn/h=4A into the node at its positive end.
  3. Check. With nothing else attached, the next solve gives back 4 V, as it should for an isolated capacitor.

Answer a 1 S conductance in parallel with a 4 A current source into the capacitor's positive node.

Check

Step 1. Write solve_linear(n, resistors, sources): resistors is a list of (a, b, R), sources a list of (node, current_in), node 0 is ground. Return the node voltages as a NumPy array v with v[0] for node 1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 2. Write solve_dc(n, resistors, sources, diodes) with Newton's method: diodes is a list of (a, b) (anode, cathode). Start from all zeros, limit each node's change to 0.1 V per iteration, stop when the largest change is below 10−12 V. Return (v, iterations). stamp_r is included.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Step 3. Write rectifier(h, t_end): a 5 V, 50 Hz sine through 10 Ω (as its Norton equivalent into node 1), a diode from node 1 to node 2, and 100 µF and 1 kΩ from node 2 to ground. Use backward Euler with step h, starting from rest, and the Newton loop of step 2 at every step (starting from the previous solution). Return the lists ts and v2s (node 2's voltage). solve_dc is included and accepts v= (a start) and extra= (a function that stamps more into G and i).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

What conductance stands for a 100 µF capacitor with a 20 µs backward-Euler step?

Write units the way you normally would: 1.5 S, 1500 mS and 0.0015 kS are the same answer.

Check

A current of 10 mA flows into a node with 1000 Ω and 2000 Ω to ground. What's the node voltage?

Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.

Explain why

Why do circuit simulators solve each time step with Newton's method on a linear "companion" circuit?

Practice

Keep practicing

5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.