The Substitution Method
The substitution method solves one equation for one unknown and plugs that expression into the other equation. The second equation then has a single unknown left. Substitution is the natural choice when a variable is already isolated, and it also works for systems that are not linear, such as a line meeting a parabola.
The idea
Solve and .
The first equation already says what is, so replace in the second equation by :
The solution is . The parentheses in the first line matter: you replace the whole by the whole expression.
The recipe
- Pick an equation and solve it for one variable, preferably one with coefficient 1 or −1 (no fractions appear).
- Substitute that expression into the other equation.
- Solve the resulting one-variable equation.
- Put the value back into the expression from step 1 to get the second variable.
- Check both original equations.
Example: and . The in the second equation has coefficient 1:
| Situation | Better method |
|---|---|
| a variable is already isolated () or has coefficient | substitution |
| matching or opposite coefficients, or all coefficients large | elimination |
| one equation is not linear (, , …) | substitution |
Both methods always give the same answer; the choice is only about how much arithmetic you do.
Substitution with SymPy
SymPy can carry out each step exactly as you would, which makes it a good way to check your work:
from sympy import symbols, Eq, solve
x, y = symbols("x y")
eq1 = Eq(5*x - 2*y, 4)
eq2 = Eq(x + 3*y, 11)
x_expr = solve(eq2, x)[0] # step 1: x in terms of y
print("x =", x_expr)
step2 = eq1.subs(x, x_expr) # step 2: substitute into eq1
print("substituted:", step2)
y_val = solve(step2, y)[0] # step 3
x_val = x_expr.subs(y, y_val) # step 4
print("solution:", x_val, y_val)x = 11 - 3*y
substituted: Eq(55 - 17*y, 4)
solution: 2 3
SymPy prints * for multiplication and has already simplified to .
When the system is not linear
Elimination needs matching terms, so it rarely helps when one equation is curved. Substitution still works. Where does the line meet the parabola ?
Two solutions, which a linear system can never have. The quadratic step uses factoring; in general you may need the quadratic formula.
from sympy import symbols, solve
x, y = symbols("x y")
print(solve([y - x**2, y - (x + 2)], [x, y]))[(-1, 1), (2, 4)]
In software: mixing problems
A chemistry lab (or a paint mixer, or a coffee roaster) combines two stocks to hit a target. Mixing problems always give one equation for the amount and one for the ingredient.
How many litres of a 20 % solution and a 50 % solution make 12 litres of a 30 % solution?
Exactly the same system appears when you build a machine-learning data set from two sources: source A has 20 % positive labels, source B has 50 %, and you want 12 000 rows with 30 % positives. Take 8 000 rows from A and 4 000 from B. A short function solves every case:
from fractions import Fraction as F
def mix(p1, p2, target, total):
"""Amounts of stock 1 and 2 (concentrations p1, p2) giving `total` at `target`."""
# x + y = total, p1·x + p2·y = target·total; y = total − x
x = (target * total - p2 * total) / (p1 - p2)
return x, total - x
print(*mix(F(20, 100), F(50, 100), F(30, 100), 12))
print(*mix(F(20, 100), F(50, 100), F(30, 100), 12000))
print(*mix(F(20, 100), F(50, 100), F(60, 100), 12)) # impossible target8 4
8000 4000
-4 16
The formula comes straight from the substitution: gives . If the target lies outside the range of the two stocks (say 60 % from 20 % and 50 %), one amount comes out negative: no real mixture exists.
Exercises
Exercise 1: Already isolated
Solve and .
Answer
, .
Solution
Exercise 2: Isolate first
Solve and .
Answer
, .
Solution
Exercise 3: A coffee blend
A roaster mixes beans costing €18 per kg with beans costing €30 per kg to make 40 kg of a blend worth €21 per kg. How many kg of each?
Answer
30 kg of the €18 beans and 10 kg of the €30 beans.
Solution
Exercise 4: Line meets parabola
Find all intersection points of and . Check them with SymPy.
Answer
and .
Solution
from sympy import symbols, solve
x, y = symbols("x y")
print(solve([y - (x**2 - 3), y - 2*x], [x, y]))[(-1, -2), (3, 6)]
Test yourself
In the system and , what equation do you get after substituting?
Which variable is the easiest to isolate in , ?
- in the second equation
- in the second equation
- in the first equation
- in the first equation
Why is substitution preferred for a line and a parabola?
- Elimination cannot cancel an term against a linear equation, but substitution turns the system into one quadratic
- Substitution is always faster
- Parabolas only have one point
- Elimination gives wrong answers for curves