# Example Problem: Using Kirchhoff's Laws¶

Find the current in all branches of the circuit shown.

### Solution:¶

First find the equations to solve. See work here.

These equations, when organized, are

$0 + 0+I_2+I_3+ 0-I_5=0$

$I_0-I_1-I_2+ 0+ 0+ 0=0$

$ 0+I_1+ 0-I_3-I_4+ 0=0$

$ 0+ 0-R_2I_2+ 0+ 0-R_5I_5 = -\mathcal{E}$

$0+ 0+ 0-R_3I_3+R_4I_4 -R_5I_5=0$

$0+R_1I_1-R_2I_2 +R_3I_3+0+0=0$

where $\mathcal{E} = 24$ V, $R_1=150$ Ω, $R_2= 50$ Ω, $R_3=100$ Ω, $R_4=300$ Ω, and $R_5=250$ Ω.

Using the techniques of linear algebra, this is written as a matrix of coefficients times a column vector of variables, equal to a column vector of constants on the right-hand side.

$$\begin{bmatrix} 0 & 0 & 1 & 1 & 0 & -1 \\ 1 & -1 & -1 & 0 & 0 & 0 \\ 0 & 1 & 0 & -1 & -1 & 0 \\ 0 & 0 & -R_2 & 0 & 0 & -R_5 \\ 0 & 0 & 0 & -R_3 & R_4 & -R_5 \\ 0 & R_1 & -R_2 & R_3 & 0 & 0 \\ \end{bmatrix} \begin{bmatrix} I_0 \\ I_1 \\ I_2 \\ I_3 \\ I_4 \\ I_5 \\ \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ 0 \\ -\mathcal{E} \\ 0 \\ 0 \\ \end{bmatrix} $$

The code below solves the simultaneous linear equations.

```
from numpy import *
R1,R2,R3,R4,R5 = 150,50,100,300,250
E = 24 # V
```

```
# I0 I1 I2 I3 I4 I5
CC = array((
( 0, 0, 1, 1, 0, -1, ),
( 1, -1, -1, 0, 0, 0, ),
( 0, 1, 0, -1, -1, 0, ),
( 0, 0, -R2, 0, 0, -R5, ),
( 0, 0, 0, -R3, R4, -R5, ),
( 0, R1, -R2, R3, 0, 0, ),
))
SS = array(( 0, 0, 0, -E, 0, 0,))
solution = linalg.solve(CC,SS)
print('currents are:')
for i in range(6):
print(f' I{i:d} = {solution[i]*1e3:3.0f} mA')
```

currents are: I0 = 136 mA I1 = 42 mA I2 = 94 mA I3 = -17 mA I4 = 59 mA I5 = 77 mA

Note the negative value for $I_3$ means this current actually flows toward the left (opposite the direction defined in the in the figure).

## Going further¶

If you have $N$ unknowns, you need $N$ equations to solve for them.
But those $N$ equations must be *linearly independent*.
For example, you cannot obtain a new equation by deriving it from
a combination of the other equations.

As an example, the four current-sum equations shown in the
solution
are *not* all independent. If you add together the first three
equations, you get the fourth. This means that if you used the fourth
current equation in place of, say, the last voltage equation, you
would have a problem. This is done below.

```
CC = array((
( 0, 0, 1, 1, 0, -1, ),
( 1, -1, -1, 0, 0, 0, ),
( 0, 1, 0, -1, -1, 0, ),
( 0, 0, -R2, 0, 0, -R5, ),
( 0, 0, 0, -R3, R4, -R5, ),
( 1, 0, 0, 0, -1, -1, ),
))
SS = array(( 0, 0, 0, -E, 0, 0,))
try:
solution = linalg.solve(CC,SS)
except Exception as e:
print(f"Error: {e}")
```

Error: Singular matrix

If you run this code you get an error that says `Singular matrix`

,
which means the rows are not independent.

To check if your equations are independent, you can calculate the
*determinant* of the coefficient matrix.
If this determinant is zero, your equations are *not* independent.

To do this in python:

```
print( linalg.det(CC) )
```

0.0

Since this is zero, we cannot solve this system of equations. For the original set of equations, the determinant is nonzero:

```
CC = array((
( 0, 0, 1, 1, 0, -1, ),
( 1, -1, -1, 0, 0, 0, ),
( 0, 1, 0, -1, -1, 0, ),
( 0, 0, -R2, 0, 0, -R5, ),
( 0, 0, 0, -R3, R4, -R5, ),
( 0, R1, -R2, R3, 0, 0, ),
))
print( linalg.det(CC) )
```

32625000.000000015

That is definitely *not* zero,
so there is no problem finding the solution in this case.