Sub topic 8
Matrix form: the resistance matrix and source vector
This chapter assumes you can write a KVL equation for a single mesh and handle mesh-current signs. It also assumes you can solve two or three linear equations by hand. Here we stop writing equations one at a time. We read the whole system off the circuit as one matrix equation, R i = v.
Why a matrix form
Mesh equations are linear in the mesh currents. For a circuit with n meshes you get n equations in n unknowns. Writing them as a matrix equation has three practical benefits:
- You can fill in the matrix by inspection, mesh by mesh, without writing KVL out in full.
- Standard solvers work on it directly, so a circuit with ten meshes is no harder to code than one with two.
- The matrix has structure you can check. Errors show up as broken patterns, not as unexplained wrong answers.
The matrix equation has this form:
R · i = vHere R is the resistance matrix (n × n), i is the vector of mesh currents, and v is the source vector. All values are in ohms, amperes, and volts.
Where the entries come from
Start from the KVL equation for mesh k, with every mesh current taken clockwise. The sum of resistor voltage drops equals the sum of source voltage rises:
(sum of all resistances in mesh k) · i_k − Σ (R_shared · i_neighbour) = sum of source rises in mesh kThe first term collects the drop across every resistor in mesh k caused by its own current. A resistor shared with a neighbouring mesh also carries the neighbour's current. With both currents clockwise, they flow in opposite directions through the shared resistor, so the neighbour's contribution enters with a minus sign. This gives the rules below.
| Entry | Rule |
|---|---|
| R[k][k] (diagonal) | Sum of all resistances around mesh k, including shared ones. Always positive. |
| R[j][k] (off-diagonal, j ≠ k) | Minus the total resistance shared by meshes j and k. Zero if they share none. |
| v[k] | Sum of source voltage rises in mesh k, in the direction of i_k. |
For v[k], a source whose "+" terminal is reached first when travelling clockwise is a rise and counts positive. A source that opposes the clockwise direction counts negative.
Worked example: two meshes
Mesh 1 contains a 10 V source, R1 = 2 Ω, and the shared resistor R2 = 4 Ω. Mesh 2 contains R2, R3 = 6 Ω, and a 5 V source that opposes the clockwise current.
Apply the rules:
- R[1][1] = 2 + 4 = 6 Ω
- R[2][2] = 4 + 6 = 10 Ω
- R[1][2] = R[2][1] = −4 Ω
- v[1] = +10 V
- v[2] = −5 V
[ 6 -4 ] [ i1 ] [ 10 ]
[ -4 10 ] [ i2 ] = [ -5 ]The code below builds this system, solves it, and checks the answer.
import numpy as np
# Two-mesh circuit, both mesh currents clockwise.
# Mesh 1: V1 = 10 V, R1 = 2 ohm, shared R2 = 4 ohm
# Mesh 2: shared R2 = 4 ohm, R3 = 6 ohm, V2 = 5 V opposing clockwise current
R1, R2, R3 = 2.0, 4.0, 6.0
V1, V2 = 10.0, 5.0
# Resistance matrix: diagonal = sum of resistances in the mesh,
# off-diagonal = minus the resistance shared by the two meshes.
R = np.array([[R1 + R2, -R2],
[-R2, R2 + R3]])
# Source vector: sum of voltage rises in the direction of each mesh current.
v = np.array([V1, -V2])
i1, i2 = np.linalg.solve(R, v)
print(f"i1 = {i1:.4f} A, i2 = {i2:.4f} A")
print(f"Current through R2 (mesh 1 direction) = {i1 - i2:.4f} A")
# Power balance: power absorbed by resistors equals power delivered by sources.
p_resistors = R1 * i1**2 + R2 * (i1 - i2)**2 + R3 * i2**2
p_sources = V1 * i1 - V2 * i2
print(f"P_resistors = {p_resistors:.4f} W, P_sources = {p_sources:.4f} W")The solution is i1 ≈ 1.818 A and i2 ≈ 0.227 A. The current in the shared resistor R2 is i1 − i2 ≈ 1.591 A in the mesh 1 direction. Both power totals come to about 17.05 W. Matching totals are the first sign that the matrix was built correctly.
Structure you can use as a check
A circuit made only of resistors and independent voltage sources, with all mesh currents in the same direction, gives a matrix with these properties:
- Symmetry. R[j][k] = R[k][j], because both entries are minus the same shared resistance. In NumPy,
np.allclose(R, R.T)tests it. - Positive diagonal. Each diagonal entry is a sum of positive resistances.
- Diagonal dominance. Each diagonal entry is at least the sum of the absolute values of the other entries in its row. The reason is that every shared resistor appears in the diagonal entry of both meshes it borders, and in the off-diagonal entries too. A mesh that touches a shared resistor can only have an equal or larger diagonal entry than the sum of its off-diagonal magnitudes.
- Invertibility. These properties together make R non-singular whenever every mesh has some resistance that is not shared with a neighbour. That is true for ordinary resistive circuits. A singular matrix usually means a wrong entry or a current-source problem, which a later chapter handles with supermeshes.
Three meshes in a row
The same rules scale directly. Take three meshes side by side. Mesh 1 has 1 Ω of its own resistance, and it shares 2 Ω with mesh 2. Mesh 2 has 3 Ω of its own and shares 4 Ω with mesh 3. Mesh 3 has 5 Ω of its own. Mesh 1 has a 12 V source aiding its clockwise current. Mesh 3 has a 6 V source opposing its clockwise current. Mesh 2 has no source. Meshes 1 and 3 share no resistor.
The entries are:
- R[1][1] = 1 + 2 = 3, R[2][2] = 2 + 3 + 4 = 9, R[3][3] = 4 + 5 = 9
- R[1][2] = R[2][1] = −2, R[2][3] = R[3][2] = −4, R[1][3] = R[3][1] = 0
- v = [12, 0, −6]
The code below builds R from a list of own resistances and a dictionary of shared resistances. The loop touches each shared resistor once and updates all four affected entries, so it works for any number of meshes.
import numpy as np
# Own (unshared) resistance of each mesh, in ohms.
own = [1.0, 3.0, 5.0]
# Shared resistance between pairs of meshes (0-based indices), in ohms.
shared = {(0, 1): 2.0, (1, 2): 4.0}
# Source rises along each clockwise mesh current, in volts.
v = np.array([12.0, 0.0, -6.0])
n = len(own)
R = np.zeros((n, n))
for k, r in enumerate(own):
R[k, k] += r
for (a, b), r in shared.items():
R[a, a] += r
R[b, b] += r
R[a, b] -= r
R[b, a] -= r
print(R)
print("symmetric:", np.allclose(R, R.T))
i = np.linalg.solve(R, v)
print("mesh currents (A):", np.round(i, 4))
# Total power absorbed by resistors is i^T R i; power from sources is i . v.
print(f"absorbed = {i @ R @ i:.4f} W, delivered = {i @ v:.4f} W")The mesh currents are about i1 = 4.604 A, i2 = 0.906 A, and i3 = −0.264 A. The negative sign on i3 means the actual current in mesh 3 flows counterclockwise. That is not an error. It is the correct result for a 6 V source that opposes the clockwise direction and is stronger than the push from mesh 2. Absorbed and delivered power both come to about 56.83 W.
The quadratic form i·R·i gives the total power absorbed by the resistors. This works because each shared resistor's power splits into the two diagonal terms and the two off-diagonal cross terms of (ij − ik)² times R.
Common errors
- Wrong sign on a source. Decide the sign of v[k] by travelling clockwise through the source. If you enter its "−" terminal first, it is a rise, so it counts positive. If you enter "+" first, it is a drop, so it counts negative.
- Leaving a shared resistor out of a diagonal. Both meshes that border a resistor include it in their diagonal entry. Forgetting it in one of them breaks the symmetry.
- Mixing mesh directions. One counterclockwise mesh flips the signs of its off-diagonal entries. If you do this on purpose, also flip those signs in the rule above.
- Putting the sum of neighbours' shared resistors into one cell. If two meshes share two separate resistors, add them: R[j][k] is minus the total shared between them.
- Reading i1 − i2 backwards. The shared-branch current is the difference of the two mesh currents. Take it in the direction of the mesh you name, and the other mesh's current subtracts.
Exercise
Mesh 1 has a 20 V source aiding its clockwise current, 3 Ω of its own resistance, and shares a 5 Ω resistor with mesh 2. Mesh 2 has 7 Ω of its own resistance and an 8 V source aiding its clockwise current. Write R and v, then solve for the mesh currents.
Show answer
R[1][1] = 3 + 5 = 8, R[2][2] = 5 + 7 = 12, R[1][2] = R[2][1] = −5, so R = [[8, −5], [−5, 12]] and v = [20, 8]. The determinant is 8·12 − 25 = 71. By Cramer's rule, i1 = (20·12 + 5·8)/71 = 280/71 ≈ 3.944 A and i2 = (8·8 + 5·20)/71 = 164/71 ≈ 2.310 A. Check the first equation: 8(3.944) − 5(2.310) ≈ 20.
Exercise
A student builds R for a circuit with only resistors and independent voltage sources, all mesh currents clockwise, and gets R = [[6, −4], [−3, 10]]. What does the matrix tell you, and what should you check?
Show answer
R is not symmetric (−4 versus −3). In a circuit with only resistors and independent sources and a common current direction, the two off-diagonal entries must both equal minus the shared resistance. So one entry is wrong, or one mesh was given a different direction. Recheck the shared resistance between the two meshes and the direction of both mesh currents.