Search

Search papers and lessons

Gaussian Elimination

Level 2 · 12 min read

Gaussian elimination solves a system of linear equations by subtracting multiples of one equation from the others until the unknowns can be read off one at a time.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

TL;DR

Gaussian elimination solves a system of linear equations by a fixed, mechanical procedure. You subtract multiples of one equation from the others to remove a variable from them, repeat until each equation has fewer unknowns than the one above, and then solve from the bottom up. The same steps work for two equations or for two thousand. The method also reveals whether a system has one solution, none, or infinitely many.

Where you'll see it: solving the equations behind a circuit, a structural load, a mixing problem or a least-squares fit, and inside the numerical routines that compute a matrix inverse or a Determinant.

Intuition

Solving by hand

Suppose two numbers satisfy these two conditions: their sum is 5, and their difference is 1. You can find them without any new theory. Add the two conditions and the yy disappears, leaving 2x=62x = 6. So x=3x = 3, and then y=2y = 2.

That move is the whole idea. You combine equations so that one unknown drops out of all but one of them. Each equation you then have is simpler, so you can solve it. Gaussian elimination is this move written as a routine that works on any number of equations, and that does not need you to notice which combination to try.

A procedure a computer can follow

The routine has three stages, shown below. First you write the equations as a grid of numbers. Then you clear the entries under each "pivot" position, one column at a time, moving left to right. Finally you read off the unknowns from the bottom equation up.

flowchart TD
  A[Write the system as a grid of numbers] --> B[Choose a pivot in the current column]
  B --> C[Clear the entries below the pivot]
  C --> D[More columns left?]
  D -->|yes| B
  D -->|no| E[Solve from the last equation upward]

Nothing in the procedure needs insight. That is why it became the default method for solving linear systems on computers.

The math

The system and its grid

A linear system is a set of equations in which each unknown appears only multiplied by a number and added to the others. We write it compactly as

Ax=b,A\mathbf{x} = \mathbf{b},

where AA is a Matrix of coefficients, x\mathbf{x} is the Vector of unknowns we want, and b\mathbf{b} is the vector of right-hand sides. The augmented matrix [A∣b][A \mid \mathbf{b}] puts the right-hand sides in an extra column, so the whole system fits in one grid. For example,

2x+y=5x−y=1⟷[2151−11].\begin{aligned} 2x + y &= 5 \\ x - y &= 1 \end{aligned} \qquad\longleftrightarrow\qquad \left[\begin{array}{cc|c} 2 & 1 & 5 \\ 1 & -1 & 1 \end{array}\right].

Three allowed moves

Gaussian elimination uses three row operations. You may:

  1. swap two rows;
  2. multiply a row by a nonzero number;
  3. add a multiple of one row to another row.

Each move leaves the set of solutions unchanged. Swapping equations changes nothing. Scaling an equation by a nonzero number gives an equivalent statement. Adding a multiple of one equation to another keeps every solution, because whatever values satisfy both original equations also satisfy the new combination. Since each move can be undone, no solution is gained or lost.

Row echelon form

The goal of the forward pass is row echelon form. In that form, each row starts with more zeros than the row above it, and every entry below a pivot (the first nonzero entry of a row) is zero. A system in this shape is easy to solve from the bottom up.

To get there, use the pivot in each column to zero the entries beneath it. If the entry you want to use as pivot is zero, swap in a lower row that has a nonzero entry in that column. The update for row jj using pivot row ii is

Rj←Rj−aj,kai,kRi,R_j \leftarrow R_j - \frac{a_{j,k}}{a_{i,k}} R_i,

where aj,ka_{j,k} is the entry in row jj and the pivot column kk, and ai,ka_{i,k} is the pivot. To avoid fractions you can instead use Rj←ai,kRj−aj,kRiR_j \leftarrow a_{i,k} R_j - a_{j,k} R_i, which gives the same zero and a row that is a multiple of the old one.

Back substitution

Once the grid is in row echelon form, solve the last equation first, then substitute its answer into the one above, and continue upward. This step is back substitution. Each equation then has only one new unknown, so it is solved directly.

Three possible outcomes

Elimination tells you how many solutions the system has.

  • Exactly one solution: every column has a pivot. For a square system this is the usual case.
  • No solution: elimination produces a row of the form 0=c0 = c with cc nonzero. The equations contradict each other.
  • Infinitely many solutions: some column has no pivot. The variable in that column is a free variable and can be set to any value, and the others follow from it.

The number of pivots is the rank of AA, so the rank tells you which of the three cases applies.

Cost

For a system with nn equations and nn unknowns, elimination takes about 23n3\tfrac{2}{3}n^3 arithmetic operations, most of them in the forward pass. The exact constant matters less than the cubic growth: doubling the size of the system multiplies the work by about eight. This is why solving very large systems requires special structure, as discussed below.

