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

C1 · Lesson 25 · Nodal solver lab

Build a circuit solver in NumPy from a netlist

Every circuit simulator, from SPICE to the sims in this course, does what you did by hand in lesson 9: write KCL at every node and solve the linear equations. The difference is that it builds the equations automatically from a list of parts. Each resistor adds the same small pattern of four numbers to a matrix, a "stamp", wherever its two nodes are. In this lab you'll write that stamping loop, handle current sources and then voltage sources, and use the solver to find a resistance that no series-parallel trick can.

  • A divider driving a load from a 12 V source, solved by the course's own circuit engine, which builds and solves exactly the matrix you'll build.
  • Every slider change rebuilds the conductance matrix and solves it again, in microseconds.
  • Predict the output for a 20 kΩ load before checking the meter.

Predict

Nodal analysis of a circuit with 50 nodes (plus ground) and only resistors and current sources. How big is its conductance matrix?

Name it

The stamp

Number the nodes 1 to n, with 0 for ground. KCL at every node is 𝐆𝐯=𝐢 (lesson 9). A resistor of conductance g=1/R between nodes a and b adds to 𝐆:

column acolumn b
row a+g−g
row b−g+g

leaving out any row or column for ground. A current source of I flowing from a through the source into b subtracts I from ia and adds it to ib. Add every part's stamp and solve.

Voltage sources: modified nodal analysis

A voltage source fixes a voltage difference but its current is unknown, so it needs a new unknown, its current, and a new equation, va−vb=V. Modified nodal analysis (MNA) grows the matrix by one row and column per source:

[𝐆𝐁𝐁T0][𝐯𝐣]=[𝐢𝐕]

where column k of 𝐁 has +1 in row a and −1 in row b for source k. Each jk comes out as the current flowing into the source's + terminal from the circuit; a negative value means the source is delivering power. SPICE builds exactly this.

Equivalent resistance

To find the resistance between two nodes, inject 1 A in at one and out at the other and solve: the voltage difference, in volts, is the resistance in ohms. This works for any network, bridges included (lesson 11).

In NumPy

import numpy as np
G = np.zeros((n, n))      # n × n of zeros
G[a - 1, b - 1] += -g     # node a is row a - 1: Python counts from 0
v = np.linalg.solve(G, i) # solve G v = i

Worked example

Write the conductance matrix for a 2 mA current source into node 1, with 1 kΩ from node 1 to ground, 1 kΩ from node 1 to node 2, and 2 kΩ from node 2 to ground.

Show the solution
  1. Node 1's row. In mS, node 1 has 1+1=2 on the diagonal and −1 to node 2.
  2. Node 2's row. Node 2 has 1+0.5=1.5 on the diagonal.
  3. The system. In mA: [2−1−11.5]𝐯=[20]
  4. Solve. v1=1.5Vv2=1.0V

Answer the conductance matrix is [2−1−11.5] mS, giving v1 = 1.5 V and v2 = 1.0 V.

Worked example

Lesson 11's bridge, driven by 10 V at its input, solved by MNA. What are the node voltages and the source's current?

Show the solution
  1. Node voltages. With nodes 1 (input), 2 (a) and 3 (b), the solver gives v1=10Vv2=7.869Vv3=7.377V
  2. Source current. The solver gives −0.34426 A: 0.344 A flows out of its + terminal.
  3. Input resistance. Rin=100.34426=29.05Ω matching the delta-wye calculation.

Answer the nodes sit at 10 V, 7.869 V and 7.377 V, and the source current is −0.344 A, which makes the input resistance 29.05 Ω.

Check

A circuit has 50 nodes besides ground. How many entries does its full conductance matrix have?

Check

A 2.2 kΩ resistor joins nodes 2 and 3. What does it add to the entry in row 2, column 3?

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

Check

A circuit has 10 non-ground nodes and 1 voltage sources. How many unknowns does modified nodal analysis solve for?

Check

A node connects to ground through 1 mS and 0.5 mS, and a 3 mA source feeds it. What is its voltage?

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

Check

Write nodal(netlist, n): each entry is ('R', a, b, ohms) or ('I', a, b, amps) (current flowing from node a through the source into node b), with node 0 as ground. Build 𝐆 and 𝐢 and return the voltages of nodes 1 to n as an array.

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

Check

Write mna(netlist, n), accepting ('V', a, b, volts) (with a the + terminal) as well as resistors and current sources. Return (v, j): the node voltages and the voltage sources' currents, in the order the sources appear.

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

Check

Write req(netlist, n, a, b=0): the resistance between nodes a and b of a resistor network, found by injecting 1 A into a and out of b with your nodal.

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

Explain why

Why does modified nodal analysis add an unknown for each voltage source, instead of just writing KCL at every node?

Practice

Keep practicing

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