1instructorguide

These exercises are designed to get students started on using computers to solve systems of linear equations. Students should have some experience in an appropriate computational package before attempting this. Mathematica is a very good choice of tools for this exercise, closely followed by Python/numpy and MatLab. C/C++ also works, with appropriate libraries. The systems of equations we’ll be using come from resistor networks typical of a second-semester physics course, but the same techniques can be used for any system of linear equations regardless of their source.

Being able to solve systems of equations is an important computational skill, with applications in everything from mechanical engineering to quantum physics. For small systems of equations it is fairly simple to solve the systems algebraically, but for large complex systems it’s important to be able to solve them numerically using computers.

2theory

We first express the system of equations as a single matrix equation,

$\begin{array}{}\text{(1)}& \mathbf{M}\mathbf{x}=\mathbf{b}\end{array}$

Since there are as many equations (rows in $\mathbf{M}$) as there are variables (columns in $\mathbf{M}$), $\mathbf{M}$ will be a square matrix. If there is a solution to this set of equations, then there will exist another matrix ${\mathbf{M}}^{-1}$ such that ${\mathbf{M}}^{-1}\mathbf{M}=\mathbf{I}$, where $\mathbf{I}$ is the identity matrix. In that case,

$\begin{array}{}\text{(2)}& {\mathbf{M}}^{-1}\mathbf{M}\mathbf{x}={\mathbf{M}}^{-1}\mathbf{b}\end{array}$
$\begin{array}{}\text{(3)}& \mathbf{I}\mathbf{x}=\mathbf{x}={\mathbf{M}}^{-1}\mathbf{b}\end{array}$

There are algorithmic methods for finding ${\mathbf{M}}^{-1}$ (and thus $\mathbf{x}$; in our case we’ll be using standard libraries to calculate this matrix and the corresponding solution. One standard method is to have the computer calculate ${\mathbf{M}}^{-1}$ directly using an “invert()” function, and then multiply that inverse matrix by $\mathbf{b}$ to obtain $\mathbf{x}$.

Most computational packages that have the ability to calculate ${\mathbf{M}}^{-1}$ also have an additional function, often called “solve()”, that takes care of the multiplication as well, and gives the user $\mathbf{x}$ directly.

For more information about how these functions work, see Numerical Recipes in C by Flannery, Teukolsky, Press, and Vetterling, or similar text.

3exercises

## Exercise 1: Introductory Problem

Consider the network of resistors and batteries shown in the first figure below.

There are three unknowns in that circuit, ${I}_{1}$, ${I}_{2}$, and ${I}_{3}$. We can solve for these unknowns by building a system of equations using Kirchhoff’s Laws:

1. The sum of voltages around any loop is zero.
2. The sum of currents at any junction is zero.

Applying the voltage law to the left-hand loop, we get

$\begin{array}{}\text{(4)}& {V}_{1}-{I}_{1}{R}_{1}-{I}_{3}{R}_{3}=0\end{array}$

From the right-hand loop, we get

$\begin{array}{}\text{(5)}& {V}_{2}+{I}_{2}{R}_{2}-{I}_{3}{R}_{3}=0\end{array}$

We need one more equation, for which we can use the junction at top center and the current law:

$\begin{array}{}\text{(6)}& {I}_{1}-{I}_{2}-{I}_{3}=0\end{array}$

We now have the requisite three independent equations, which we can solve using methods learned in high-school algebra.

There’s another way of solving this, using matrix methods. First, rewrite the equations just a bit.

And now we can see that this can be written as a matrix equation!

This matrix equation,

$\begin{array}{}\text{(11)}& \mathbf{M}\mathbf{x}=\mathbf{b}\end{array}$

has solution

$\begin{array}{}\text{(12)}& \mathbf{x}={\mathbf{M}}^{-1}\mathbf{b}\end{array}$

where ${\mathbf{M}}^{-1}$ is the inverse of $\mathbf{M}$. Most computational packages have built-in capability for inverting matrices.

### ASSIGNMENT

1. By carrying out the matrix multiplication explicitly, show that the matrix equation above reduces to the system of equations from which it is derived.
2. Use a matrix-solving package to find the currents.
3. Check your answers by substituting the currents into the equations and verifying that they are solutions.

## Exercise 2: More complicated problem

The resistor network below is perhaps a bit more challenging.

### ASSIGNMENT

1. Write a system of equations that can be used to solve for the currents in the circuit above.
2. Re-write the system of equations from part 1 of this assignment as a matrix equation.
3. Use a matrix-solving package to find the currents.
4. Check your answers: do the values of currents you found solve the equations with which you started?
5. You may notice something interesting if you compare your solution here to your solution for exercise 1. (Assuming the same values of $R$ and $V$ were used in both exercises.) Explain why this happened.

Unless your instructor provides you with other values, assume each voltage source and each resistor has a value $10×$ its identifying number. (i.e. ${V}_{2}=20$ V, ${R}_{3}=30\mathrm{\Omega }$.)

Assignment
1. 1) By carrying out the matrix multiplication explicitly, show that the matrix equation above reduces to the system of equations from which it is derived.
2. 2) Use a matrix-solving package to find the currents.
3. 3) Check your answers by substituting the currents into the equations and verifying that they are solutions.
resistorpy

'''

resistor.py

Eric Ayars

June 2016

A demonstration program for solving a system of linear equations using Python.

'''

import numpy as np

# Start by getting the values of R and V

R1 = float(input("R1 = "))

R2 = float(input("R2 = "))

R3 = float(input("R3 = "))

V1 = float(input("V1 = "))

V2 = float(input("V2 = "))

# Use these values to build the matrix M. Note that this matrix

# will vary depending on the equation set selected to solve the

# problem: here I use the equations from the PICUP webpage.

# Here is the matrix M:

M = np.array([ [R1, 0, R3],

[0,-R2, R3],

[1, -1, -1] ])

# here is the right-hand-side b:

b = np.array([V1,V2,0])

# And this is all it takes to get a solution:

x = np.linalg.solve(M,b)

# show the result.

print(x)

# The result is an array of currents:

# [-0.09090909 -0.45454545 0.36363636]

# The first one is I_1, the second is I_2, etc.

### Software Requirements

SoftwareRequirements

 Android iOS Windows MacOS with best with Chrome Chrome Chrome Chrome support fullscreen? Yes. Chrome/Opera No. Firefox/ Sumsung Internet Not yet Yes Yes cannot work on some mobile browser that don't understand JavaScript such as..... cannot work on Internet Explorer 9 and below

### Credits

Fremont Teng; Loo Kang Wee

[text]

## Solving Resistor Networks JavaScript Simulation Applet HTML5

### Instructions

#### Combo Box and Functions

Toggling through the combobox will give you their respective functions.

Selecting Solution will give you the input fields for the respective variables
as shown in the diagram.

Whereas selecting user defined will allow you to change the Mgeneral and bgeneral
to get any solution for it.

Note that typing an invalid solution/matrix will give you an error.

#### Toggling Full Screen

Double clicking anywhere in the panel will toggle full screen

#### Reset Button

Resets the simulation.

Research

[text]

[text]

[text]