Worked examples

Example
Tiny: two equations

Solve 2x+y=52x + y = 5 and x−y=1x - y = 1.

  1. Write the augmented matrix: [2151−11]\left[\begin{array}{cc|c} 2 & 1 & 5 \\ 1 & -1 & 1 \end{array}\right].
  2. Use the first row's pivot, 2, to clear the entry under it. Replace row 2 by 2R2−R12 R_2 - R_1. The new row 2 is (2−2,  −2−1∣2−5)=(0,−3∣−3)(2 - 2,\; -2 - 1 \mid 2 - 5) = (0, -3 \mid -3).
  3. The grid is now [2150−3−3]\left[\begin{array}{cc|c} 2 & 1 & 5 \\ 0 & -3 & -3 \end{array}\right], which is row echelon form.
  4. Solve the last row: −3y=−3-3y = -3, so y=1y = 1.
  5. Substitute into the first row: 2x+1=52x + 1 = 5, so x=2x = 2.

Interpretation: (x,y)=(2,1)(x, y) = (2, 1). Check: 2×2+1=52 \times 2 + 1 = 5 and 2−1=12 - 1 = 1, both correct. Both pivots, 2 and −3-3, are nonzero, so the solution is unique.

Example
Standard: three equations and three unknowns

Solve the system

x+2y+z=82x−y+3z=9x+y−z=0.\begin{aligned} x + 2y + z &= 8 \\ 2x - y + 3z &= 9 \\ x + y - z &= 0. \end{aligned}
  1. Augmented matrix: [12182−13911−10]\left[\begin{array}{ccc|c} 1 & 2 & 1 & 8 \\ 2 & -1 & 3 & 9 \\ 1 & 1 & -1 & 0 \end{array}\right].
  2. Clear column 1. Replace row 2 by R2−2R1=(0,−5,1∣−7)R_2 - 2R_1 = (0, -5, 1 \mid -7). Replace row 3 by R3−R1=(0,−1,−2∣−8)R_3 - R_1 = (0, -1, -2 \mid -8).
  3. Clear column 2 using the pivot −5-5 in row 2. Replace row 3 by 5R3−R25R_3 - R_2. The yy entry becomes 5(−1)−(−5)=05(-1) - (-5) = 0, the zz entry 5(−2)−1=−115(-2) - 1 = -11, and the right-hand side 5(−8)−(−7)=−335(-8) - (-7) = -33. So row 3 is (0,0,−11∣−33)(0, 0, -11 \mid -33).
  4. Solve row 3: −11z=−33-11z = -33, so z=3z = 3.
  5. Row 2: −5y+z=−7-5y + z = -7, so −5y=−10-5y = -10 and y=2y = 2.
  6. Row 1: x+2(2)+3=8x + 2(2) + 3 = 8, so x=1x = 1.

Interpretation: (x,y,z)=(1,2,3)(x, y, z) = (1, 2, 3). Check the first equation: 1+4+3=81 + 4 + 3 = 8. Each column had a pivot (1, −5-5, −11-11), so the answer is the only one. Note that step 3 needed no fraction: multiplying a row by a nonzero number is allowed, and keeping integers makes the arithmetic easier to check by hand.

Example
Realistic: mixing three stock solutions

A lab wants 100 mL of solution containing 25 g of salt. It has three stock solutions: 10%, 20% and 50% salt by mass (treat 1 mL as 1 g for this exercise). Let xx, yy and zz be the millilitres of each stock. The recipe must use 2 times as much of the 20% stock as of the 50% stock.

x+y+z=1000.10 x+0.20 y+0.50 z=25y−2z=0\begin{aligned} x + y + z &= 100 \\ 0.10\,x + 0.20\,y + 0.50\,z &= 25 \\ y - 2z &= 0 \end{aligned}
  1. Multiply the middle equation by 10 to clear decimals: x+2y+5z=250x + 2y + 5z = 250.
  2. Augmented matrix: [11110012525001−20]\left[\begin{array}{ccc|c} 1 & 1 & 1 & 100 \\ 1 & 2 & 5 & 250 \\ 0 & 1 & -2 & 0 \end{array}\right].
  3. Replace row 2 by R2−R1=(0,1,4∣150)R_2 - R_1 = (0, 1, 4 \mid 150).
  4. Replace row 3 by R3−R2=(0,0,−6∣−150)R_3 - R_2 = (0, 0, -6 \mid -150).
  5. Solve the last row: −6z=−150-6z = -150, so z=25z = 25.
  6. From y−2z=0y - 2z = 0: y=50y = 50.
  7. From x+y+z=100x + y + z = 100: x=25x = 25.

