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

A7 · Lesson 12 · Solving Ax = b

Writing a circuit as a matrix and letting the computer solve it

A resistor ladder with three junctions is about as far as most people go by hand. Real circuits have hundreds of nodes, and their equations are solved the same way every time: write the conductance matrix by inspection, write the vector of injected currents, and hand both to a solver. This lesson is that recipe, in code.

Predict

To solve G𝐯=𝐢 in code, which is better: np.linalg.solve(G, i) or np.linalg.inv(G) @ i?

Name it

The recipe for a resistor circuit

  1. Pick a ground node and number the others 1 to n.
  2. Build G by inspection: each diagonal entry Gkk is the sum of the conductances touching node k; each off-diagonal entry Gjk is minus the conductance joining j and k (zero if they aren't joined). G is symmetric.
  3. Build 𝐢: the current pushed into each node by current sources. A voltage source in series with a resistor R can be swapped for a current source V/R in parallel with R (A source transformation, C1), which is how the ladder below gets its right-hand side.
  4. Solve G𝐯=𝐢 for the node voltages.
  5. Check: compute the residual G𝐯−𝐢, which should be zero to rounding error, and branch currents from voltage differences.

For a ladder fed by Vs through R1, with shunt resistors R2, R4, R6 to ground at nodes 1, 2, 3 and series resistors R3 (1 to 2) and R5 (2 to 3):

G=[1R1+1R2+1R3−1R30−1R31R3+1R4+1R5−1R50−1R51R5+1R6]𝐢=[Vs/R100]

The zeros in the corners are typical: each node touches only a few others, so real conductance matrices are mostly zeros (sparse), and simulators store and solve them in ways that skip the zeros.

Worked example

A two-node circuit has G=[3−1−12] mS and 𝐢=(10,0) mA. Find 𝐯.

Show the solution
  1. Determinant. det⁡G=6−1=5
  2. Inverse. G−1=15[2113]kΩ
  3. Multiply. 𝐯=15(20,10)=(4,2)V
  4. In NumPy. np.linalg.solve(G, i) with G in mS and 𝐢 in mA gives the same numbers in volts.

Answer 𝐯=(4,2) V.

Worked example

For a 1000 × 1000 system, how does solve compare with inv(G) @ i?

Show the solution
  1. Cost of solve. It factors once, about n33≈3×108operations
  2. Cost of the inverse. Forming the inverse takes about n3=109, three times as much, and then a multiplication on top.
  3. Accuracy. The inverse is also less accurate.

Answer solve wins on both counts: about a third of the work, and a more accurate answer.

Worked example

A three-node ladder has G=[2−10−12−10−12] mS, with 1 mA injected at node 1. Find the node voltages.

Show the solution
  1. Eliminate below the first pivot. Solving G𝐯=(1,0,0), row 2 + ½ row 1 gives 1.5v2−v3=0.5
  2. Eliminate below the second. Row 3 + ⅔ of that gives 43v3=13
  3. Back-substitute. v3 = 0.25 V, v2 = 0.5 V and v1 = 0.75 V.
  4. The pattern. The voltage falls by a quarter volt per node along the ladder. In Python, np.linalg.solve(G, [1, 0, 0]) gives the same three numbers.

Answer v1 = 0.75 V, v2 = 0.5 V and v3 = 0.25 V.

Check

Build G for the ladder above with all six resistors equal to R = 2.2 kΩ and Vs = 10 V, build i, and solve for v, the three node voltages.

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

Check

Now with all six resistors different (they're defined as R1 to R6) and Vs = 10 V: solve for v, then set I3 to the current through R3 from node 1 to node 2, and resid to the largest absolute entry of G𝐯−𝐢.

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

Check

Use the 2 × 2 inverse to solve [2115][xy]=[813] for x.

Check

A circuit has G=[2+1−1−12] mS, with 1 mA injected at node 1.

(a)

What is v1?

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

(b)

What is v2?

Check

Node 1 has g mS to ground and 1 mS to node 2; node 2 has 1 mS to ground; 1 mA flows into node 1.

Step 1

G=[g+1−1−12]. What is det⁡G?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Check

The three-node ladder above, G=[2−10−12−10−12] mS, has G−1=14[321242123] kΩ. With 2 mA injected at node 2 and nothing else, what is the voltage at node 3?

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

Explain why

Why does a circuit with n nodes (besides ground) give an n × n conductance matrix?

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.