TExcelTAD
Home › Algebra › Three Unknowns by Substitution

Three Unknowns by Substitution

Substitution also scales to three unknowns: solve one equation for a variable, substitute it into the other two, and you are left with two equations in two unknowns. It shines when one equation already isolates a variable, and its last step, back substitution, is exactly how computers finish solving any system.

The plan

  1. Choose an equation where a variable is easy to isolate (coefficient 1 or −1), and solve for it.
  2. Substitute that expression into both other equations. You now have a 2×22 \times 2 system.
  3. Solve the 2×22 \times 2 system by substitution or elimination.
  4. Put the two values into the expression from step 1 to get the third variable.
  5. Check all three original equations.

A worked example

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

Equation (1) already gives zz. Substitute it into (2) and (3), keeping the parentheses:

(2):2x+y+(x+y−5)=2→3x+2y=7(A)(3):x−y+3(x+y−5)=−7→x−y+3x+3y−15=−7→4x+2y=8→2x+y=4(B)\begin{aligned} (2): &\quad 2x + y + (x + y - 5) = 2 && \to 3x + 2y = 7 && \text{(A)} \\ (3): &\quad x - y + 3(x + y - 5) = -7 && \to x - y + 3x + 3y - 15 = -7 \\ & && \to 4x + 2y = 8 \to 2x + y = 4 && \text{(B)} \end{aligned}

Solve (B) for yy and substitute into (A):

y=4−2x3x+2(4−2x)=7→3x+8−4x=7→−x=−1→x=1y=4−2=2z=1+2−5=−2\begin{aligned} &y = 4 - 2x \\ &3x + 2(4 - 2x) = 7 \to 3x + 8 - 4x = 7 \to -x = -1 \to x = 1 \\ &y = 4 - 2 = 2 \\ &z = 1 + 2 - 5 = -2 \end{aligned}

Check:

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

The solution is (1,2,−2)(1, 2, -2).

Tip: If no variable has coefficient ±1\pm 1, substitution forces fractions on you. Then elimination or the matrix method is usually less work.

Triangular systems: substitution in its best form

Some systems arrive with each equation one unknown shorter than the one above. Such a system is called triangular, and substitution solves it with no effort at all, working from the bottom up:

x+y+z=6y+2z=83z=9→z=3y+6=8→y=2x+2+3=6→x=1\begin{aligned} x + y + z &= 6 \\ y + 2z &= 8 \\ 3z &= 9 && \to z = 3 \\ y + 6 &= 8 && \to y = 2 \\ x + 2 + 3 &= 6 && \to x = 1 \end{aligned}

This is back substitution. Elimination in a computer works by turning any system into a triangular one (forward elimination) and then running back substitution. So the two methods of this chapter are really two halves of one algorithm.

python · try it yourself
from fractions import Fraction as F

def back_substitute(U, d):
    """Solve an upper-triangular system U·x = d, bottom row first."""
    n = len(d)
    x = [F(0)] * n
    for i in reversed(range(n)):
        known = sum(U[i][j] * x[j] for j in range(i + 1, n))
        x[i] = (F(d[i]) - known) / U[i][i]
        print(f"row {i}: x{i} = ({d[i]} - {known}) / {U[i][i]} = {x[i]}")
    return x

U = [[1, 1, 1],
     [0, 1, 2],
     [0, 0, 3]]
back_substitute(U, [6, 8, 9])
row 2: x2 = (9 - 0) / 3 = 3
row 1: x1 = (8 - 6) / 1 = 2
row 0: x0 = (6 - 5) / 1 = 1

Each row needs only the values already found below it. For nn unknowns this takes about n2/2n^2/2 multiplications, far fewer than the elimination that comes before it (about n3/3n^3/3).

Substitution with SymPy

SymPy can follow the plan step by step, which is a good way to check hand work:

python · try it yourself
from sympy import symbols, Eq, solve

x, y, z = symbols("x y z")
z_expr = x + y - 5                                 # from (1)
A = Eq(2*x + y + z_expr, 2)                        # (2) with z replaced
B = Eq(x - y + 3*z_expr, -7)                       # (3) with z replaced
print("A:", A.simplify(), "  B:", B.simplify())
sol = solve([A, B], [x, y])
print("x, y:", sol, "  z:", z_expr.subs(sol))
A: Eq(3*x + 2*y, 7)   B: Eq(2*x + y, 4)
x, y: {x: 1, y: 2}   z: -2

In software

  • Dependency chains. Spreadsheet cells and build systems compute values in an order where each one depends only on values already known. That is back substitution on a triangular system of "equations" (formulas).
  • Constraint solvers in UI layout. A layout rule like "the sidebar is 250 px, the content fills the rest of 1200 px, the gutter is 120\frac{1}{20} of the content" can be solved by substituting each known width into the next rule.
  • LU decomposition. Numerical libraries split a matrix into a lower and an upper triangular part once, then solve each new right-hand side by two cheap substitution passes. See Gaussian Elimination and the numerical linear algebra lesson.

