Sub topic 9
Solving for the mesh currents
This chapter takes the matrix equation R i = v from the previous chapter and solves it. It covers three solution methods, how to check the result, and what a failed solve tells you. It assumes you can write the resistance matrix and source vector, and that you know how to solve two or three linear equations by hand.
What you are solving
For a planar circuit with N meshes and no current sources on shared branches, the mesh equations form an N-by-N linear system:
Ris the resistance matrix. Diagonal entryR[k][k]is the total resistance around mesh k. Off-diagonal entryR[j][k]is minus the resistance shared by meshes j and k.iis the vector of unknown mesh currents.vis the source vector, the sum of voltage rises in the direction of each mesh current.
Take the two-mesh circuit used throughout this tutorial: V1 = 10 V, R1 = 2 Ω, shared R2 = 4 Ω, R3 = 6 Ω, and V2 = 5 V opposing the clockwise current in mesh 2. The system is:
[ 6 -4 ] [i1] [ 10 ]
[-4 10 ] [i2] = [ -5 ]Method 1: Substitution and elimination
Elimination works for two or three meshes. Solve one equation for one unknown, substitute into the others, and repeat.
The first equation is 6·i1 − 4·i2 = 10, so i1 = (10 + 4·i2) / 6. Substitute into the second equation:
−4·(10 + 4·i2)/6 + 10·i2 = −5
Multiply through by 6 to clear the fraction:
−40 − 16·i2 + 60·i2 = −30
44·i2 = 10, so i2 = 10/44 ≈ 0.2273 A. Then i1 = (10 + 4·0.2273)/6 ≈ 1.8182 A.
Elimination is error-prone beyond three meshes, so use it for small hand problems and for checking.
Method 2: Cramer's rule
Cramer's rule gives each unknown as a ratio of determinants. Let Δ be the determinant of R. Let Δk be the determinant of R with column k replaced by v.
For the example:
- Δ = 6·10 − (−4)(−4) = 60 − 16 = 44
- Δ1 = 10·10 − (−4)(−5) = 100 − 20 = 80
- Δ2 = 6·(−5) − 10·(−4) = −30 + 40 = 10
So i1 = 80/44 = 20/11 A and i2 = 10/44 = 5/22 A. This agrees with elimination.
Cramer's rule is convenient for 2×2 systems and for finding one current without solving for the rest. Its cost grows quickly with size. A 3×3 system needs four 3×3 determinants, and a 6×6 system is impractical by hand.
The same calculation in Python, using exact fractions so you can compare with the hand result:
from fractions import Fraction as F
# Resistance matrix and source vector for the two-mesh example
a, b = F(6), F(-4)
c, d = F(-4), F(10)
v1, v2 = F(10), F(-5)
det = a * d - b * c
i1 = (v1 * d - b * v2) / det
i2 = (a * v2 - v1 * c) / det
print(f"det = {det}")
print(f"i1 = {i1} A = {float(i1):.4f} A")
print(f"i2 = {i2} A = {float(i2):.4f} A")
print(f"Current in shared R2 = {i1 - i2} A = {float(i1 - i2):.4f} A")Method 3: Gaussian elimination with a library
For three or more meshes, use a numerical solver. It performs Gaussian elimination with row pivoting, which is elimination done systematically.
import numpy as np
# Three meshes in a ladder, all currents clockwise.
# Mesh 1: R = 2 ohm own, 1 ohm shared with mesh 2, source 12 V
# Mesh 2: R = 3 ohm own, 1 ohm shared with mesh 1, 2 ohm shared with mesh 3
# Mesh 3: R = 4 ohm own, 2 ohm shared with mesh 2, source 6 V opposing
R = np.array([[ 3.0, -1.0, 0.0],
[-1.0, 6.0, -2.0],
[ 0.0, -2.0, 6.0]])
v = np.array([12.0, 0.0, -6.0])
i = np.linalg.solve(R, v)
print("mesh currents (A):", np.round(i, 4))
# Checks
print("symmetric:", np.allclose(R, R.T))
print("residual norm:", np.linalg.norm(R @ i - v))
print("determinant:", round(np.linalg.det(R), 3))The solution is i1 ≈ 4.1333 A, i2 = 0.4 A, i3 ≈ −0.8667 A. The residual R @ i - v should be near zero, on the order of 1e-15. Use np.linalg.solve rather than np.linalg.inv(R) @ v. Solving directly is faster and loses less precision than forming the inverse.
Reading the signs of the answer
A negative mesh current is a valid answer. It means the current flows opposite to the direction you assumed. In the three-mesh example, i3 ≈ −0.8667 A means mesh 3 current actually circulates counterclockwise. Do not rerun the problem with a different direction. Keep the sign and carry it into the branch-current calculations in the next chapter.
Properties that help you catch mistakes
For a circuit with only resistors and independent voltage sources, and a consistent mesh direction (all clockwise or all counterclockwise), the resistance matrix has these properties:
- Symmetric.
R[j][k] = R[k][j], because the shared resistance is the same from either mesh's view. - Positive diagonal. Each diagonal entry is the total resistance of a mesh.
- Non-positive off-diagonals. Shared resistances enter with a minus sign when currents circulate the same way through the shared branch. Meshes sharing a branch in opposite directions (mixed clockwise and counterclockwise choices) give positive entries, which is a common source of confusion.
- Diagonal dominance. Each diagonal entry is at least the sum of the magnitudes of the off-diagonal entries in its row, since a mesh's total resistance includes every resistor it shares.
If your matrix is not symmetric, you have probably made an arithmetic or sign error. The exception is a circuit with dependent sources, where asymmetry is expected.
Failure modes
Singular matrix
If the solver raises LinAlgError: Singular matrix, the determinant is zero and the system has no unique solution. In a circuit this usually means:
- Two meshes were defined that are not independent, for example an outer loop counted in addition to the inner meshes.
- A mesh was left with no resistance, such as a loop containing only ideal voltage sources.
- A current source on a shared branch was written as a resistance. Use the supermesh technique, covered in a later chapter.
Loss of precision
Real resistive networks are well conditioned, so this is rarely a problem for hand-sized circuits. It can occur when resistances differ by many orders of magnitude, such as a 1 mΩ sense resistor beside a 10 MΩ bias resistor. Compare the residual to the size of v, not to zero in absolute terms.
Rounding too early
When solving by hand, keep fractions or at least five significant digits until the end. Rounding i2 to 0.23 A before computing i1 − i2 shifts the shared-branch current visibly.
A quick substitution check
Before moving on, put the answers back in every mesh equation. For the two-mesh example:
- Mesh 1: 6·(1.8182) − 4·(0.2273) = 10.909 − 0.909 = 10.000 ✓
- Mesh 2: −4·(1.8182) + 10·(0.2273) = −7.273 + 2.273 = −5.000 ✓
This uses the same equations you solved, so it catches arithmetic slips but not errors in setting up the equations. The power-balance check, covered later, tests the setup itself.
Summary of choices
| Situation | Suggested method |
|---|---|
| 2 meshes, exact answer wanted | Cramer's rule or elimination with fractions |
| 3 meshes, hand calculation | Elimination, checked by substitution |
| One mesh current needed out of several | Cramer's rule for that unknown |
| 4 or more meshes | Numerical solver such as `np.linalg.solve` |
| Many source values on the same circuit | Factor `R` once, reuse for each `v` |
Exercise
A two-mesh circuit gives the system 5·i1 − 2·i2 = 9 and −2·i1 + 7·i2 = −4. Use Cramer's rule to find i1 and i2.
Show answer
Δ = 5·7 − (−2)(−2) = 35 − 4 = 31. Δ1 = 9·7 − (−2)(−4) = 63 − 8 = 55. Δ2 = 5·(−4) − 9·(−2) = −20 + 18 = −2. So i1 = 55/31 ≈ 1.774 A and i2 = −2/31 ≈ −0.0645 A. The negative i2 means mesh 2 current flows opposite to its assumed direction.
Exercise
In the three-mesh example, R = [[3, -1, 0], [-1, 6, -2], [0, -2, 6]] and v = [12, 0, -6]. The solver returns i = [4.1333, 0.4, -0.8667]. Show by substitution that the second mesh equation holds.
Show answer
Row 2 gives −1·(4.1333) + 6·(0.4) − 2·(−0.8667) = −4.1333 + 2.4 + 1.7333 = 0.0000, which equals v2 = 0. The equation holds.
Exercise
Your solver reports a singular matrix for a circuit you believe has three meshes. Name two likely causes.
Show answer
Likely causes include counting a mesh that is not independent (for example an outer loop in addition to the inner meshes), a loop with no resistance (only ideal voltage sources), or a current source in a shared branch that was treated as a resistance instead of being handled with a supermesh.