Equations with One Unknown
A linear equation in one unknown, such as , has at most one solution, and a fixed routine always finds it: simplify each side, gather the unknown on one side, then isolate it. This lesson turns that routine into a checklist, treats the special cases (no solution and every number), and writes a tiny solver for equations of the form .
The routine
| Step | What to do | Example: |
|---|---|---|
| 1 | Remove parentheses on both sides | |
| 2 | Combine like terms on each side | |
| 3 | Move the unknown terms to one side | (subtract ) |
| 4 | Move the numbers to the other side | (add 11) |
| 5 | Divide by the coefficient | |
| 6 | Check in the original equation | and ✓ |
Every step is an equivalent transformation: it does the same thing to both sides, so the new equation has exactly the same solutions as the old one. This is why the method is safe. (Later lessons meet steps that are not equivalent, such as squaring both sides, and those need extra care.)
Tip: Move the terms toward the side with the bigger coefficient. In , subtracting leaves a positive on the right and avoids dividing by a negative.
Negative coefficients
If the unknown ends with a negative coefficient, divide by that negative number:
Three possible outcomes
After gathering terms, every linear equation looks like :
| Case | After simplifying | Solutions | Example |
|---|---|---|---|
| exactly one | |||
| , | (false) | none | |
| , | (true) | every number |
SymPy distinguishes them: solve returns a single value, an empty list, or (for an identity) also an empty list, which is ambiguous. solveset makes the difference explicit:
from sympy import symbols, Eq, solve, solveset, S
x = symbols("x")
eq1 = Eq(3*(2*x - 5) + 4, 2*(x + 7) - 1)
eq2 = Eq(2*x + 3, 2*x + 8)
eq3 = Eq(2*(x + 4), 2*x + 8)
print(solve(eq1, x), solveset(eq1, x, S.Reals))
print(solve(eq2, x), solveset(eq2, x, S.Reals))
print(solve(eq3, x), solveset(eq3, x, S.Reals))[6] {6}
[] EmptySet
[] Reals
A solver for
Gathering terms turns into . That single line is a complete solver, as long as it handles the cases. Using Fraction keeps answers exact:
from fractions import Fraction as F
def solve_linear(a, b, c, d):
"""Solve a*x + b = c*x + d."""
k, m = F(a) - F(c), F(d) - F(b)
if k != 0:
return m / k
return "every number" if m == 0 else "no solution"
print(solve_linear(6, -11, 2, 13)) # 6x - 11 = 2x + 13
print(solve_linear(-3, 5, 0, 17)) # -3x + 5 = 17
print(solve_linear(4, 1, 1, 3)) # 4x + 1 = x + 3
print(solve_linear(2, 3, 2, 8))
print(solve_linear(2, 8, 2, 8))6
-4
2/3
no solution
every number
Decimals
Equations with decimal coefficients can be cleared by multiplying both sides by a power of 10:
In software
Linear equations hide inside many routine calculations: the price before tax from a total, the time an animation reaches a frame, the speed needed to arrive on time. A solver like solve_linear is the simplest example of solving equations in code; the important engineering detail is the branch, the algebraic version of guarding against division by zero.
Exercises
Exercise 1: The full routine
Solve .
Answer
Solution
from sympy import symbols, Eq, solve
x = symbols("x")
print(solve(Eq(4*(x - 3) - 2, 5 - 3*(x + 1)), x))[16/7]
Exercise 2: Decimals
Solve .
Answer
Solution
Exercise 3: Which case?
Classify each without fully solving: (a) , (b) , (c) .
Answer
(a) every number, (b) no solution, (c) exactly one, .
Solution
(a) Both sides are , an identity. (b) leads to , false. (c) , so . A solution of zero is still a solution.
Exercise 4: Price before tax
A total of €95.20 includes 19% sales tax. Set up the equation, solve it by hand, and check it with solve_linear (, , , , using strings so Fraction stays exact).
Answer
, .
Solution
from fractions import Fraction as F
def solve_linear(a, b, c, d):
k, m = F(a) - F(c), F(d) - F(b)
if k != 0:
return m / k
return "every number" if m == 0 else "no solution"
print(solve_linear("1.19", 0, 0, "95.20"))80
Test yourself
Solve .
After simplifying, an equation becomes . What does that mean?
- Every number is a solution
- There is no solution
Why should you check the answer in the original equation rather than in a later line?
- It is shorter
- A mistake in an early step would also be in later lines and go unnoticed
- SymPy requires it
- Later lines have no