TExcelTAD
Home › Algebra › The Matrix Method

The Matrix Method

The matrix method writes a system of equations as a grid of numbers and solves it by operating on the rows of that grid. Nothing new happens mathematically: it is elimination with the letters and plus signs left out. Dropping them makes the work shorter, easier to check and easy to hand to a computer.

From equations to a matrix

A matrix is a rectangular table of numbers. For a system, write each equation as one row: the coefficients of xx, yy and zz, then a bar, then the constant. The result is the augmented matrix:

x+2y+z=02x+y−z=3x−y+2z=7→[121021−131−127]\begin{aligned} x + 2y + z &= 0 \\ 2x + y - z &= 3 \\ x - y + 2z &= 7 \end{aligned} \quad\to\quad \left[\begin{array}{ccc|c} 1 & 2 & 1 & 0 \\ 2 & 1 & -1 & 3 \\ 1 & -1 & 2 & 7 \end{array}\right]

A missing variable gets a 0 in its column, and every equation must have its variables in the same order.

The three row operations

These are the moves elimination already uses, written for rows. None of them changes the solutions:

Operation Notation Equation meaning
swap two rows R1↔R2R_1 \leftrightarrow R_2 write the equations in a different order
multiply a row by a non-zero number R2→3⋅R2R_2 \to 3 \cdot R_2 multiply both sides of an equation
add a multiple of one row to another R3→R3−2⋅R1R_3 \to R_3 - 2 \cdot R_1 add a multiple of one equation to another

The goal is row echelon form: zeros below the diagonal, a staircase shape. Then back substitution finishes the job (see Three Unknowns by Substitution).

A worked example

[121021−131−127]R2→R2−2⋅R1R3→R3−R1[12100−3−330−317]R3→R3−R2[12100−3−330044]\begin{aligned} &\left[\begin{array}{ccc|c} 1 & 2 & 1 & 0 \\ 2 & 1 & -1 & 3 \\ 1 & -1 & 2 & 7 \end{array}\right] \begin{array}{l} \\ R_2 \to R_2 - 2 \cdot R_1 \\ R_3 \to R_3 - R_1 \end{array} \\[1ex] &\left[\begin{array}{ccc|c} 1 & 2 & 1 & 0 \\ 0 & -3 & -3 & 3 \\ 0 & -3 & 1 & 7 \end{array}\right] \begin{array}{l} \\ \\ R_3 \to R_3 - R_2 \end{array} \\[1ex] &\left[\begin{array}{ccc|c} 1 & 2 & 1 & 0 \\ 0 & -3 & -3 & 3 \\ 0 & 0 & 4 & 4 \end{array}\right] \end{aligned}

Read the rows back as equations, bottom first:

4z=4→z=1−3y−3⋅1=3→−3y=6→y=−2x+2⋅(−2)+1=0→x=3\begin{aligned} 4z &= 4 && \to z = 1 \\ -3y - 3 \cdot 1 &= 3 && \to -3y = 6 \to y = -2 \\ x + 2 \cdot (-2) + 1 &= 0 && \to x = 3 \end{aligned}

Check in the original system: 3−4+1=03 - 4 + 1 = 0 ✓, 6−2−1=36 - 2 - 1 = 3 ✓, 3+2+2=73 + 2 + 2 = 7 ✓.

If you keep going until every pivot is 1 and every other entry in the pivot columns is 0, you reach reduced row echelon form (RREF), and the last column is the answer. SymPy computes it exactly:

python · try it yourself
from sympy import Matrix

M = Matrix([[1, 2, 1, 0],
            [2, 1, -1, 3],
            [1, -1, 2, 7]])
R, pivots = M.rref()
for row in R.tolist():
    print(row)
print("pivot columns:", pivots)
[1, 0, 0, 3]
[0, 1, 0, -2]
[0, 0, 1, 1]
pivot columns: (0, 1, 2)

The rows now read x=3x = 3, y=−2y = -2, z=1z = 1. The pivots tuple lists the columns that hold a leading 1.

The matrix equation AX=BAX = B

You can also split the augmented matrix into a coefficient matrix AA, a column of unknowns XX and a column of constants BB:

A=[12121−11−12]X=[xyz]B=[037]A⋅X=BA = \begin{bmatrix} 1 & 2 & 1 \\ 2 & 1 & -1 \\ 1 & -1 & 2 \end{bmatrix} \qquad X = \begin{bmatrix} x \\ y \\ z \end{bmatrix} \qquad B = \begin{bmatrix} 0 \\ 3 \\ 7 \end{bmatrix} \qquad A \cdot X = B

