TExcelTAD
Home › Algebra › Three Unknowns by Elimination

Three Unknowns by Elimination

With three unknowns you need three equations, and elimination works in two rounds: first use the equations in pairs to remove one variable, which leaves a 2×22 \times 2 system; then solve that system as before. This lesson walks through the steps and shows how a program does the same bookkeeping.

The plan

A linear equation in xx, yy and zz, such as x+y+z=4x + y + z = 4, describes a flat plane in 3D space. A solution of a 3×33 \times 3 system is a point on all three planes. Typically three planes meet in exactly one point, the way two walls and a floor meet in a corner.

The strategy:

  1. Pick a variable to eliminate (the one that cancels most easily).
  2. Combine equations (1) and (2) to eliminate it, giving equation (A).
  3. Combine (1) and (3), or (2) and (3), to eliminate the same variable, giving equation (B).
  4. Solve the 2×22 \times 2 system (A), (B) with elimination.
  5. Substitute both values into an original equation to find the third variable.
  6. Check in all three original equations.

A worked example

(1)x+y+z=4(2)2x−y+z=8(3)x+2y−z=−3\begin{aligned} (1)&\quad & x + y + z &= 4 \\ (2)&\quad & 2x - y + z &= 8 \\ (3)&\quad & x + 2y - z &= -3 \end{aligned}

zz has coefficients 1, 1 and −1, so adding (3) to either of the others removes it:

(1)+(3):2x+3y=1(A)(2)+(3):3x+y=5(B)\begin{aligned} (1) + (3): &\quad 2x + 3y = 1 && \text{(A)} \\ (2) + (3): &\quad 3x + y = 5 && \text{(B)} \end{aligned}

Now a 2×22 \times 2 system. Multiply (B) by 3 and subtract (A):

3⋅(B):9x+3y=15−(A):2x+3y=17x=14→x=2(B):3⋅2+y=5→y=−1(1):2−1+z=4→z=3\begin{aligned} 3 \cdot \text{(B)}: &\quad 9x + 3y = 15 \\ -\text{(A)}: &\quad 2x + 3y = 1 \\ &\quad 7x = 14 \to x = 2 \\ \text{(B)}: &\quad 3 \cdot 2 + y = 5 \to y = -1 \\ (1): &\quad 2 - 1 + z = 4 \to z = 3 \end{aligned}

Check every equation:

(1)2+(−1)+3=4 ✓(2)4−(−1)+3=8 ✓(3)2+2⋅(−1)−3=−3 ✓\begin{aligned} (1)&\quad & 2 + (-1) + 3 &= 4\ \checkmark \\ (2)&\quad & 4 - (-1) + 3 &= 8\ \checkmark \\ (3)&\quad & 2 + 2 \cdot (-1) - 3 &= -3\ \checkmark \end{aligned}

The solution is the point (x,y,z)=(2,−1,3)(x, y, z) = (2, -1, 3).

Tip: Number every equation you create and write down which ones you combined, as in "(1) + (3)". A 3×33 \times 3 system has enough steps that unlabelled work quickly becomes impossible to check.

A common mistake

Steps 2 and 3 must eliminate the same variable. If (A) loses zz but (B) loses xx, you have two equations in different pairs of unknowns ((x,y)(x, y) and (y,z)(y, z)) and the 2×22 \times 2 step does not work. Also, (A) and (B) must come from different pairs of equations; combining (1) and (3) twice only gives a multiple of the same equation.

When there is no single point

Three planes do not always meet in one point. Elimination shows it the same way as in two unknowns:

Result during elimination Geometry Solutions
unique values for xx, yy, zz three planes through one point one
a false statement such as 0=20 = 2 no common point (parallel planes, or a "tent" of three planes) none
a true statement 0=00 = 0 that leaves a variable free planes share a whole line (or are the same plane) infinitely many

Elimination in code

Computers do not choose clever combinations; they follow a fixed order. Use row 1 to clear xx from the rows below it, then row 2 to clear yy below it. What remains is a triangular system, solved from the bottom up:

python · try it yourself
from fractions import Fraction as F

rows = [[1, 1, 1, 4],          # each row: a, b, c | d  for  ax + by + cz = d
        [2, -1, 1, 8],
        [1, 2, -1, -3]]
rows = [[F(v) for v in r] for r in rows]

for col in range(3):                       # forward elimination
    pivot = rows[col]
    for r in rows[col + 1:]:
        factor = r[col] / pivot[col]
        for k in range(4):
            r[k] -= factor * pivot[k]
for r in rows:
    print([str(v) for v in r])

x = [F(0)] * 3                              # back substitution
for i in (2, 1, 0):
    s = sum(rows[i][j] * x[j] for j in range(i + 1, 3))
    x[i] = (rows[i][3] - s) / rows[i][i]
print("x, y, z =", [str(v) for v in x])
['1', '1', '1', '4']
['0', '-3', '-1', '0']
['0', '0', '-7/3', '-7']
x, y, z = ['2', '-1', '3']

The bottom row says −73z=−7-\frac{7}{3}z = -7, so z=3z = 3; the middle row then gives −3y−3=0-3y - 3 = 0, so y=−1y = -1; the top row gives x=2x = 2. Same answer, different route. This orderly version is called Gaussian elimination. It is how libraries such as NumPy solve systems with thousands of unknowns, with one extra trick: they swap rows so the pivot is never zero or tiny. The full algorithm is in Gaussian Elimination; the same steps written on a grid of numbers are The Matrix Method.

