{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Example Problem: Using Kirchhoff's Laws\n",
"\n",
"Find the current in all branches of the circuit shown.\n",
"\n",
"![](http://madisoncollegephysics.net/comp/images/kirchoff-example.png)\n",
"\n",
"### Solution:\n",
"\n",
"First find the equations to solve. \n",
"[See work here](http://madisoncollegephysics.net/comp/images/kirchoff-handwritten-work.jpg).\n",
"\n",
"These equations, when organized, are\n",
"\n",
"$0 + 0+I_2+I_3+ 0-I_5=0$ \n",
"$I_0-I_1-I_2+ 0+ 0+ 0=0$ \n",
"$ 0+I_1+ 0-I_3-I_4+ 0=0$ \n",
"$ 0+ 0-R_2I_2+ 0+ 0-R_5I_5 = -\\mathcal{E}$ \n",
"$0+ 0+ 0-R_3I_3+R_4I_4 -R_5I_5=0$ \n",
"$0+R_1I_1-R_2I_2 +R_3I_3+0+0=0$ \n",
"\n",
"where \n",
"$\\mathcal{E} = 24$ V, \n",
"$R_1=150$ \u03a9,\n",
"$R_2= 50$ \u03a9,\n",
"$R_3=100$ \u03a9,\n",
"$R_4=300$ \u03a9, and\n",
"$R_5=250$ \u03a9.\n",
"\n",
"Using the techniques of linear algebra, this is written as a matrix of\n",
"coefficients times a column vector of variables, equal to a column vector of\n",
"constants on the right-hand side.\n",
"\n",
"$$\\begin{bmatrix}\n",
" 0 & 0 & 1 & 1 & 0 & -1 \\\\\n",
" 1 & -1 & -1 & 0 & 0 & 0 \\\\\n",
" 0 & 1 & 0 & -1 & -1 & 0 \\\\\n",
" 0 & 0 & -R_2 & 0 & 0 & -R_5 \\\\\n",
" 0 & 0 & 0 & -R_3 & R_4 & -R_5 \\\\\n",
" 0 & R_1 & -R_2 & R_3 & 0 & 0 \\\\\n",
"\\end{bmatrix}\n",
"\\begin{bmatrix} \n",
" I_0 \\\\ I_1 \\\\ I_2 \\\\ I_3 \\\\ I_4 \\\\ I_5 \\\\\n",
"\\end{bmatrix} = \n",
"\\begin{bmatrix} \n",
" 0 \\\\ 0 \\\\ 0 \\\\ -\\mathcal{E} \\\\ 0 \\\\ 0 \\\\\n",
"\\end{bmatrix} $$\n",
"\n",
"The code below solves the simultaneous linear equations.\n"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"from numpy import *\n",
"R1,R2,R3,R4,R5 = 150,50,100,300,250\n",
"E = 24 # V"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# I0 I1 I2 I3 I4 I5\n",
"CC = array((\n",
" ( 0, 0, 1, 1, 0, -1, ),\n",
" ( 1, -1, -1, 0, 0, 0, ),\n",
" ( 0, 1, 0, -1, -1, 0, ),\n",
" ( 0, 0, -R2, 0, 0, -R5, ),\n",
" ( 0, 0, 0, -R3, R4, -R5, ),\n",
" ( 0, R1, -R2, R3, 0, 0, ),\n",
" ))\n",
"SS = array(( 0, 0, 0, -E, 0, 0,))\n",
"solution = linalg.solve(CC,SS)\n",
"print('currents are:')\n",
"for i in range(6): \n",
" print(f'\tI{i:d} = {solution[i]*1e3:3.0f} mA')"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Note the negative value for $I_3$ means this current actually flows toward the\n",
"left (opposite the direction defined in the\n",
"in the \n",
"[figure](http://madisoncollegephysics.net/comp/images/kirchoff-handwritten-work.jpg)).\n",
"\n",
"## Going further\n",
"\n",
"If you have $N$ unknowns, you need $N$ equations to solve for them.\n",
"But those $N$ equations must be *linearly independent*. \n",
"For example, you cannot obtain a new equation by deriving it from \n",
"a combination of the other equations.\n",
"\n",
"As an example, the four current-sum equations shown in the\n",
"[solution](http://madisoncollegephysics.net/comp/images/kirchoff-handwritten-work.jpg)\n",
"are *not* all independent. If you add together the first three\n",
"equations, you get the fourth. This means that if you used the fourth\n",
"current equation in place of, say, the last voltage equation, you\n",
"would have a problem. This is done below.\n"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"CC = array((\n",
" ( 0, 0, 1, 1, 0, -1, ),\n",
" ( 1, -1, -1, 0, 0, 0, ),\n",
" ( 0, 1, 0, -1, -1, 0, ),\n",
" ( 0, 0, -R2, 0, 0, -R5, ),\n",
" ( 0, 0, 0, -R3, R4, -R5, ),\n",
" ( 1, 0, 0, 0, -1, -1, ),\n",
" ))\n",
"SS = array(( 0, 0, 0, -E, 0, 0,))\n",
"try:\n",
" solution = linalg.solve(CC,SS)\n",
"except Exception as e:\n",
" print(f\"Error: {e}\")"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"If you run this code you get an error that says `Singular matrix`,\n",
"which means the rows are not independent.\n",
"\n",
"To check if your equations are independent, you can calculate the \n",
"*determinant* of the coefficient matrix. \n",
"If this determinant is zero, your equations are *not* independent. \n",
"\n",
"To do this in python:\n"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"print( linalg.det(CC) )"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Since this is zero, we cannot solve this system of equations. \n",
"For the original set of equations, the determinant is nonzero:\n"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"CC = array((\n",
" ( 0, 0, 1, 1, 0, -1, ),\n",
" ( 1, -1, -1, 0, 0, 0, ),\n",
" ( 0, 1, 0, -1, -1, 0, ),\n",
" ( 0, 0, -R2, 0, 0, -R5, ),\n",
" ( 0, 0, 0, -R3, R4, -R5, ),\n",
" ( 0, R1, -R2, R3, 0, 0, ),\n",
" ))\n",
"print( linalg.det(CC) )"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"That is definitely *not* zero, \n",
"so there is no problem finding the solution in this case.\n"
],
"metadata": {}
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}