TExcelTAD
Home › Circuit Theory & Network Analysis › Modified Nodal Analysis: The Matrix Method of SPICE

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 (v+−v−=Vv_+ - v_- = V) 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.

+ - V1 5 V R1 1 kOhm R2 2 kOhm R3 4 kOhm + - E1 2 v2 R4 1 kOhm 1 2 3 E1 senses v2 (node 2 to ground) and drives node 3 with 2 v2

The block structure

With nn non-reference nodes and mm voltage-defined elements, the unknowns are the node voltages v\mathbf{v} and the branch currents j\mathbf{j} of those elements:

[GBCD][vj]=[ie]\begin{bmatrix} G & B \\ C & D \end{bmatrix} \begin{bmatrix} \mathbf{v} \\ \mathbf{j} \end{bmatrix} = \begin{bmatrix} \mathbf{i} \\ \mathbf{e} \end{bmatrix}
  • GG (n×nn \times n): the conductance matrix of plain nodal analysis, from resistors (and VCCS entries).
  • BB (n×mn \times m): where each branch current enters KCL: +1+1 in the row of its ++ node, −1-1 in its −- node.
  • CC (m×nm \times n): each element's voltage equation in node voltages; C=BTC = B^T for independent sources.
  • DD (m×mm \times m): zero for ideal sources; nonzero for current-controlled sources.
  • i\mathbf{i}: independent current sources into each node; e\mathbf{e}: independent source voltages.

The stamps

Write pp, qq for an element's nodes (rows and columns of ground are simply dropped) and kk for its extra row and column.

Element (SPICE line) Entries added
resistor R p q R Gpp+=gG_{pp} \mathrel{+}= g, Gqq+=gG_{qq} \mathrel{+}= g, Gpq−=gG_{pq} \mathrel{-}= g, Gqp−=gG_{qp} \mathrel{-}= g, with g=1/Rg = 1/R
current source I p q I (flows from pp through it to qq) ip−=Ii_p \mathrel{-}= I, iq+=Ii_q \mathrel{+}= I
voltage source V p q V Bpk=+1B_{pk} = +1, Bqk=−1B_{qk} = -1, Ckp=+1C_{kp} = +1, Ckq=−1C_{kq} = -1, ek=Ve_k = V
VCVS E p q cp cq mu as a voltage source with ek=0e_k = 0, plus Ck,cp−=μC_{k,cp} \mathrel{-}= \mu, Ck,cq+=μC_{k,cq} \mathrel{+}= \mu
VCCS G p q cp cq gm Gp,cp+=gmG_{p,cp} \mathrel{+}= g_m, Gp,cq−=gmG_{p,cq} \mathrel{-}= g_m, Gq,cp−=gmG_{q,cp} \mathrel{-}= g_m, Gq,cq+=gmG_{q,cq} \mathrel{+}= g_m

The VCVS row reads vp−vq−μ(vcp−vcq)=0v_p - v_q - \mu (v_{cp} - v_{cq}) = 0. The branch current jkj_k is defined flowing from pp through the element to qq, 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 v3=2v2v_3 = 2 v_2), R3 3 2 4k, R4 3 0 1k.

Find: the MNA system, the node voltages and the source currents.

Solve: unknowns v1,v2,v3,jV1,jE1v_1, v_2, v_3, j_{V1}, j_{E1}; units kΩ\text{k}\Omega, mA, V, so conductances are in mS. Stamping line by line:

[1−1010−11.75−0.25000−0.251.2501100000−2100][v1v2v3jV1jE1]=[00050]\begin{bmatrix} 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 \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \\ v_3 \\ j_{V1} \\ j_{E1} \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \\ 5 \\ 0 \end{bmatrix}

Row 4 gives v1=5 Vv_1 = 5\ \text{V}; row 5 gives v3=2v2v_3 = 2 v_2. Row 2 then reads −5+1.75v2−0.5v2=0-5 + 1.75 v_2 - 0.5 v_2 = 0, so v2=4 Vv_2 = 4\ \text{V} and v3=8 Vv_3 = 8\ \text{V}. Row 1: 5−4+jV1=05 - 4 + j_{V1} = 0, so jV1=−1 mAj_{V1} = -1\ \text{mA}. Row 3: −1+10+jE1=0-1 + 10 + j_{E1} = 0, so jE1=−9 mAj_{E1} = -9\ \text{mA}.

Check: both source currents are negative: both deliver power (5⋅1=5 mW5 \cdot 1 = 5\ \text{mW} and 8⋅9=72 mW8 \cdot 9 = 72\ \text{mW}). Resistors: 1+8+4+64=77 mW1 + 8 + 4 + 64 = 77\ \text{mW}. KCL at node 2: R1R_1 brings 1 mA1\ \text{mA}, R3R_3 brings (8−4)/4=1 mA(8 - 4)/4 = 1\ \text{mA}, R2R_2 takes 4/2=2 mA4/2 = 2\ \text{mA}.

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).

