Lecture 8 · Week 2· 18:32

Gaussian elimination

Row operations, row echelon form, back substitution.

Play: Lecture 8: Gaussian elimination
Lecture · 18:32 · Open on YouTube · Report a problem with this video
TranscriptThe full text of the lecture. Click any sentence to play the video from there.

Why elimination

Welcome back. In the last lecture, we wrote down systems of linear equations, and stored each one compactly as an augmented matrix. Today we learn how to solve them, every time. You already know the basic trick from school. Take the two equations x plus y equals three, and x minus y equals one. Subtract the second equation from the first, and x disappears. We are left with two y equals two, so y equals one. Putting y equals one back into the first equation gives x plus one equals three, so x equals two.

With three equations and three unknowns, or a hundred of each, inventing tricks one at a time quickly gets out of hand. We need a method that always works, in the same order every time, and that a computer could carry out. That method is Gaussian elimination. It has two stages. First, forward elimination clears out the variables step by step, until the system has the shape of a staircase. Then, back substitution solves it from the bottom up. The method is named after Carl Friedrich Gauss, but it is much older. A Chinese text, the Nine Chapters on the Mathematical Art, solved systems this way about two thousand years ago, with the numbers laid out in columns on a counting board.

Every step of elimination is one of three simple moves, called row operations. Let's meet them.

Elementary row operations

Here is our small system again, next to its augmented matrix. Each equation is one row of the matrix: its coefficients, then, after the bar, its right hand side. The first row operation is a swap. We may exchange two rows, just as we may list two equations in the other order. A swap changes nothing about the system: the same two equations, in a different order. The second is scaling. We may multiply a row by any nonzero number c. Multiplying both sides of an equation by the same nonzero number gives an equation with the same solutions.

For example, let's multiply the second row by two. In the first column, two times one is two. In the second column, two times minus one is minus two. And after the bar, two times one is two. The new equation, two x minus two y equals two, has the same solutions as before. The third, and the most important, is replacement. We may add a multiple of one row to a different row. This is the school trick, since subtracting one equation from another means adding minus one times it. Let's go back to the original second row, and replace it by row two minus row one. In the first column, one minus one is zero. In the second column, minus one minus one is minus two. And after the bar, one minus three is minus two.

The new second row says minus two y equals minus two. The variable x has been eliminated from it, which is exactly what we wanted. Together, these are the elementary row operations. Swap two rows. Multiply a row by a nonzero scalar. And add a multiple of one row to a different row. We write them in this short form, where the arrow means "is replaced by". Notice that a row operation always acts on whole rows, including the entries after the bar. Changing the left side of an equation without its right side would change the equation.

Row operations keep the solutions

Now the key question. When we apply row operations, do we still have the same problem? Could we gain new solutions, or lose some along the way? We call two matrices row equivalent when one can be turned into the other by a sequence of elementary row operations. Here is the theorem that makes elimination work. If two augmented matrices are row equivalent, then their systems have exactly the same solutions.

This proof is short, but it is the logical heart of the lecture, so take it slowly, and feel free to pause or rewatch it. It is enough to check a single row operation, since a sequence is just one operation after another. The proof has two halves. First, we show that no solutions are lost: every solution of the old system is a solution of the new one. A swap or a scaling clearly keeps a solution, since it only reorders the equations, or multiplies both sides of one equation by c. Now take a replacement, and write L i for the left side of equation i.

If our solution makes L i equal to b i, and L j equal to b j, then adding c times the second equation to the first shows that L i plus c L j equals b i plus c b j. That is exactly the new row i. Intuitively, the new row adds no new information. It is built from rows i and j, and row j is still in the system. Subtract c times row j, and row i comes back. So the new system says exactly what the old one said: no solution is lost, and none is gained. Second, we show that no solutions are gained. The trick is that every row operation can be undone by another row operation.

A swap is undone by swapping back. Multiplying by c is undone by multiplying by one over c, which exists because c is not zero. And adding c times row j is undone by subtracting c times row j. So the old system comes from the new one by a row operation. By the first half, every solution of the new system is a solution of the old one. The two solution sets are equal, and that completes the proof. This is where the rule c not equal to zero matters. Multiplying a row by zero would turn it into zero equals zero, and the information in that equation would be lost for good. That step could not be undone, and it could add new solutions.

This theorem may look obvious, but it is the reason every answer we find by elimination is an answer to the original system. We will rely on it every time we row reduce a matrix in this course.

Seeing it in the plane

With two unknowns, we can watch this theorem at work. This interactive figure is on the lecture page, at opensemester dot com, so you can try it yourself. Our two equations are two lines in the plane: row one is the blue line, row two is the coral line, and the solution (two, one) is the point where they meet. We choose the replacement R two is replaced by R two plus c R one. Row two, in coral, is the row being replaced, and the dashed green line is the new second equation. The slider sets c.

