Modified Nodal Analysis: The Matrix Method of SPICE
By hand you choose a method per circuit: nodal here, a supernode there, a supermesh somewhere else. A circuit simulator cannot choose; it needs one rule that turns any netlist into a linear system. That rule is modified nodal analysis (MNA), introduced for the circuit simulators of the 1970s and still the core of SPICE, ngspice, LTspice and the ee-netlist lab on this site. It is nodal analysis plus one extra unknown, a branch current, for every element whose current Ohm's law cannot give: voltage sources, voltage-controlled voltage sources and, later, inductors.
The idea
Plain nodal analysis fails on a voltage source because its current is unknown. MNA simply keeps that current as an unknown and adds the source's own equation () as an extra row. No supernodes, no special cases: every element type gets a fixed pattern of entries, called a stamp, that you add into the matrix while reading the netlist line by line.
The block structure
With non-reference nodes and voltage-defined elements, the unknowns are the node voltages and the branch currents of those elements:
- (): the conductance matrix of plain nodal analysis, from resistors (and VCCS entries).
- (): where each branch current enters KCL: in the row of its node, in its node.
- (): each element's voltage equation in node voltages; for independent sources.
- (): zero for ideal sources; nonzero for current-controlled sources.
- : independent current sources into each node; : independent source voltages.
The stamps
Write , for an element's nodes (rows and columns of ground are simply dropped) and for its extra row and column.
| Element (SPICE line) | Entries added |
|---|---|
resistor R p q R |
, , , , with |
current source I p q I (flows from through it to ) |
, |
voltage source V p q V |
, , , , |
VCVS E p q cp cq mu |
as a voltage source with , plus , |
VCCS G p q cp cq gm |
, , , |
The VCVS row reads . The branch current is defined flowing from through the element to , which is why SPICE prints a negative current for a source that delivers power.
Worked example
Given: the circuit above: V1 1 0 5, R1 1 2 1k, R2 2 0 2k, E1 3 0 2 0 2 (so ), R3 3 2 4k, R4 3 0 1k.
Find: the MNA system, the node voltages and the source currents.
Solve: unknowns ; units , mA, V, so conductances are in mS. Stamping line by line:
Row 4 gives ; row 5 gives . Row 2 then reads , so and . Row 1: , so . Row 3: , so .
Check: both source currents are negative: both deliver power ( and ). Resistors: . KCL at node 2: brings , brings , takes .
A complete MNA solver in Python
About forty lines stamp any netlist of R, V, I, E and G elements. The linear algebra is one call to np.linalg.solve; the theory behind it is Gaussian elimination and LU factorisation, which is what SPICE uses (with sparse matrices).
import numpy as np
def mna(netlist):
nodes = sorted({n for e in netlist for n in e[2:4]} - {"0"})
vsrc = [e[1] for e in netlist if e[0] in "VE"]
N, M = len(nodes), len(vsrc)
row = {n: k for k, n in enumerate(nodes)}
A = np.zeros((N + M, N + M)); z = np.zeros(N + M)
def add(r, c, x):
if r is not None and c is not None: A[r, c] += x
r = lambda n: row.get(n) # None for ground
for e in netlist:
kind, name, p, q = e[:4]
if kind == "R":
g = 1 / e[4]
add(r(p), r(p), g); add(r(q), r(q), g); add(r(p), r(q), -g); add(r(q), r(p), -g)
elif kind == "I":
if r(p) is not None: z[r(p)] -= e[4]
if r(q) is not None: z[r(q)] += e[4]
elif kind == "G":
cp, cq, gm = e[4:7]
add(r(p), r(cp), gm); add(r(p), r(cq), -gm); add(r(q), r(cp), -gm); add(r(q), r(cq), gm)
else: # V or E: extra row/column k
k = N + vsrc.index(name)
add(r(p), k, 1); add(r(q), k, -1); add(k, r(p), 1); add(k, r(q), -1)
if kind == "V": z[k] = e[4]
else:
cp, cq, mu = e[4:7]
add(k, r(cp), -mu); add(k, r(cq), mu)
x = np.linalg.solve(A, z)
return A, dict(zip(nodes, x[:N])), dict(zip(vsrc, x[N:]))
net = [("V", "V1", "1", "0", 5.0),
("R", "R1", "1", "2", 1.0), ("R", "R2", "2", "0", 2.0),
("E", "E1", "3", "0", "2", "0", 2.0),
("R", "R3", "3", "2", 4.0), ("R", "R4", "3", "0", 1.0)] # kOhm, V -> mA
A, v, j = mna(net)
np.set_printoptions(precision=3, suppress=True)
print(A)
print({n: round(float(x), 4) for n, x in v.items()}, "V")
print({n: round(float(x), 4) for n, x in j.items()}, "mA")[[ 1. -1. 0. 1. 0. ]
[-1. 1.75 -0.25 0. 0. ]
[ 0. -0.25 1.25 0. 1. ]
[ 1. 0. 0. 0. 0. ]
[ 0. -2. 1. 0. 0. ]]
{'1': 5.0, '2': 4.0, '3': 8.0} V
{'V1': -1.0, 'E1': -9.0} mA
The printed matrix is the one derived by hand. Try adding a line such as ("I", "I1", "0", "2", 1.0) and the solver handles the new source with no new code.
Cross-check with ngspice
ngspice builds the same matrix internally. Its E line has the same order: output nodes, then control nodes, then gain.
cat > mna.cir <<'EOF'
* MNA example: VCVS E1 = 2 * v(2), feedback through R3
V1 1 0 DC 5
R1 1 2 1k
R2 2 0 2k
E1 3 0 2 0 2
R3 3 2 4k
R4 3 0 1k
.control
op
print v(1) v(2) v(3) i(V1)
.endc
.end
EOF
ngspice -b mna.cir 2>&1 | grep -E '^(v\(|i\()'v(1) = 5.000000e+00
v(2) = 4.000000e+00
v(3) = 8.000000e+00
i(v1) = -1.00000e-03
Interactive: the same netlist in the lab
The ee-netlist lab is a small MNA solver written in JavaScript. Raise the gain of E1 and watch climb: the feedback through eats into the conductance of node 2, and at a gain of 7 it would cancel it and make the matrix singular. At the slider's end, 4.5, .
At the default gain of 2 it shows , , and , the MNA unknowns above.
Guided lab: an amplifier block as a VCVS
Hand calculation. In general, row 2 gives , so : for , for .
Simulation. The lab above (slide the gain) and the ngspice deck (edit the last field of E1).
What you would measure. A VCVS of gain 2 is a non-inverting op-amp stage with two equal resistors (Analog Electronics builds it). With a rail-to-rail op-amp on a current-limited supply, feed instead of 5 V so node 3 stays at , well inside the output swing, and use to keep the op-amp's output current small (recompute: , ; does not appear because an ideal VCVS drives any load). Expect agreement within about 1 % with 1 % resistors.
Hazards and safe setup. Supplies at most with limits; decouple the op-amp with at its supply pins. A real op-amp saturates near its rails: a "solution" with above the supply is a sign the linear model no longer applies.
Pitfalls
- Ground row and column. Node 0 has no row; stamps that touch ground drop those entries.
- Current direction. MNA's branch current flows from through the element to ; a delivering source shows a negative value.
- Singular matrices. A loop of ideal voltage sources, a node with no DC path to ground, or a controlled source with exactly the "wrong" gain makes singular. SPICE reports "singular matrix" or "timestep too small".
- Control node order.
E p q cp cq mumeans ; swapping and inverts the gain.
Practice
- How big is the MNA matrix for a circuit with 6 non-reference nodes, 2 independent voltage sources and 1 VCVS?
- Write the stamp of a VCCS
G 3 0 2 0 0.5m(in mS: ) into the example's matrix. Which entries change? - Using the general formula above, at what gain does become infinite, and why?
Answer
- : a matrix.
- (row 3, column 2), so it changes from to ; the ground entries are dropped.
- at : the feedback through exactly cancels the conductance at node 2. With , row 2 of the system then no longer determines . (The lab's slider stops at 4.5.)
Test yourself
Which elements add an extra unknown (a branch current) in MNA?
- resistors
- independent current sources
- voltage sources and voltage-controlled voltage sources
- voltage-controlled current sources
In the block form , for independent voltage sources the block equals…
- the identity
A VCVS E1 3 0 2 0 2 enforces…
In the example, the solver returns . This means…
- E1 absorbs 72 mW
- 9 mA flows out of E1's + terminal into node 3, so E1 delivers 72 mW
- the circuit is unstable
- the VCVS is reversed