Interpretation: mix 25 mL of the 10% stock, 50 mL of the 20% stock and 25 mL of the 50% stock. Check the salt: 0.10(25)+0.20(50)+0.50(25)=2.5+10+12.5=250.10(25) + 0.20(50) + 0.50(25) = 2.5 + 10 + 12.5 = 25 grams. All three pivots are nonzero, so the recipe is the only one that meets all three conditions.

Tradeoffs and failure modes

Elimination is reliable on paper, but a computer running it on floating-point numbers (real numbers stored with a limited number of digits, so each operation rounds slightly) can go wrong in ways that are not visible from the algebra.

SituationProblemBetter choice
A pivot is exactly zeroDividing by it is impossibleSwap in a lower row with a nonzero entry (partial pivoting)
A pivot is tiny but not zeroRounding error is multiplied by a huge factor and swamps the answerPartial pivoting: pick the largest entry in the column as the pivot
Many unknowns but most coefficients are zeroElimination fills in the zeros and wastes memory and timeA sparse-aware ordering, or an iterative method
Same AA, many different right-hand sides b\mathbf{b}Elimination is repeated from scratch each timeFactor AA once into a lower triangular and an upper triangular matrix (all entries above, respectively below, the diagonal are zero), called an LU factorisation, and reuse it
More equations than unknownsUsually no exact solutionLeast squares (the choice that makes the total squared error smallest), as in Linear Regression

Two failure modes need extra care. First, the algebra can be correct while the numbers are not: a near-zero pivot in floating point can give an answer with no correct digits. Use pivoting, and check the residual Ax−bA\mathbf{x} - \mathbf{b} (how far the computed answer is from satisfying the equations) after solving. Second, a system with free variables has many solutions, and a routine that silently picks one may mislead you. Report the rank and the free variables, not just one vector that happens to solve the equations.

History

Methods equivalent to elimination are much older than the name. The Nine Chapters on the Mathematical Art, a Chinese mathematical text compiled over the first centuries of the common era, solves systems of linear equations by exactly this kind of column-by-column subtraction. Carl Friedrich Gauss used the procedure in 1809 to compute the orbit of the asteroid Ceres from noisy observations, which is where the name comes from.

In the late 19th century, Wilhelm Jordan published a variant that also clears the entries above each pivot, and the combined name "Gauss–Jordan elimination" is now common. In 1948, Alan Turing analysed how rounding errors grow during elimination, which laid groundwork for pivoting and for the factorisation methods used in software today.

It caught on because it is mechanical, it works for any size of system, and it became straightforward to code. Most of the numerical linear algebra in science and engineering is built on it.

Recap

A linear system is written as a grid, and row operations that keep its solutions unchanged reduce the grid to row echelon form, with a pivot in each column. Back substitution then reads off the unknowns, and the presence or absence of pivots tells you whether the solution is unique, absent or one of infinitely many. The number of pivots is the rank, and the product of the pivots, adjusted for row swaps, gives the Determinant. Those links lead on to the inverse and to the Schur complement, which applies the same elimination to a system split into blocks.

Check yourself

QuestionWhy does adding a multiple of one equation to another not change the solution set?Show answer

Any values that satisfy both original equations also satisfy their combination, since that combination is built from two statements that are both true. Conversely, the original pair can be recovered by reversing the operation, so no solution is gained or lost. This is why the row operations are allowed.

QuestionSolve x+y=4x + y = 4 and 2x−y=22x - y = 2 by elimination. Show the pivot and the result of clearing the entry under it.Show answer

Use the pivot 1 in row 1. Replace row 2 by R2−2R1=(0,−3∣−6)R_2 - 2R_1 = (0, -3 \mid -6). So −3y=−6-3y = -6 gives y=2y = 2, and x+2=4x + 2 = 4 gives x=2x = 2. Check: 2+2=42 + 2 = 4 and 4−2=24 - 2 = 2, both correct.

QuestionIn a system with three equations and three unknowns, elimination produces the row (0,0,0∣5)(0, 0, 0 \mid 5). What does this mean, and what would change if the last entry were 0?Show answer

The row says 0=50 = 5, which no values can satisfy, so the system has no solution. If the last entry were 0, the row would say 0=00 = 0, which is always true and gives no information. Then the third column has no pivot, the variable it belongs to is free, and the system has infinitely many solutions.

QuestionYou must solve the same 1000-equation system Ax=bA\mathbf{x} = \mathbf{b} for 500 different right-hand sides b\mathbf{b}. Why is it wasteful to run full elimination every time, and what would you do instead?Show answer

Full elimination costs on the order of n3n^3 operations, and most of that work depends only on AA, not on b\mathbf{b}. Factor AA once into lower and upper triangular parts, then for each new b\mathbf{b} do only two cheap triangular solves. This costs on the order of n2n^2 operations per right-hand side after the one-time factorisation.

Sources