As c changes, the green line turns. Watch the point where the lines meet. Whatever c is, the green line passes through the solution. A replacement never moves the solution, which is exactly the theorem we just proved. At c equals minus one, the green line is level. Its equation, minus two y equals minus two, no longer contains x. Elimination is the search for exactly this value of c.

Now let's try to scale row two by zero. The figure refuses: the row would become zero equals zero, which every point of the plane satisfies. On the lecture page, you can also pick another system, including one with no solution, and try every operation yourself.

Row echelon form

Row operations change a system without changing its solutions. So what shape should we aim for? The answer is a staircase. In each row that is not all zeros, the first nonzero entry is called the leading entry of that row. A matrix is in row echelon form when two things hold. First, any rows made entirely of zeros are at the bottom. Second, each leading entry is in a column strictly to the right of the leading entry in the row above it.

As a result, every entry below a leading entry is zero. The leading entries step down and to the right, like a staircase, and everything under the staircase is zero. The leading entries of a row echelon form are called pivots, and the columns that hold them are called pivot columns. In lecture nine, we study pivots in detail. Let's test the definition on three matrices. The first one is in row echelon form. Its leading entries step to the right, and its zero row is at the bottom.

The second is not. The leading entries of its second and third rows are in the same column, so the third one does not step to the right. The third is not in row echelon form either. It has a zero row above a nonzero row. Why this shape? A system in row echelon form can be solved from the bottom up, one variable at a time. Getting the matrix into this form is the job of forward elimination.

Forward elimination: an example

Let's solve a system of three equations in three unknowns. x plus two y plus z equals three. Two x plus five y plus three z equals eight. And minus x plus two z equals four. We write its augmented matrix. Notice the zero in the third row, because there is no y in the third equation. The first pivot is the one in the top left corner. Our goal is to make every entry below it zero, using replacements with row one.

The entry below it, in row two, is two. Two divided by the pivot is two, so we replace row two by row two minus two times row one. Column by column. In the first column, two minus two times one is zero. In the second column, five minus two times two is one. In the third column, three minus two times one is one. And after the bar, eight minus two times three is two. The new second row is (zero, one, one, two).

In row three, the entry below the pivot is minus one. To clear it, we add row one to row three. In the first column, minus one plus one is zero. In the second, zero plus two is two. In the third, two plus one is three. And after the bar, four plus three is seven. The first column is now clear below the pivot. We set row one aside, and repeat the process on the rows below it. The next pivot is the one in row two, column two.

Below this pivot is a two, so we replace row three by row three minus two times row two. The first column stays zero, since both rows have a zero there. In the second column, two minus two times one is zero. In the third column, three minus two times one is one. And after the bar, seven minus two times two is three. The matrix is now in row echelon form. Its pivots, one, one and one, run down the diagonal, with zeros everywhere below them.

Read as equations, it says x plus two y plus z equals three, y plus z equals two, and z equals three. By our theorem, this system has exactly the same solutions as the one we started with.

Back substitution

Now the second stage, back substitution. We start from the last equation, and work our way up. The last row says z equals three. That already tells us z. The second row says y plus z equals two. We substitute z equals three, so y plus three equals two, and y equals minus one.

The first row says x plus two y plus z equals three. We substitute both values. x plus two times minus one, plus three, equals three. That is x plus one equals three, so x equals two. So the system has exactly one solution. x equals two, y equals minus one, and z equals three. As a vector, the solution is (two, minus one, three). It is always worth checking a solution in the original equations. In the first, two, plus two times minus one, plus three, is three. In the second, two times two, plus five times minus one, plus three times three, is eight. In the third, minus two, plus two times three, is four. All three hold.

In the column picture from the last lecture, this says that two times the first column, minus the second column, plus three times the third column, is exactly the right hand side.

The algorithm in general

Our example needed only replacements. In general, two other things can happen, and the other two row operations deal with them. Here is the method in full. Step one. Find the leftmost column that is not all zeros. Step two. If its top entry is zero, swap in a row that has a nonzero entry there. That entry becomes the pivot. Step three. Use replacements to make every entry below the pivot zero. Step four. Set the pivot row aside, and repeat on the rows below it, until no nonzero rows are left.

Scaling is never required, but it is often convenient. Dividing a row by its pivot makes the pivot equal to one, and keeps the numbers simple. This gives us a theorem: every matrix is row equivalent to a matrix in row echelon form. The four steps are the proof. They work on any matrix and use only row operations, so the result is row equivalent to the matrix we started with. They always stop, because each round sets aside one more row. And when they stop, the pivots step to the right, with any zero rows at the bottom. That is row echelon form. Let's see a swap and a scaling in action. Here is a system whose first equation has no x. Two y plus four z equals six. x plus y plus z equals two. And two x plus three y plus z equals one.

The top entry of the first column is zero, so it cannot be a pivot. We swap rows one and two, and now the pivot is one. To clear the first column, we replace row three by row three minus two times row one. Two minus two is zero. Three minus two is one. One minus two is minus one. And after the bar, one minus four is minus three. Row two is (zero, two, four, six). We scale it by one half, to make its pivot one. Zero stays zero. Two halves is one. Four halves is two. And six halves is three.

