What a linear system is
A linear equation in the unknowns multiplies each unknown by a number, adds the results and sets the sum equal to a number. There are no powers, roots or products of unknowns: is linear, is not.
A linear system is a set of linear equations that must all hold at once. A solution is a list of values for the unknowns that satisfies every equation.
Lesson 1.2.1 solved one already. The lines and meet at . Rename and as and , and bring the unknowns to the left:
Lesson 3.2.1 multiplied a matrix by a vector: entry of is row of times . So the whole system is one matrix equation:
Rule: A linear system
is the coefficient matrix, with one row per equation and one column per unknown. is the vector of unknowns and the right-hand side. Row of is equation .
Two pictures of one system
The row picture. Each equation is a line in the plane of and . A solution lies on both lines, so it is the point where they meet: .
The column picture. Lesson 3.2.1 also read as a linear combination of the columns of (lesson 3.1.1). The columns of are and , each a column vector. The system asks: how much of each column adds up to ?
Rule: The column picture
Solving means finding the combination of the columns of that gives . The solution's entries are the amounts of each column.
The same system twice. As rows, two lines meet at ; elimination swaps the second line for , dashed, through the same point. As columns, 2 of and then 5 of reach .
With three unknowns, each equation is a flat plane in space and the row picture looks for the point on all three. The column picture combines three columns in , the space of lists of three numbers. Lesson 3.3.2 shows what happens when the lines or planes do not meet in one point.
Try it
The first line multiplies by and gets : the row picture. The second adds 2 of column 1 and 5 of column 2: the column picture. A[:, 0] is the first column.
The last line asks NumPy for the solution, with np.linalg.solve (end of this lesson).
The augmented matrix and three row operations
Elimination works on the numbers alone. Write as an extra column beside , behind a bar: this is the augmented matrix . Each row is one equation.
Write for the rows. Three row operations change the equations without changing the solutions.
Rule: Row operations
Swap two rows; multiply a row by a number that is not 0; subtract a multiple of one row from another. Each can be undone, by the same kind of operation, so no solution is lost or gained.
To eliminate from the second equation, add 2 times row 1 to row 2. That is . The new row 2 is , which says .
In the row picture, the second line has become the flat line . It still passes through , because a row operation keeps the solutions: the dashed line in the figure.
Now solve from the bottom up. Row 2 gives . Row 1 then gives .
Try it
M is the augmented matrix, made of decimals (dtype=float) so that its rows can hold fractions. M[1] is row 2: Python counts from 0.
The row operation is one line. The last two lines solve from the bottom up.
Elimination, pivots and back substitution
Elimination aims for row echelon form: a staircase of zeros below the diagonal. The first nonzero entry of each row is its pivot, and each pivot sits to the right of the pivot in the row above. Rows of zeros, if any, go at the bottom.
Work one column at a time, from left to right. Use the pivot to clear every entry below it.
Rule: One elimination step
The multiplier is the entry below the pivot divided by the pivot. Subtracting times the pivot row puts a 0 in that place. Then back substitution solves the last equation first and works upwards.
A 3 × 3 example by hand
Solve this system:
Its augmented matrix is
The first pivot is the 1 in the top left. Two steps clear column 1, and one more clears column 2 below the new pivot.
| step | operation | multiplier | new row of | new entry of |
|---|---|---|---|---|
| 1 | 2 | |||
| 2 | 5 | |||
| 3 | 9 |
The result is in row echelon form, with pivots 1, 1 and 3 on the diagonal:
Back substitution reads it from the bottom:
So . Check it in the third original equation: .
Try it
The three row operations of the table, then back substitution. Each x uses the ones already found below it.
The last line multiplies the original by the answer and gets back.
A zero pivot: swap rows
Sometimes the pivot place holds a 0, and dividing by it is impossible. Then swap in a row from below that has a nonzero entry in that column: the first row operation.
Take this system:
Clearing column 1 with and leaves a 0 where the second pivot should be:
Row 3 has a 1 in column 2, so swap rows 2 and 3. The matrix is then already in row echelon form. Back substitution gives , and .
If every entry at and below the pivot place is 0, no swap helps: that column has no pivot. Lesson 3.3.2 shows that the system then has no solution or infinitely many.
Try it
The cell clears column 1, prints the 0 in the second pivot place, swaps rows 2 and 3, and prints the staircase.
M[[1, 2]] = M[[2, 1]] swaps two rows. The last line solves the swapped system and gets .
A tiny pivot needs a swap too
A pivot that is not 0 but very small causes a different problem: rounding. A computer keeps about 16 significant digits (lesson 2.8.1), and a tiny pivot makes a huge multiplier.
Solve and . The answer is very close to . With the pivot , the multiplier is , and row 2 becomes .
To 16 digits, both and round to . So , and then row 1 gives : wrong.
Swap the rows first and the multiplier is instead. Nothing important is lost, and the answer is .
Rule: Partial pivoting
Before clearing column , swap up the row, at or below row , whose entry in that column is largest in size. Every multiplier is then at most 1 in size, which keeps rounding errors small. It also takes care of zero pivots.
Try it
two_by_two eliminates and back-substitutes without any swap. On the original order it returns .
M[[1, 0]] is the same matrix with its rows swapped, and gives the right answer. np.linalg.solve gets it right on the original order, because it swaps for itself.
Solving with NumPy
np.linalg.solve(A, b) returns the solution of for a square . NumPy passes the work to LAPACK, a library of compiled linear-algebra routines. Its routine runs this lesson's method: elimination with partial pivoting, then back substitution.
Lesson 3.2.3 could also solve the system as , with np.linalg.inv. Computing the whole inverse takes more arithmetic than one elimination and adds rounding, so solve is the better choice.
When elimination finds a column with no pivot, solve stops with LinAlgError: Singular matrix, though rounding can hide a missing pivot (lesson 3.3.2). A matrix that is not square is refused too. Lesson 3.3.2 deals with both kinds of system, and case study 3.3.4 uses solve to copy one asset's payoff with others.
Try it
solve on the 3 × 3 example, then a check: . Its entries are 0 or tiny, such as : rounding, so the check passes.
The singular matrix has a second row that is twice the first, so elimination leaves a row of zeros and solve raises an error.
Common mistakes
- Apply a row operation to the whole row, right-hand side included. Changing and not solves a different system.
- Watch the sign of the multiplier. To clear a below a pivot of 1, , so the step is .
- Never divide by a zero pivot. Swap in a row from below. If every entry below is 0 too, the column has no pivot (lesson 3.3.2).
- Multiplying a row by 0 is not a row operation. It throws an equation away and can add false solutions.
- Back-substitute from the bottom. The last row has one unknown; each row above adds one more.
- A tiny pivot is a trap in code. Swap up the largest entry in size, as
np.linalg.solvedoes. - Prefer
solve(A, b)toinv(A) @ b, and check the answer by computing .
Linear systems and elimination
0 of 11 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A linear system is . Each row is an equation, a line or a plane; the solution is the combination of the columns of that gives .
- Row operations (swap, scale by a nonzero number, subtract a multiple of another row) change the equations but not the solutions. Work on the augmented matrix .
- Elimination clears each column below its pivot with multiplier , giving row echelon form. Back substitution then solves from the bottom up.
- A zero pivot needs a row swap. Partial pivoting always swaps up the largest entry in size, which also protects against rounding from tiny pivots.
np.linalg.solve(A, b)does elimination with partial pivoting; prefer it toinv(A) @ b. It raisesLinAlgErrorfor a singular matrix.