Sub topic 13
Singular Matrices and Floating Sub-Circuits
The earlier chapters assumed np.linalg.solve(G, i) always returns an answer. This chapter covers what happens when it doesn't. You should be comfortable with the matrix form G v = i, with building G by stamping (the previous chapter), and with the idea of rank. We look at why G becomes singular, how to detect that before solving, and how to repair the circuit or the model.
What "singular" means for a circuit
A matrix is singular when some nonzero vector x satisfies G x = 0. For nodal analysis this has a physical meaning. If G x = 0, then adding x to the node voltages changes none of the node currents, so the circuit cannot tell the two solutions apart.
The standard case is a floating sub-circuit: a group of nodes connected to each other but not to the reference node. Every resistor in the group depends only on voltage differences. Raise every node in the group by the same amount, and every branch current stays the same. So the group's absolute voltage is undetermined, and the vector x with ones on those nodes is in the null space of G.
You can also see it in the numbers. For a floating group, each row of G restricted to that group has diagonal entries equal to the sum of the conductances touching the node, and off-diagonal entries equal to minus the individual conductances. Every conductance appears once positive and once negative, so each row sums to zero. A matrix whose rows sum to zero maps the all-ones vector to zero, which makes it singular.
A grounded circuit avoids this because a resistor to ground adds to the diagonal without adding a matching negative entry. The row sum becomes positive.
Detecting the problem before solving
The cheapest check is graph connectivity. Treat each resistor as an edge between its two nodes. Start at ground (node 0) and walk across edges. Any node you never reach is in a floating component.
The script below builds G, finds the floating nodes, checks the rank, and shows what solve does. Node 3 is grounded through 1 Ω and driven by 1 A. Nodes 1 and 2 are joined by 2 Ω and connect to nothing else.
import numpy as np
def build_G(n, resistors):
G = np.zeros((n, n))
for a, b, r in resistors:
g = 1.0 / r
for node in (a, b):
if node > 0:
G[node - 1, node - 1] += g
if a > 0 and b > 0:
G[a - 1, b - 1] -= g
G[b - 1, a - 1] -= g
return G
def floating_nodes(n, resistors):
adj = {k: set() for k in range(n + 1)}
for a, b, _ in resistors:
adj[a].add(b)
adj[b].add(a)
seen = {0}
stack = [0]
while stack:
node = stack.pop()
for nxt in adj[node]:
if nxt not in seen:
seen.add(nxt)
stack.append(nxt)
return [k for k in range(1, n + 1) if k not in seen]
resistors = [(1, 2, 2), (3, 0, 1)]
G = build_G(3, resistors)
i = np.array([0.0, 0.0, 1.0])
print(floating_nodes(3, resistors)) # [1, 2]
print(np.linalg.matrix_rank(G)) # 2
try:
np.linalg.solve(G, i)
except np.linalg.LinAlgError as err:
print("solve failed:", err) # solve failed: Singular matrixThe rank is 2 for a 3 by 3 matrix, so one dimension of freedom is missing. Here that dimension is the shared offset of nodes 1 and 2. The graph check is better than the rank check as a diagnostic, because it tells you which nodes are the problem. The rank only tells you how many independent directions are missing.
Two kinds of floating group
The graph check tells you a group floats. It does not tell you whether the problem is solvable. KCL applied to the whole floating group says the net current entering the group must be zero, because no current has anywhere to leave. This gives two cases.
Net injected current is nonzero. Suppose a 1 A source pushes current into node 1 of the floating pair and nothing takes it out. Current would pile up, which KCL forbids. The system G v = i has no solution at all, and the circuit as drawn is physically inconsistent. Fix the model. Usually a return path, a wire, or a ground connection is missing.
Net injected current is zero. Suppose 1 A enters node 1 and leaves node 2. Then G v = i has infinitely many solutions. They all share the same voltage difference, and they differ only by a constant offset. The difference is still meaningful. With the 2 Ω resistor, V1 − V2 = 2 V.
import numpy as np
# Floating pair: 2 ohm between node 1 and node 2, no connection to ground.
# 1 A enters node 1 and 1 A leaves node 2, so net injected current is zero.
g = 1 / 2
G = np.array([[ g, -g],
[-g, g]])
i = np.array([1.0, -1.0])
v, *_ = np.linalg.lstsq(G, i, rcond=None)
print(v) # [ 1. -1.]
print(v[0] - v[1]) # 2.0lstsq returns the minimum-norm solution, the one with the smallest sum of squares. That is a choice made by the algorithm. It is not physics. Only the difference of 2 V is trustworthy. Report differences, or pick a reference explicitly.
Repairing a floating group
You have three standard repairs, and the right one depends on what you want to say about the circuit.
- Choose a reference node inside the group. Treat one node of the floating group as that group's own ground and solve for the rest. In the example, call node 2 the local reference. The single equation is 0.5 · V1 = 1, so V1 = 2 V relative to node 2. This is the cleanest choice when you only care about voltages within the group.
- Tie the group to ground. If the real circuit has a common-mode connection, such as a chassis or earth, add it to the model. A resistor of very large value stands in for an unknown leakage path.
- Add a tiny conductance to ground at every node. Circuit simulators do this and call it a gmin conductance, often around 1e-12 S. It makes G nonsingular. The cost is that floating voltages are pulled toward 0 V, and the result is only as meaningful as your choice of gmin. A value that is too small leaves G nearly singular. A value that is too large disturbs the real circuit.
Other causes of singular systems
A circuit whose nodes all reach ground can still produce a singular system once you add sources. The next table lists the common patterns.
| Pattern | What goes wrong | Typical fix |
|---|---|---|
| Two ideal voltage sources in parallel with different values | Contradiction: one node pair cannot have two voltages | Correct the model, or add the real source resistance |
| Two ideal voltage sources in parallel with equal values | Redundant equation, the system loses a rank | Remove one source |
| Loop made only of voltage sources | KVL around the loop must sum to zero, otherwise no solution | Remove one source or add series resistance |
| Node touching only current sources | The node's row of G is all zeros | Add the resistor that is really there, or merge sources in series |
| Two unequal current sources in series | KCL requires them to carry the same current | Correct the model |
| Resistor of 0 Ω | Conductance is infinite | Merge the two nodes into one |
| Dependent source that cancels a conductance | A pivot becomes exactly zero | Re-examine the controlling variable and the gain |
The last row is rare, but it shows that connectivity is necessary and not sufficient. If the graph check passes and the matrix is still singular, inspect the sources.
Near-singular matrices
A matrix can be nonsingular on paper and still cause trouble in floating-point arithmetic. The condition number measures how much errors in the data are amplified in the answer. You can read it with np.linalg.cond(G). When resistances span many decades, such as 1 Ω next to 1 GΩ, G contains entries of 1 S next to entries of 1e-9 S, and the condition number is large. A condition number near 1e16 means that double precision has no reliable digits left.
Two habits help:
- Check
np.linalg.cond(G)before trusting a result. A value above roughly 1e10 deserves suspicion. - Verify the answer with a KCL residual, as in the earlier checking chapter. Compute
G @ v - iand confirm it is tiny relative to the largest term. A small residual with a huge condition number still means the voltages may be inaccurate, because the residual does not reveal error along nearly null directions.
A nearly floating group, such as one tied to ground through 1 TΩ, is the usual source of a large condition number. Treat it as a floating group in practice.
Exercise
A circuit has nodes 1, 2, 3, and ground. Resistors: 1 to 0 of 5 Ω, 2 to 3 of 10 Ω. A 3 A source pushes current into node 2 and takes it from node 1. Is G singular, and is there a solution?
Show answer
G is singular, because nodes 2 and 3 form a floating group that never reaches ground. The net current into that group is +3 A, since the source injects 3 A into node 2 and nothing returns from the group. KCL cannot hold, so there is no solution. The model is inconsistent. A return path from node 3 to the rest of the circuit is missing.
Exercise
Nodes 1 and 2 are joined by a 4 Ω resistor and nothing else. A 2 A source pushes current into node 1 and takes it from node 2. Find V1 − V2, and say which parts of the answer are determined.
Show answer
The branch current is 2 A, so V1 − V2 = 2 × 4 = 8 V. The difference is determined. The absolute values of V1 and V2 are not, because the pair floats and G = [[0.25, -0.25], [-0.25, 0.25]] has rank 1. Choosing node 2 as a local reference gives V2 = 0 V and V1 = 8 V.