Exercises

Exercise 1: Back substitution

Solve the triangular system 2x−y+z=32x - y + z = 3, y−z=−1y - z = -1, 4z=84z = 8.

Answer

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

Solution
4z=8→z=2y−2=−1→y=12x−1+2=3→2x=2→x=1\begin{aligned} 4z &= 8 && \to z = 2 \\ y - 2 &= -1 && \to y = 1 \\ 2x - 1 + 2 &= 3 && \to 2x = 2 \to x = 1 \end{aligned}

Exercise 2: One variable isolated

Solve x=2zx = 2z, x+y+z=10x + y + z = 10, y−z=2y - z = 2.

Answer

x=4x = 4, y=4y = 4, z=2z = 2.

Solution
substitute x=2z:2z+y+z=10→y+3z=10(A)y−z=2(B)(A)−(B):4z=8→z=2y=2+2=4, x=2⋅2=4check:4+4+2=10 ✓4−2=2 ✓\begin{aligned} \text{substitute } x = 2z: &\quad 2z + y + z = 10 \to y + 3z = 10 && \text{(A)} \\ &\quad y - z = 2 && \text{(B)} \\ \text{(A)} - \text{(B)}: &\quad 4z = 8 \to z = 2 \\ &\quad y = 2 + 2 = 4,\ x = 2 \cdot 2 = 4 \\ \text{check:} &\quad 4 + 4 + 2 = 10\ \checkmark \quad 4 - 2 = 2\ \checkmark \end{aligned}

Exercise 3: Isolate, then substitute

Solve x+2y−z=1x + 2y - z = 1, 2x−y+3z=122x - y + 3z = 12, 3x+y+z=103x + y + z = 10.

Answer

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

Solution

Only the third equation has a variable with coefficient 1 that is easy to isolate: z=10−3x−yz = 10 - 3x - y.

(1):x+2y−(10−3x−y)=1→4x+3y=11(A)(2):2x−y+3(10−3x−y)=12→−7x−4y=−18→7x+4y=18(B)4⋅(A):16x+12y=443⋅(B):21x+12y=54subtract:−5x=−10→x=2(A):8+3y=11→y=1z=10−6−1=3check (2):4−1+9=12 ✓\begin{aligned} (1): &\quad x + 2y - (10 - 3x - y) = 1 && \to 4x + 3y = 11 && \text{(A)} \\ (2): &\quad 2x - y + 3(10 - 3x - y) = 12 && \to -7x - 4y = -18 \\ & && \to 7x + 4y = 18 && \text{(B)} \\ 4 \cdot \text{(A)}: &\quad 16x + 12y = 44 \\ 3 \cdot \text{(B)}: &\quad 21x + 12y = 54 \\ \text{subtract:} &\quad -5x = -10 \to x = 2 \\ \text{(A)}: &\quad 8 + 3y = 11 \to y = 1 \\ &\quad z = 10 - 6 - 1 = 3 \\ \text{check } (2): &\quad 4 - 1 + 9 = 12\ \checkmark \end{aligned}
python · try it yourself
from sympy import symbols, solve
x, y, z = symbols("x y z")
print(solve([x + 2*y - z - 1, 2*x - y + 3*z - 12, 3*x + y + z - 10], [x, y, z]))
{x: 2, y: 1, z: 3}

Exercise 4: Write a solver

Use back_substitute from the lesson (or write your own) to solve x+y+z+w=10x + y + z + w = 10, y+z+w=9y + z + w = 9, z+w=7z + w = 7, w=4w = 4.

Answer

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

Solution
python · try it yourself
from fractions import Fraction as F

def back_substitute(U, d):
    n = len(d)
    x = [F(0)] * n
    for i in reversed(range(n)):
        known = sum(U[i][j] * x[j] for j in range(i + 1, n))
        x[i] = (F(d[i]) - known) / U[i][i]
    return x

U = [[1, 1, 1, 1], [0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1]]
print([int(v) for v in back_substitute(U, [10, 9, 7, 4])])
[1, 2, 3, 4]

Test yourself

In the system z=2x−yz = 2x - y, x+y+z=9x + y + z = 9, x−y+2z=4x - y + 2z = 4, what is the best first step?

  1. Add the second and third equations
  2. Substitute z=2x−yz = 2x - y into the other two equations
  3. Solve the second equation for yy
  4. Multiply everything by 2

In back substitution on an upper-triangular system, which unknown is found first?

  1. The first one (top row)
  2. The middle one
  3. The last one (bottom row)
  4. All at the same time