What is a system of equations?
A system of linear equations is a set of equations that share the same unknown numbers. An example is the price of a bike and the price of a helmet. Solving it means finding the numbers that make every equation true at once.
Many computing problems reduce to this, and elimination is the method a computer uses.
By the end of this lesson
Note
I can solve a small system by elimination and say when it has none or many answers.
Key words
| Word | What it means |
|---|---|
| Unknown | a number you have to find |
| System of equations | several equations that share the same unknowns |
| Elimination | removing one unknown at a time by subtracting rows |
| Pivot | the number you divide by at each step |
| Singular matrix | a matrix that squashes points and cannot be undone |
The cells students fill in are left blank in this preview.
A system is a matrix times a vector
A bike shop sells day passes. Two bikes and one helmet cost 25. One bike and three helmets cost 30. What does one bike cost, and one helmet?
Put the counts in a matrix A, the two unknown prices in a vector x and the totals in a vector b. The whole system is A times x equals b. Solving it means finding x.
Worked example: the shop's two equations
| Equation | Bikes | Helmets | Total cost |
|---|---|---|---|
| First | 2 | 1 | 25 |
| Second | 1 | 3 | 30 |
The cells students fill in are left blank in this preview.
Elimination removes one unknown at a time
Subtract a multiple of one row from another so that a column becomes zero. The last row then has only one unknown, so you can solve it.
Then work back up. Put the answer into the row above, and that row has only one unknown left.
Worked example: solving the shop's system
| Step | Row 1 | Row 2 | What happens |
|---|---|---|---|
| Start | [2, 1 | 25] | [1, 3 | 30] | Each row holds bikes, helmets and the total |
| Take half of row 1 from row 2 | [2, 1 | 25] | [0, 2.5 | 17.5] | The bikes column of row 2 is now 0 |
| Read the last row | 2.5 helmets = 17.5 | A helmet costs 7 | |
| Work back up | 2 bikes + 7 = 25 | A bike costs 9 |
The cells students fill in are left blank in this preview.
Let NumPy solve the system
docker exec jupyter python3 -c "import numpy as np; a = np.array([[2, 1], [1, 3]]); print(np.linalg.solve(a, np.array([25, 30])).round(6).tolist())"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The computer agrees with the table
The Run printed [9.0, 7.0]: a bike costs 9 and a helmet costs 7. That matches the working by hand.
Check it in the first equation: 2 x 9 + 7 = 25. And in the second: 9 + 3 x 7 = 30.
Eliminate in Python
# Rows are [bikes, helmets, total].
row1 = [2, 1, 25]
row2 = [1, 3, 30]
# Take away enough of row 1 to make the first number of row 2 zero.
factor = row2[0] / row1[0]
for i in range(3):
row2[i] = row2[i] - factor * row1[i]
print(row2)
# Work back up: helmets first, then bikes.
helmet = row2[2] / row2[1]
bike = (row1[2] - row1[1] * helmet) / row1[0]
print(bike, helmet)
# Change the totals 25 and 30 to 30 and 35. What are the prices now?
Some systems have no answer or many
Sometimes a pivot is zero. Swap that row with a row below that has a number there. If every row below has a zero too, the matrix is singular.
A singular matrix squashes the plane onto a line, so no matrix can undo it. The system then has no answer or many.
Worked example: one, many or none
| The rows | After elimination | How many answers |
|---|---|---|
| [2, 1 | 25] and [1, 3 | 30] | [2, 1 | 25] and [0, 2.5 | 17.5] | One: a bike is 9 and a helmet is 7 |
| [1, 2 | 4] and [2, 4 | 8] | [1, 2 | 4] and [0, 0 | 0] | Many: the second row says nothing new |
| [1, 2 | 4] and [2, 4 | 9] | [1, 2 | 4] and [0, 0 | 1] | None: the last row says 0 = 1 |
The cells students fill in are left blank in this preview.
Spot a singular matrix
docker exec jupyter python3 -c "import numpy as np; print(round(np.linalg.det(np.array([[2, 1], [1, 3]])), 6), np.linalg.det(np.array([[1, 2], [2, 4]])))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A determinant of zero means singular
The Run printed 5.0 0.0. The determinant is one number worked out from a square matrix. If it is 0 the matrix is singular.
The first matrix is the shop's. The second has a second row that is twice its first, so it squashes everything onto one line.
A common mistake
Warning
Many people think two equations in two unknowns always give one answer. They do not when one row is a multiple of the other. Then there are no answers or many. Check for a zero pivot before you divide.
Going further
In your own notebook, make up a system of three equations with answers you choose, such as 5, 3 and -2. Work out the totals, then ask np.linalg.solve to find your answers again.
Change one row so it is a multiple of another, and see what NumPy says.