Below the new pivot there is a one, so we replace row three by row three minus row two. One minus one is zero. Minus one minus two is minus three. And after the bar, minus three minus three is minus six. Now back substitution. The last row says minus three z equals minus six, so z equals two. The second row says y plus two z equals three, so y is three minus four, which is minus one. And the first row says x plus y plus z equals two, so x is two, plus one, minus two, which is one. Notice that we only ever added, subtracted, multiplied and divided. So Gaussian elimination works in exactly the same way over any field, such as the rational numbers, the complex numbers, or the finite fields from lecture six.

Echelon forms are not unique. Different choices of row operations can lead to different staircases. In lecture nine, we go one step further, to a form that is unique.

When there is no solution

Not every system has a solution, and elimination tells us when. Take x plus y plus two z equals one, two x plus three y plus five z equals four, and x plus two y plus three z equals six. We replace row two by row two minus two times row one. Two minus two is zero. Three minus two is one. Five minus four is one. And four minus two is two. Then we replace row three by row three minus row one. One minus one is zero. Two minus one is one. Three minus two is one. And six minus one is five.

Finally, we replace row three by row three minus row two. One minus one is zero, and one minus one is zero again. And after the bar, five minus two is three. The last row reads zero x plus zero y plus zero z equals three. That is, zero equals three. No choice of x, y and z can make this true. So this system has no solution, and since row operations do not change the solution set, neither does the original system. A system with no solution is called inconsistent.

In general, whenever an echelon form has a row of zeros followed by a nonzero entry after the bar, the system is inconsistent. But suppose the third equation had ended in three, instead of six. Then after the bar we would get three, minus one, minus two, which is zero. The last row would say zero equals zero, which is always true. Then the system has infinitely many solutions, one for each value of z. Variables that are free to take any value are the subject of lectures nine and ten.

Summary

Let's summarise. The three elementary row operations are: swap two rows, multiply a row by a nonzero scalar, and add a multiple of one row to another. Every row operation can be undone, so row operations never change the solution set of a system. A matrix is in row echelon form when its zero rows are at the bottom, and its leading entries, the pivots, step to the right. Every matrix can be brought to row echelon form.

Gaussian elimination solves a system in two stages: forward elimination to reach row echelon form, then back substitution from the bottom up. A row that says zero equals a nonzero number means there is no solution. In lecture eighteen, we will see that elimination is really a way of factoring a matrix, and that this is how computers solve large systems. Before the next lecture, read section two point three of the course book, try the elimination problems on sheet two, and do the lecture check on this lecture's page at Open Semester dot com. Next time, we push elimination one step further, to the reduced row echelon form, and learn to read off every solution of a system. See you there.

In this lecture

  1. Why elimination
  2. Elementary row operations
  3. Row operations keep the solutions
  4. Seeing it in the plane
  5. Row echelon form
  6. Forward elimination: an example
  7. Back substitution
  8. The algorithm in general
  9. When there is no solution
  10. Summary

Materials

Try it

Row operations never move the solution

Check yourself

3 questions, from warm-up to stretch

Work each one out on paper, type your answer and press Check. Stuck? Open a hint; the worked solution is there when you need it.

  1. Question 1 of 3 Warm-up

    Apply the replacement to the augmented matrix . What is the new second row ?

    Hint 1 of 2

    Work column by column: each new entry is the entry of minus times the entry of above it, including the entry after the bar.

    Hint 2 of 2

    In the first column, . Now do the same in the second column and after the bar.

    Worked solution
    1. First column: . Second column: . After the bar: .
    2. The new second row is . The first column is now zero below the pivot, so the first unknown has been eliminated from the second equation.
  2. Question 2 of 3 Core

    Solve by Gaussian elimination: , , .

    Hint 1 of 2

    Write the augmented matrix and use the pivot in the top left corner to clear the first column: and .

    Hint 2 of 2

    After those two replacements the lower rows are and . Clear below the second pivot with , then substitute from the bottom up.

    Worked solution
    1. The augmented matrix is .
    2. gives (, , , ), and gives (, , , ).
    3. gives (, , ). The matrix is now in row echelon form.
    4. Back substitution: , so . Then gives . Then gives , so .
    5. Check in the original equations: , , .
  3. Question 3 of 3 Stretch

    Consider the system , , . For which value of does it have solutions, and how many does it have then?

    Number of solutions for that
    Hint 1 of 2

    Reduce the augmented matrix to row echelon form, keeping as a letter after the bar: start with and .

    Hint 2 of 2

    After the last row is . When does it say something true?

    Worked solution
    1. The augmented matrix is .
    2. gives (, , , ), and gives .
    3. gives .
    4. If , this row says with : no solution. If , it says , and the system is , .
    5. Then is free: and . There is one solution for each value of , so infinitely many.

Found a mistake, or something not working on this page? Report it and we'll fix it. See what we've corrected.