TExcelTAD
Home › Algebra › Solving Formulas for a Variable

Solving Formulas for a Variable

A literal equation is an equation with several letters, usually a formula such as v=u+atv = u + at or A=12bhA = \frac{1}{2}bh. 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 v=u+atv = u + at for tt:

v=u+atv−u=atsubtract u from both sidesv−ua=tdivide both sides by a(a≠0)t=v−ua\begin{aligned} v &= u + at \\ v - u &= at && \text{subtract } u \text{ from both sides} \\ \frac{v - u}{a} &= t && \text{divide both sides by } a \quad (a \ne 0) \\ t &= \frac{v - u}{a} \end{aligned}

Compare it with the numeric version 30=10+4t30 = 10 + 4t: subtract 10, divide by 4, t=5t = 5. 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: t=v−uat = \frac{v - u}{a} needs a≠0a \ne 0. In code that condition becomes a guard. If a=0a = 0 (no acceleration), the speed never changes and "when does it reach vv?" has no answer, or every answer if v=uv = u.

More examples

The area of a triangle, A=12bhA = \frac{1}{2}bh, for hh:

2A=bhmultiply by 2h=2Abdivide by b(b≠0)\begin{aligned} 2A &= bh && \text{multiply by 2} \\ h &= \frac{2A}{b} && \text{divide by } b \quad (b \ne 0) \end{aligned}

Temperature, F=9C5+32F = \frac{9C}{5} + 32, for CC:

F−32=9C55(F−32)=9CC=5(F−32)9\begin{aligned} F - 32 &= \frac{9C}{5} \\ 5(F - 32) &= 9C \\ C &= \frac{5(F - 32)}{9} \end{aligned}

The unknown appears twice. Solve ax+b=cx+dax + b = cx + d for xx: gather the xx terms, then factor xx out:

ax−cx=d−bx(a−c)=d−bx=d−ba−c(a≠c)\begin{aligned} ax - cx &= d - b \\ x(a - c) &= d - b \\ x &= \frac{d - b}{a - c} && (a \ne c) \end{aligned}

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, T=p(1−d)(1+t)T = p(1 - d)(1 + t), solved for pp, just divides: p=T(1−d)(1+t)p = \frac{T}{(1 - d)(1 + t)}.

Squares. Kinetic energy E=12mv2E = \frac{1}{2}mv^2, for vv:

2E=mv2v2=2Emv=2Emtaking the positive root, since speed≥0\begin{aligned} 2E &= mv^2 \\ v^2 &= \frac{2E}{m} \\ v &= \sqrt{\frac{2E}{m}} && \text{taking the positive root, since speed} \ge 0 \end{aligned}

SymPy does the bookkeeping

solve(equation, letter) treats every other symbol as a constant:

python · try it yourself
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: 5F9−1609\frac{5F}{9} - \frac{160}{9} is the same as 5(F−32)9\frac{5(F - 32)}{9}, and 2⋅E/m\sqrt{2} \cdot \sqrt{E/m} is 2E/m\sqrt{2E/m}. It also returns both square roots, because it does not know that vv is a speed. Declaring the symbols as positive removes the negative one:

python · try it yourself
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 HH with gravity gg, solve H=v22gH = \frac{v^2}{2g} for vv. To make an animation take exactly 0.4 s over 300 px, solve d=s⋅td = s \cdot t for ss. Once SymPy has the rearranged formula, lambdify turns it into a function:

python · try it yourself
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 t=v−ab−at = \frac{v - a}{b - a} in Lerp.

Exercises

Exercise 1: Perimeter

Solve P=2l+2wP = 2l + 2w for ww.

Answer

w=P−2l2w = \frac{P - 2l}{2}, which is also P2−l\frac{P}{2} - l.

Solution
P−2l=2wsubtract 2lw=P−2l2divide by 2\begin{aligned} P - 2l &= 2w && \text{subtract } 2l \\ w &= \frac{P - 2l}{2} && \text{divide by 2} \end{aligned}

Exercise 2: Simple interest

Simple interest gives A=P(1+rt)A = P(1 + rt). Solve for rr.

Answer

r=A−PPtr = \frac{A - P}{Pt}, with P≠0P \ne 0 and t≠0t \ne 0.

Solution
A=P+PrtdistributeA−P=Prtr=A−PPt\begin{aligned} A &= P + Prt && \text{distribute} \\ A - P &= Prt \\ r &= \frac{A - P}{Pt} \end{aligned}

Check with P=1000P = 1000, r=0.05r = 0.05, t=3t = 3: A=1150A = 1150, and 1150−10001000⋅3=0.05\frac{1150 - 1000}{1000 \cdot 3} = 0.05 ✓.

Exercise 3: The target appears twice

Solve y=x+2x−3y = \frac{x + 2}{x - 3} for xx.

Answer

x=3y+2y−1x = \frac{3y + 2}{y - 1}, with y≠1y \ne 1.

Solution
y(x−3)=x+2multiply by (x−3)xy−3y=x+2xy−x=3y+2gather the x termsx(y−1)=3y+2factor out xx=3y+2y−1\begin{aligned} y(x - 3) &= x + 2 && \text{multiply by } (x - 3) \\ xy - 3y &= x + 2 \\ xy - x &= 3y + 2 && \text{gather the } x \text{ terms} \\ x(y - 1) &= 3y + 2 && \text{factor out } x \\ x &= \frac{3y + 2}{y - 1} \end{aligned}
python · try it yourself
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 1R=1R1+1R2\frac{1}{R} = \frac{1}{R_1} + \frac{1}{R_2}. Solve for R2R_2 and write a Python function. What R2R_2 must be added to a 60 Ω60\ \Omega resistor to get 20 Ω20\ \Omega?

Answer

R2=R⋅R1R1−RR_2 = \frac{R \cdot R_1}{R_1 - R}; for R=20R = 20, R1=60R_1 = 60, R2=30 ΩR_2 = 30\ \Omega.

Solution
1R2=1R−1R1=R1−RR⋅R1R2=R⋅R1R1−R(R1≠R)\begin{aligned} \frac{1}{R_2} &= \frac{1}{R} - \frac{1}{R_1} = \frac{R_1 - R}{R \cdot R_1} \\ R_2 &= \frac{R \cdot R_1}{R_1 - R} && (R_1 \ne R) \end{aligned}
python · try it yourself
def r2_needed(R, R1):
    return R * R1 / (R1 - R)

print(r2_needed(20, 60))
print(1 / (1/60 + 1/30))        # check
30.0
20.0

Test yourself

Solve d=rtd = rt for rr.

  1. r=dtr = dt
  2. r=d/tr = d/t
  3. r=t/dr = t/d
  4. r=d−tr = d - t

Solving ax−b=cxax - b = cx for xx gives:

  1. x=b/(a−c)x = b/(a - c)
  2. x=(b+c)/ax = (b + c)/a
  3. x=b/(a+c)x = b/(a + c)
  4. x=(a−c)/bx = (a - c)/b

SymPy returns two solutions when solving E=12mv2E = \frac{1}{2}mv^2 for vv. How do you keep only the positive one?

  1. Use expand
  2. Declare the symbols with positive=True
  3. Use lambdify
  4. It is impossible