Solving Formulas for a Variable
A literal equation is an equation with several letters, usually a formula such as or . Solving it "for" one letter means isolating that letter using exactly the same moves as for a numeric equation, treating every other letter as if it were a known number. Programmers do this all the time to turn a formula around, so this lesson works through physics, geometry and finance formulas and checks them with SymPy.
Same moves, more letters
Solve for :
Compare it with the numeric version : subtract 10, divide by 4, . The steps are identical; only the result is a formula instead of a number.
Tip: Highlight the target letter. Every other letter is "just a number", and you undo the operations around the target in reverse order, as in Equations with One Unknown.
Watch the conditions
Dividing by a letter is only allowed when that letter is not zero, so the result comes with a condition: needs . In code that condition becomes a guard. If (no acceleration), the speed never changes and "when does it reach ?" has no answer, or every answer if .
More examples
The area of a triangle, , for :
Temperature, , for :
The unknown appears twice. Solve for : gather the terms, then factor out:
Factoring out the target letter is the key move whenever it appears in more than one term. Another example, the formula for a discounted price with tax, , solved for , just divides: .
Squares. Kinetic energy , for :
SymPy does the bookkeeping
solve(equation, letter) treats every other symbol as a constant:
from sympy import symbols, Eq, solve, Rational
v, u, a, t, A, b, h, F, C, E, m = symbols("v u a t A b h F C E m")
print(solve(Eq(v, u + a*t), t))
print(solve(Eq(A, Rational(1, 2)*b*h), h))
print(solve(Eq(F, Rational(9, 5)*C + 32), C))
print(solve(Eq(E, Rational(1, 2)*m*v**2), v))[(-u + v)/a]
[2*A/b]
[5*F/9 - 160/9]
[-sqrt(2)*sqrt(E/m), sqrt(2)*sqrt(E/m)]
SymPy writes the results in its own form: is the same as , and is . It also returns both square roots, because it does not know that is a speed. Declaring the symbols as positive removes the negative one:
from sympy import symbols, Eq, solve, Rational
E, m, v = symbols("E m v", positive=True)
print(solve(Eq(E, Rational(1, 2)*m*v**2), v))[sqrt(2)*sqrt(E)/sqrt(m)]
In software: from formula to function
Games and simulations constantly turn formulas around. To make a projectile reach a height with gravity , solve for . To make an animation take exactly 0.4 s over 300 px, solve for . Once SymPy has the rearranged formula, lambdify turns it into a function:
from sympy import symbols, Eq, solve, lambdify
H, v, g = symbols("H v g", positive=True)
v_needed = solve(Eq(H, v**2 / (2*g)), v)[0]
print(v_needed)
launch_speed = lambdify((H, g), v_needed)
for height in [1.0, 2.5, 5.0]:
print(f"jump {height} m needs {launch_speed(height, 9.81):.2f} m/s")sqrt(2)*sqrt(H)*sqrt(g)
jump 1.0 m needs 4.43 m/s
jump 2.5 m needs 7.00 m/s
jump 5.0 m needs 9.90 m/s
This is how a platformer's designer can set "jump height" in the editor while the code applies a launch speed. The same rearranging gives the inverse lerp in Lerp.
Exercises
Exercise 1: Perimeter
Solve for .
Answer
, which is also .
Solution
Exercise 2: Simple interest
Simple interest gives . Solve for .
Answer
, with and .
Solution
Check with , , : , and ✓.
Exercise 3: The target appears twice
Solve for .
Answer
, with .
Solution
from sympy import symbols, Eq, solve
x, y = symbols("x y")
print(solve(Eq(y, (x + 2)/(x - 3)), x))[(3*y + 2)/(y - 1)]
Exercise 4: Parallel resistors
Two resistors in parallel give . Solve for and write a Python function. What must be added to a resistor to get ?
Answer
; for , , .
Solution
def r2_needed(R, R1):
return R * R1 / (R1 - R)
print(r2_needed(20, 60))
print(1 / (1/60 + 1/30)) # check30.0
20.0
Test yourself
Solve for .
Solving for gives:
SymPy returns two solutions when solving for . How do you keep only the positive one?
- Use expand
- Declare the symbols with positive=True
- Use lambdify
- It is impossible