python · try it yourself
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.

bash · try it yourself
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 v2v_2 climb: the feedback through R3R_3 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, v2=5/(1.75−1.125)=8 Vv_2 = 5/(1.75 - 1.125) = 8\ \text{V}.

node voltages (to ground 0)15.000 V24.000 V38.000 VnetlistV1 1 0 DC 5R1 1 2 1kR2 2 0 2kE1 3 0 2 0 2R3 3 2 4kR4 3 0 1kelement currents (node 1 to node 2 through it)V11>0-1.00 mAR11>21.00 mAR22>02.00 mAE13>0-9.00 mAR33>21.00 mAR43>08.00 mA

At the default gain of 2 it shows v2=4 Vv_2 = 4\ \text{V}, v3=8 Vv_3 = 8\ \text{V}, I(V1)=−1 mAI(V_1) = -1\ \text{mA} and I(E1)=−9 mAI(E_1) = -9\ \text{mA}, the MNA unknowns above.

Guided lab: an amplifier block as a VCVS

Hand calculation. In general, row 2 gives v2(1+0.5+0.25−0.25μ)=5v_2 (1 + 0.5 + 0.25 - 0.25\mu) = 5, so v2=5/(1.75−0.25μ)v_2 = 5/(1.75 - 0.25 \mu): 4 V4\ \text{V} for μ=2\mu = 2, 5 V5\ \text{V} for μ=3\mu = 3.

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 ±12 V\pm 12\ \text{V} current-limited supply, feed V1=2.5 VV_1 = 2.5\ \text{V} instead of 5 V so node 3 stays at 4 V4\ \text{V}, well inside the output swing, and use R4=10 kΩR_4 = 10\ \text{k}\Omega to keep the op-amp's output current small (recompute: v2=2.5/1.25=2.00 Vv_2 = 2.5/1.25 = 2.00\ \text{V}, v3=4.00 Vv_3 = 4.00\ \text{V}; R4R_4 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 ±12 V\pm 12\ \text{V} with 20 mA20\ \text{mA} limits; decouple the op-amp with 100 nF100\ \text{nF} at its supply pins. A real op-amp saturates near its rails: a "solution" with v3v_3 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 pp through the element to qq; 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 AA singular. SPICE reports "singular matrix" or "timestep too small".
  • Control node order. E p q cp cq mu means vp−vq=μ(vcp−vcq)v_p - v_q = \mu (v_{cp} - v_{cq}); swapping cpcp and cqcq inverts the gain.

Practice

  1. How big is the MNA matrix for a circuit with 6 non-reference nodes, 2 independent voltage sources and 1 VCVS?
  2. Write the stamp of a VCCS G 3 0 2 0 0.5m (in mS: gm=0.5g_m = 0.5) into the example's matrix. Which entries change?
  3. Using the general formula above, at what gain μ\mu does v2v_2 become infinite, and why?
Answer
  1. n+m=6+3=9n + m = 6 + 3 = 9: a 9×99 \times 9 matrix.
  2. G3,2+=0.5G_{3,2} \mathrel{+}= 0.5 (row 3, column 2), so it changes from −0.25-0.25 to 0.250.25; the ground entries are dropped.
  3. 1.75−0.25μ=01.75 - 0.25 \mu = 0 at μ=7\mu = 7: the feedback through R3R_3 exactly cancels the conductance at node 2. With v3=μv2v_3 = \mu v_2, row 2 of the 5×55 \times 5 system then no longer determines v2v_2. (The lab's slider stops at 4.5.)

Test yourself

Which elements add an extra unknown (a branch current) in MNA?

  1. resistors
  2. independent current sources
  3. voltage sources and voltage-controlled voltage sources
  4. voltage-controlled current sources

In the block form [G B; C D][G\ B;\ C\ D], for independent voltage sources the block CC equals…

  1. GG
  2. BTB^T
  3. −B-B
  4. the identity

A VCVS E1 3 0 2 0 2 enforces…

  1. v2=2v3v_2 = 2 v_3
  2. v3=2v2v_3 = 2 v_2
  3. i3=2v2i_3 = 2 v_2
  4. v3−v2=2v_3 - v_2 = 2

In the example, the solver returns jE1=−9 mAj_{E1} = -9\ \text{mA}. This means…

  1. E1 absorbs 72 mW
  2. 9 mA flows out of E1's + terminal into node 3, so E1 delivers 72 mW
  3. the circuit is unstable
  4. the VCVS is reversed