If AA has an inverse A−1A^{-1} (a matrix with A−1⋅A=IA^{-1} \cdot A = I, the identity), multiply both sides by it: X=A−1⋅BX = A^{-1} \cdot B. This is the matrix version of solving 3x=63x = 6 by multiplying by 13\frac{1}{3}. For a 2×22 \times 2 matrix there is a short formula:

A=[abcd]A−1=1ad−bc[d−b−ca](only if ad−bc≠0)A = \begin{bmatrix} a & b \\ c & d \end{bmatrix} \qquad A^{-1} = \frac{1}{ad - bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix} \qquad (\text{only if } ad - bc \ne 0)

The number ad−bcad - bc is the determinant from Solving with 2×2 Determinants. If it is 0, AA has no inverse and the system has no unique solution.

python · try it yourself
import numpy as np

A = np.array([[1, 2, 1], [2, 1, -1], [1, -1, 2]], dtype=float)
B = np.array([0, 3, 7], dtype=float)

print("solve:      ", np.linalg.solve(A, B))
print("inverse · B:", np.round(np.linalg.inv(A) @ B, 10))

A2 = np.array([[4, 7], [2, 6]], dtype=float)
print("2x2 inverse:")
print(np.linalg.inv(A2) * 10)          # ×10 to show the exact entries
solve:       [ 3. -2.  1.]
inverse · B: [ 3. -2.  1.]
2x2 inverse:
[[ 6. -7.]
 [-2.  4.]]

For A2A_2, ad−bc=24−14=10ad - bc = 24 - 14 = 10, so A2−1=110⋅[6−7−24]A_2^{-1} = \frac{1}{10} \cdot \begin{bmatrix} 6 & -7 \\ -2 & 4 \end{bmatrix}, which matches the printed values times 10.

Tip: In code, prefer np.linalg.solve(A, B) to np.linalg.inv(A) @ B. It does elimination directly, is faster, and loses less precision. Compute an inverse only when you need the matrix itself.

In software

  • Every numeric library solves systems this way. NumPy, SciPy, MATLAB and Excel's MINVERSE/MMULT all build on row operations (in the form of an LU decomposition).
  • Graphics. A 2D or 3D transform is a matrix; undoing it (mapping a mouse click on screen back to world coordinates) uses its inverse. See Matrix Inverses and the game-math lesson on inverse transforms.
  • Machine learning. Fitting a linear regression solves a matrix equation for the weights.

The linear algebra course treats all of this in depth: Gaussian Elimination, Gauss–Jordan elimination and Matrix Inverses.

Exercises

Exercise 1: Write the augmented matrix

Write the augmented matrix of 3x−z=53x - z = 5, x+y=2x + y = 2, 2y+4z=−12y + 4z = -1.

Answer

[30−151102024−1]\left[\begin{array}{ccc|c} 3 & 0 & -1 & 5 \\ 1 & 1 & 0 & 2 \\ 0 & 2 & 4 & -1 \end{array}\right]

Solution

Put the variables in the order xx, yy, zz and write 0 for each missing one:

3x+0y−1z=5→[ 3  0  −1∣5 ]1x+1y+0z=2→[ 1  1  0∣2 ]0x+2y+4z=−1→[ 0  2  4∣−1 ]\begin{aligned} 3x + 0y - 1z &= 5 && \to [\,3\ \ 0\ \ {-1} \mid 5\,] \\ 1x + 1y + 0z &= 2 && \to [\,1\ \ 1\ \ 0 \mid 2\,] \\ 0x + 2y + 4z &= -1 && \to [\,0\ \ 2\ \ 4 \mid {-1}\,] \end{aligned}

Exercise 2: Row reduce a 2×2

Solve x+3y=5x + 3y = 5, 2x−y=32x - y = 3 by row operations on the augmented matrix.

Answer

x=2x = 2, y=1y = 1.

Solution
[1352−13]R2→R2−2⋅R1→[ 0  −7∣−7 ]→y=1row 1: x+3=5→x=2\begin{aligned} &\left[\begin{array}{cc|c} 1 & 3 & 5 \\ 2 & -1 & 3 \end{array}\right] \quad R_2 \to R_2 - 2 \cdot R_1 \quad\to\quad [\,0\ \ {-7} \mid {-7}\,] \quad\to\quad y = 1 \\ &\text{row 1: } x + 3 = 5 \to x = 2 \end{aligned}

