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
- Choose an equation where a variable is easy to isolate (coefficient 1 or −1), and solve for it.
- Substitute that expression into both other equations. You now have a system.
- Solve the system by substitution or elimination.
- Put the two values into the expression from step 1 to get the third variable.
- Check all three original equations.
A worked example
Equation (1) already gives . Substitute it into (2) and (3), keeping the parentheses:
Solve (B) for and substitute into (A):
Check:
The solution is .
Tip: If no variable has coefficient , 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:
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.
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 unknowns this takes about multiplications, far fewer than the elimination that comes before it (about ).
Substitution with SymPy
SymPy can follow the plan step by step, which is a good way to check hand work:
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 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 , , .
Answer
, , .
Solution
Exercise 2: One variable isolated
Solve , , .
Answer
, , .
Solution
Exercise 3: Isolate, then substitute
Solve , , .
Answer
, , .
Solution
Only the third equation has a variable with coefficient 1 that is easy to isolate: .
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 , , , .
Answer
, , , .
Solution
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 , , , what is the best first step?
- Add the second and third equations
- Substitute into the other two equations
- Solve the second equation for
- Multiply everything by 2
In back substitution on an upper-triangular system, which unknown is found first?
- The first one (top row)
- The middle one
- The last one (bottom row)
- All at the same time