Assignment: From Two Loops to a Matrix

Python
Jupyter
Kirchhoff
matrices
assignment
Derive two loop equations, arrange them as a matrix, and solve the same system numerically and symbolically in Jupyter.

Learning goals

  • I can define the current directions in a two-loop circuit.
  • I can write one signed KVL equation for each loop.
  • I can rearrange both equations into the same variable order.
  • I can build \(A\mathbf{x}=\mathbf{b}\) and solve it with NumPy and SymPy.
  • I can verify the answer using residuals, substitution, and KCL.

Why this is useful

Two loops share one component, so the current through the shared component depends on both loop currents. A matrix does not replace the circuit reasoning. It organizes the equations after the arrows, signs, units, and KVL equations are correct.

This bridge has two parts. Question 4 is the fully worked teaching example. Question 5 is the independent transfer task: the helper questions reveal the process, but not the completed coefficients, matrix, or currents.

What you will make

Submit one Jupyter notebook showing this complete chain:

arrows two KVL equations standard form \(A\mathbf{x}=\mathbf{b}\) two solver methods verification

Before you start

  1. Complete Python and NumPy Foundations. You should already be able to run all cells, inspect an array, call a function, read basic documentation, and make an assertion pass.
  2. Download the printable two-loop-to-matrix guide.
  3. Download Notebook 3A: From Two Loops to a Matrix.
  4. Keep your returned Question 4 and Question 5 work available. Use the released Question 4 answer for comparison, but do not copy the released Question 5 answer into the scaffold.
  5. Choose a Jupyter route:
  6. Rename the notebook before working:
studentnumber-lastname-kirchhoff-jupyter.ipynb

Example: 123456789-Smith-kirchhoff-jupyter.ipynb. Keep the .ipynb ending.

NoteTextbook and lesson support

Part A: Worked Question 4

Follow the complete example once. Each line performs one algebra move, so you can reuse the method without reusing the numbers.

1. Use Kirchhoff’s Current Law to define the three currents

Complete Question 4 two-loop circuit. I1 points to the right through the left loop resistor, I2 points to the right through the right loop resistor, and I3 points downward through the shared centre resistor.
Figure 1. Read the current arrows before reading the equations. The top centre junction connects all three current paths. #

Kirchhoff’s Current Law at the junction

At the top centre junction:

\(I_1\)entersfrom the left
\(I_2\)leavesto the right
\(I_3\)leavesdownward

current entering = current leaving

\[ I_1=I_2+I_3 \]

\[ I_1-I_2-I_3=0 \]

\[ \boxed{I_3=I_1-I_2} \]

Use clockwise mesh currents \(I_1\) and \(I_2\). Define \(I_3\) downward through the shared resistor, as shown. Keep the arrows and the equation \(I_3=I_1-I_2\) visible beside the work.

2. Derive Equation 1 from Loop 1

Loop 1 isolated from the two-loop circuit. The 10 volt source is on the left, the 3 kiloohm resistor is on top, and the shared 2 kiloohm resistor is on the right. I3 points downward beside the shared resistor.
Figure 2. Isolate Loop 1 before writing its KVL equation. Only the three components crossed by this loop are shown. #

Turn the circuit into signed terms

\(V_{S1}\)source rise\(+10\)
\(R_1\)with \(I_1\)\(-3I_1\)
\(R_3\)with \(I_3\)\(-2I_3\)

\[ +10-3I_1-2I_3=0 \]

Use the printable guide and record each signed KVL term:

Element Signed term Reason
\(V_{S1}=10\,\mathrm{V}\) \(+10\) source rise
\(R_1=3.0\,\mathrm{k\Omega}\) \(-3I_1\) travel with \(I_1\)
\(R_3=2.0\,\mathrm{k\Omega}\) \(-2I_3\) travel with the defined \(I_3\)

Do not replace the resistor-voltage symbols with numbers in one jump. Show the complete chain:

\[ +10-V_{R1}-V_{R3}=0, \]

\[ V_{R1}=I_1R_1=3I_1,\qquad V_{R3}=I_3R_3=2I_3, \]

\[ +10-3I_1-2I_3=0, \]

\[ +10-3I_1-2(I_1-I_2)=0, \]

\[ +10-3I_1-2I_1+2I_2=0, \]

\[ +10-5I_1+2I_2=0, \]

\[ -5I_1+2I_2=-10, \]

\[ \boxed{5I_1-2I_2=10}. \]

The second-last line subtracts \(10\) from both sides. The final line multiplies the complete equation by \(-1\) so the leading coefficient is positive.

3. Derive Equation 2 from Loop 2

Loop 2 isolated from the two-loop circuit. The shared 2 kiloohm resistor is on the left, the 5 kiloohm resistor is on top, and the 6 volt source is on the right. I3 points downward beside the shared resistor.
Figure 3. Isolate Loop 2. This loop crosses the shared resistor in the direction opposite the defined \(I_3\) arrow. #

The shared-resistor sign changes

\(V_{S2}\)source rise\(+6\)
\(R_2\)with \(I_2\)\(-5I_2\)
\(R_3\)against \(I_3\)\(+2I_3\)