Exercise 3: Row reduce a 3×3

Solve x+y+z=2x + y + z = 2, x+2y+3z=5x + 2y + 3z = 5, 2x+3y+5z=92x + 3y + 5z = 9 with the matrix method. Check with rref().

Answer

x=1x = 1, y=−1y = -1, z=2z = 2.

Solution
[111212352359]R2→R2−R1R3→R3−2⋅R1[111201230135]R3→R3−R2[111201230012]\begin{aligned} &\left[\begin{array}{ccc|c} 1 & 1 & 1 & 2 \\ 1 & 2 & 3 & 5 \\ 2 & 3 & 5 & 9 \end{array}\right] \begin{array}{l} \\ R_2 \to R_2 - R_1 \\ R_3 \to R_3 - 2 \cdot R_1 \end{array} \\[1ex] &\left[\begin{array}{ccc|c} 1 & 1 & 1 & 2 \\ 0 & 1 & 2 & 3 \\ 0 & 1 & 3 & 5 \end{array}\right] \begin{array}{l} \\ \\ R_3 \to R_3 - R_2 \end{array} \\[1ex] &\left[\begin{array}{ccc|c} 1 & 1 & 1 & 2 \\ 0 & 1 & 2 & 3 \\ 0 & 0 & 1 & 2 \end{array}\right] \end{aligned}
z=2y+2⋅2=3→y=−1x−1+2=2→x=1check: 1−1+2=2 ✓1−2+6=5 ✓2−3+10=9 ✓\begin{aligned} z &= 2 \\ y + 2 \cdot 2 &= 3 && \to y = -1 \\ x - 1 + 2 &= 2 && \to x = 1 \\ &\text{check: } 1 - 1 + 2 = 2\ \checkmark \quad 1 - 2 + 6 = 5\ \checkmark \quad 2 - 3 + 10 = 9\ \checkmark \end{aligned}
python · try it yourself
from sympy import Matrix
R, _ = Matrix([[1, 1, 1, 2], [1, 2, 3, 5], [2, 3, 5, 9]]).rref()
print(R.col(-1).T)
Matrix([[1, -1, 2]])

Exercise 4: The 2×2 inverse

Find the inverse of A=[3152]A = \begin{bmatrix} 3 & 1 \\ 5 & 2 \end{bmatrix} with the formula and use it to solve 3x+y=43x + y = 4, 5x+2y=75x + 2y = 7.

Answer

A−1=[2−1−53]A^{-1} = \begin{bmatrix} 2 & -1 \\ -5 & 3 \end{bmatrix}; x=1x = 1, y=1y = 1.

Solution
ad−bc=6−5=1A−1=11⋅[2−1−53]X=A−1⋅B=[2⋅4−1⋅7−5⋅4+3⋅7]=[11]check: 3+1=4 ✓5+2=7 ✓\begin{aligned} ad - bc &= 6 - 5 = 1 \\ A^{-1} &= \tfrac{1}{1} \cdot \begin{bmatrix} 2 & -1 \\ -5 & 3 \end{bmatrix} \\ X = A^{-1} \cdot B &= \begin{bmatrix} 2 \cdot 4 - 1 \cdot 7 \\ -5 \cdot 4 + 3 \cdot 7 \end{bmatrix} = \begin{bmatrix} 1 \\ 1 \end{bmatrix} \\ &\text{check: } 3 + 1 = 4\ \checkmark \quad 5 + 2 = 7\ \checkmark \end{aligned}
python · try it yourself
import numpy as np
A = np.array([[3, 1], [5, 2]])
print(np.round(np.linalg.inv(A)).astype(int).tolist(), np.round(np.linalg.solve(A, [4, 7]), 10).tolist())
[[2, -1], [-5, 3]] [1.0, 1.0]

Test yourself

Which of these is NOT an allowed row operation?

  1. Swap two rows
  2. Multiply a row by 5
  3. Add 2 times row 1 to row 3
  4. Add 1 to every entry of a row

A 2×22 \times 2 matrix has determinant ad−bc=0ad - bc = 0. What follows?

  1. Its inverse is the identity
  2. It has no inverse, and the system AX=BAX = B has no unique solution
  3. The solution is X=0X = 0
  4. Its inverse has all entries 0

Why is np.linalg.solve(A, B) preferred over np.linalg.inv(A) @ B?

  1. inv does not exist in NumPy
  2. solve is faster and more accurate because it eliminates directly
  3. solve gives exact fractions
  4. They give different answers