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.
We first express the system of equations as a single matrix equation,
Since there are as many equations (rows inM) as there are variables (columns inM),Mwill be a square matrix. If there is a solution to this set of equations, then there will exist another matrixM−1such thatM−1M=I, whereIis the identity matrix. In that case,
There are algorithmic methods for findingM−1(and thusx; 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 calculateM−1directly using an “invert()” function, and then multiply that inverse matrix bybto obtainx.
Most computational packages that have the ability to calculateM−1also have an additional function, often called “solve()”, that takes care of the multiplication as well, and gives the userxdirectly.
For more information about how these functions work, seeNumerical Recipes in Cby Flannery, Teukolsky, Press, and Vetterling, or similar text.
Exercise 1: Introductory Problem
Consider the network of resistors and batteries shown in the first figure below.
There are three unknowns in that circuit,I1,I2, andI3. We can solve for these unknowns by building a system of equations using Kirchhoff’s Laws:
The sum of voltages around any loop is zero.
The sum of currents at any junction is zero.
Applying the voltage law to the left-hand loop, we get
From the right-hand loop, we get
We need one more equation, for which we can use the junction at top center and the current law:
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.