\[ +6-5I_2+2I_3=0 \]

Loop 2 travels through the shared resistor against the defined direction of \(I_3\). Its resistor term is therefore positive.

Element Signed term Reason
\(V_{S2}=6.0\,\mathrm{V}\) \(+6\) source rise
\(R_2=5.0\,\mathrm{k\Omega}\) \(-5I_2\) travel with \(I_2\)
\(R_3=2.0\,\mathrm{k\Omega}\) \(+2I_3\) travel against the defined \(I_3\)

Again, begin with resistor-voltage symbols and show one algebra move per line:

\[ +6-V_{R2}+V_{R3}=0, \]

\[ V_{R2}=I_2R_2=5I_2,\qquad V_{R3}=I_3R_3=2I_3, \]

\[ +6-5I_2+2I_3=0, \]

\[ +6-5I_2+2(I_1-I_2)=0, \]

\[ +6-5I_2+2I_1-2I_2=0, \]

\[ +6+2I_1-5I_2-2I_2=0, \]

\[ +6+2I_1-7I_2=0, \]

\[ 2I_1-7I_2=-6, \]

\[ \boxed{-2I_1+7I_2=6}. \]

Multiplying the complete equation by \(-1\) does not change its solution.

4. Build the matrix

State the variable order before entering any coefficients:

\[ [I_1,I_2]. \]

The two standard-form Kirchhoff equations are read left to right into a coefficient table. The coefficient rows form matrix A, the current symbols form vector x, and the voltage values form vector b. Expanding the matrix product recreates the original two equations.
Figure 4. Each circuit equation becomes one matrix row. The variable order \([I_1,I_2]\) fixes the column order. #

Read each standard-form equation from left to right:

Equation coefficient of \(I_1\) coefficient of \(I_2\) right side
\(5I_1-2I_2=10\) \(5\) \(-2\) \(10\)
\(-2I_1+7I_2=6\) \(-2\) \(7\) \(6\)

Therefore,

\[ \underbrace{\begin{bmatrix}5&-2\\-2&7\end{bmatrix}}_{A\,[\mathrm{k\Omega}]} \underbrace{\begin{bmatrix}I_1\\I_2\end{bmatrix}}_{\mathbf{x}\,[\mathrm{mA}]} = \underbrace{\begin{bmatrix}10\\6\end{bmatrix}}_{\mathbf{b}\,[\mathrm{V}]}. \]

The matrix multiplication is only a compact way to write the same two row equations:

\[ \begin{bmatrix} 5I_1+(-2)I_2\\ (-2)I_1+7I_2 \end{bmatrix} = \begin{bmatrix}10\\6\end{bmatrix}. \]

Read the top row as Equation 1 and the bottom row as Equation 2. If either read-back equation differs from the boxed KVL equation, fix the matrix before calling a solver.

Use the row-reading cells in Notebook 3A to turn both rows back into equations. Do not continue until they match the two boxed equations above.

5. Solve with numerical linear algebra

Run the NumPy cells using:

A = np.array([
    [ 5.0, -2.0],  # Equation 1 coefficients
    [-2.0,  7.0],  # Equation 2 coefficients
])
b = np.array([10.0, 6.0])
currents_mA = np.linalg.solve(A, b)
residual_V = A @ currents_mA - b

Print \(I_1\), \(I_2\), and the two residuals with units. Keep the residual assertion in the code.

6. Solve symbolically

Run the SymPy cells. Keep the exact fractional results visible before converting them to decimals.

I1, I2 = sp.symbols("I1 I2")
equations = [
    sp.Eq(5*I1 - 2*I2, 10),
    sp.Eq(-2*I1 + 7*I2, 6),
]
exact_solution = sp.solve(
    equations,
    (I1, I2),
    dict=True,
)[0]

Calculate \(I_3=I_1-I_2\) symbolically and numerically.

7. Compare and verify

Complete the notebook comparison table:

Quantity Original work Released answer NumPy SymPy exact Agreement and explanation
\(I_1\)
\(I_2\)
\(I_3\)

Then verify:

  • Equation 1 by substitution;
  • Equation 2 by substitution;
  • \(I_3=I_1-I_2\);
  • the NumPy residual vector.

Part B: Build Question 5 without being given the matrix

Use your original Question 5 circuit and work through the prompts below. The symbols \(a_{11}\), \(a_{12}\), \(a_{21}\), \(a_{22}\), \(b_1\), and \(b_2\) are placeholders. Replace them with coefficients obtained from your own algebra.

WarningDo not start with the released answer

The purpose is to translate a circuit into a system. Complete the two standard-form equations, coefficient table, matrix, and row read-back before comparing currents with the released answer.

B1. Define the currents before doing algebra

Answer these questions in a Markdown cell:

  1. Which direction did you assign to \(I_1\)?
  2. Which direction did you assign to \(I_2\)?
  3. Which direction did you assign to the shared-branch current \(I_3\)?
  4. Is your shared-current relationship \(I_3=I_1-I_2\) or \(I_3=I_2-I_1\)? Why does it match your arrow?

B2. Rearrange both loop equations one move at a time