In software

Three-unknown systems appear when three constraints must hold at once:

  • Colour mixing: which amounts of three pigments (or three LED channels) produce a target colour with given red, green and blue values?
  • Budgets with three resources: CPU, memory and storage across three server types.
  • Fitting a parabola: the curve y=ax2+bx+cy = ax^2 + bx + c through three measured points gives three linear equations in aa, bb, cc. Exercise 4 below does exactly this.

Exercises

Exercise 1: Warm-up

Solve x+y+z=6x + y + z = 6, x−y+z=2x - y + z = 2, x+y−z=0x + y - z = 0.

Answer

x=1x = 1, y=2y = 2, z=3z = 3.

Solution
(1)−(2):2y=4→y=2(1)−(3):2z=6→z=3(1):x+2+3=6→x=1check (2):1−2+3=2 ✓(3) ⁣: 1+2−3=0 ✓\begin{aligned} (1) - (2): &\quad 2y = 4 \to y = 2 \\ (1) - (3): &\quad 2z = 6 \to z = 3 \\ (1): &\quad x + 2 + 3 = 6 \to x = 1 \\ \text{check } (2): &\quad 1 - 2 + 3 = 2\ \checkmark \quad (3)\colon\ 1 + 2 - 3 = 0\ \checkmark \end{aligned}

Exercise 2: Full elimination

Solve 2x+y−z=32x + y - z = 3, x−y+2z=5x - y + 2z = 5, 3x+2y+z=103x + 2y + z = 10.

Answer

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

Solution

Eliminate yy, which has coefficients 1, −1 and 2:

(1)+(2):3x+z=8(A)2⋅(2)+(3):2x−2y+4z=10+ 3x+2y+z=10= 5x+5z=20→x+z=4(B)(A)−(B):2x=4→x=2(B):z=4−2=2(1):4+y−2=3→y=1\begin{aligned} (1) + (2): &\quad 3x + z = 8 && \text{(A)} \\ 2 \cdot (2) + (3): &\quad 2x - 2y + 4z = 10 \\ &\quad {+}\ 3x + 2y + z = 10 \\ &\quad {=}\ 5x + 5z = 20 \to x + z = 4 && \text{(B)} \\ \text{(A)} - \text{(B)}: &\quad 2x = 4 \to x = 2 \\ \text{(B)}: &\quad z = 4 - 2 = 2 \\ (1): &\quad 4 + y - 2 = 3 \to y = 1 \end{aligned}
python · try it yourself
from sympy import symbols, solve
x, y, z = symbols("x y z")
print(solve([2*x + y - z - 3, x - y + 2*z - 5, 3*x + 2*y + z - 10], [x, y, z]))
{x: 2, y: 1, z: 2}

Exercise 3: Code it

Solve with numpy.linalg.solve: x+2y+3z=14x + 2y + 3z = 14, 2x+y+z=72x + y + z = 7, 3x+y+2z=113x + y + 2z = 11.

Answer

x=1x = 1, y=2y = 2, z=3z = 3.

Solution
python · try it yourself
import numpy as np
A = np.array([[1, 2, 3], [2, 1, 1], [3, 1, 2]], dtype=float)
b = np.array([14, 7, 11], dtype=float)
print(np.round(np.linalg.solve(A, b), 6))
[1. 2. 3.]

Check: 1+4+9=141 + 4 + 9 = 14 ✓, 2+2+3=72 + 2 + 3 = 7 ✓, 3+2+6=113 + 2 + 6 = 11 ✓.

Exercise 4: A parabola through three points

Find aa, bb and cc so that y=ax2+bx+cy = ax^2 + bx + c passes through (1,2)(1, 2), (2,3)(2, 3) and (3,6)(3, 6).

Answer

a=1a = 1, b=−2b = -2, c=3c = 3, so y=x2−2x+3y = x^2 - 2x + 3.

Solution

Each point gives one equation in aa, bb, cc:

(1,2):a+b+c=2(2,3):4a+2b+c=3(3,6):9a+3b+c=6(2)−(1):3a+b=1(A)(3)−(2):5a+b=3(B)(B)−(A):2a=2→a=1(A):3+b=1→b=−2(1):1−2+c=2→c=3\begin{aligned} (1, 2): &\quad a + b + c = 2 \\ (2, 3): &\quad 4a + 2b + c = 3 \\ (3, 6): &\quad 9a + 3b + c = 6 \\ (2) - (1): &\quad 3a + b = 1 && \text{(A)} \\ (3) - (2): &\quad 5a + b = 3 && \text{(B)} \\ \text{(B)} - \text{(A)}: &\quad 2a = 2 \to a = 1 \\ \text{(A)}: &\quad 3 + b = 1 \to b = -2 \\ (1): &\quad 1 - 2 + c = 2 \to c = 3 \end{aligned}

Check: x=3x = 3 gives 9−6+3=69 - 6 + 3 = 6 ✓.

Test yourself

After the first round of eliminating zz from a 3×33 \times 3 system, what do you have?

  1. One equation in zz
  2. Two equations in xx and yy
  3. Three equations in xx, yy and zz
  4. The final answer

What does a single linear equation in xx, yy and zz describe geometrically?

  1. A point
  2. A line
  3. A plane
  4. A sphere

Gaussian elimination turns a system into which shape before back substitution?

  1. Triangular: each row has fewer unknowns than the one above
  2. Diagonal only
  3. Circular
  4. A single equation