For each loop, show all seven lines below. Do not combine multiple algebra moves on one line.

  1. Write signed KVL using resistor-voltage symbols such as \(V_{R1}\).
  2. Replace every resistor voltage with \(IR\).
  3. Substitute your expression for \(I_3\).
  4. Distribute the resistance coefficient through the bracket.
  5. Reorder the terms into the variable order \([I_1,I_2]\).
  6. Combine like terms.
  7. Move the constant to the right side. If you multiply by \(-1\), multiply every term.

Your final equations must have this form:

\[ \boxed{a_{11}I_1+a_{12}I_2=b_1} \]

\[ \boxed{a_{21}I_1+a_{22}I_2=b_2}. \]

B3. Extract coefficients instead of guessing them

Copy this table into the notebook and replace every blank:

Question 5 equation coefficient of \(I_1\) coefficient of \(I_2\) right side
Equation 1
Equation 2

Now construct the matrix using the same variable order in both rows:

\[ \underbrace{\begin{bmatrix}a_{11}&a_{12}\\a_{21}&a_{22}\end{bmatrix}}_{A} \underbrace{\begin{bmatrix}I_1\\I_2\end{bmatrix}}_{\mathbf{x}} = \underbrace{\begin{bmatrix}b_1\\b_2\end{bmatrix}}_{\mathbf{b}}. \]

Replace the six placeholders with your Question 5 numbers. Then expand the top matrix row back into Equation 1 and the bottom row back into Equation 2. The read-back equations must match your boxed equations exactly.

B4. Use Jupyter as a check, not as the source of the equations

Enter your coefficients only after completing B1-B3. Use the Question 5 scaffold in Notebook 3A to:

  • print both rows beside the equations they represent;
  • solve \(A\mathbf{x}=\mathbf{b}\) numerically;
  • calculate the shared current using your chosen current definition;
  • calculate \(A\mathbf{x}-\mathbf{b}\);
  • compare with the released answer only after the residual and row read-back checks pass.

Progressive hints

The mesh currents oppose each other in the shared resistor. Start with \(I_3=I_1-I_2\) and substitute that expression into both KVL equations.

Search numpy.linalg.solve A shape or SymPy solve simultaneous equations. Use the official NumPy solve documentation and SymPy solver documentation.

With variable order \([I_1,I_2]\), use Row 1 \([5,-2]\), Row 2 \([-2,7]\), and right side \([10,6]\). Read both rows back as equations before solving.

The first boxed equation supplies \([a_{11},a_{12}]\) and \(b_1\). The second supplies \([a_{21},a_{22}]\) and \(b_2\). Keep the order \([I_1,I_2]\) in both rows. This tells you where coefficients go without revealing their values.

Common issues

The shared resistor has the wrong sign

Loop 1 travels with the defined \(I_3\), so its term is \(-2I_3\). Loop 2 travels against \(I_3\), so its term is \(+2I_3\).

The matrix solves the wrong equations

Check the variable order and read each row back as an equation. A solver cannot recognize that a coefficient was copied into the wrong column.

SymPy is not installed

If you see ModuleNotFoundError: No module named 'sympy', use the current course JupyterLite environment or install the provided requirements in the local virtual environment:

python -m pip install -r requirements-jupyter.txt

A negative current appears

A negative current means the real current is opposite the arrow selected before solving. It is not automatically an error.

Check your work

Save and submit

  1. Restart the kernel.
  2. Run all required cells from top to bottom.
  3. Confirm that no required cell ends with an unexpected error.
  4. Save and download studentnumber-lastname-kirchhoff-jupyter.ipynb.
  5. Reopen the downloaded notebook and confirm that the Markdown, code, and output are present.
  6. Submit the .ipynb file through the assigned classroom system.

Later hands-on verification

ImportantBuild later - not required for this notebook submission

Students can eventually test the model with a low-voltage circuit board. Start with a one-battery, three-resistor network to verify KCL and KVL. To reproduce a two-source problem like Question 4, use two isolated battery holders or a current-limited dual bench supply.

A practical classroom system should include:

  • switched AA battery holders or a current-limited low-voltage supply - never wall voltage;
  • \(1\,\mathrm{k\Omega}\) to \(4.7\,\mathrm{k\Omega}\) resistors so currents remain in the milliampere range;
  • a breadboard or labelled terminal board with colour-coded current paths;
  • power-off wiring checks before connecting either source;
  • voltage measurements across each resistor, followed by \(I=V/R\) to infer current before students attempt direct current measurement;
  • a comparison table for predicted, simulated, and measured values.

Use the measured battery voltages in the equations because real batteries will not equal their printed values exactly. The physical goal is agreement within component and measurement tolerance, not perfect decimal matching.

Next lesson

The intentional keyword error, array-shape error, silent unit bug, parameter sweep, capacitors, and RC response are later checkpoints. They are not required tomorrow.

Help improve this lesson (optional)

If something got in the way, tell the teacher which step, sign, diagram, or code cell was unclear, what you still do not know how to do, and one change that would help the next student. If you can fix the wording, schematic, example, or starter code, show the teacher. A useful fix may be added to the course website and credited to you.