TExcelTAD
Home › Linear Algebra & Matrix Theory › Application: Balancing Chemical Reactions

Application: Balancing Chemical Reactions

A chemical equation is balanced when every element appears the same number of times on both sides. Finding the coefficients is a homogeneous linear system: one equation per element, one unknown per molecule. Row reduction turns balancing, which can be a frustrating guessing game, into a mechanical computation.

Setting up the system

Take the burning of propane (C3H8\mathrm{C_3H_8}) in oxygen, which produces carbon dioxide and water:

x1 C3H8+x2 O2→x3 CO2+x4 H2Ox_1\,\mathrm{C_3H_8} + x_2\,\mathrm{O_2} \to x_3\,\mathrm{CO_2} + x_4\,\mathrm{H_2O}

Count each element, putting the reactants on the left with a plus sign and the products with a minus sign:

carbon: 3x1−x3=0hydrogen: 8x1−2x4=0oxygen: 2x2−2x3−x4=0\begin{aligned} \text{carbon: } & 3x_1 - x_3 = 0 \\ \text{hydrogen: } & 8x_1 - 2x_4 = 0 \\ \text{oxygen: } & 2x_2 - 2x_3 - x_4 = 0 \end{aligned}

Three equations, four unknowns, all right-hand sides zero. A homogeneous system with more unknowns than equations always has non-trivial solutions (Homogeneous Systems), and that is exactly what we want.

C3H8O2CO2H2OC30−10H800−2O02−2−1→RREF[100−14010−54001−34]\begin{array}{c|rrrr} & \mathrm{C_3H_8} & \mathrm{O_2} & \mathrm{CO_2} & \mathrm{H_2O} \\ \hline \mathrm{C} & 3 & 0 & -1 & 0 \\ \mathrm{H} & 8 & 0 & 0 & -2 \\ \mathrm{O} & 0 & 2 & -2 & -1 \end{array} \qquad \xrightarrow{\text{RREF}} \qquad \begin{bmatrix} 1 & 0 & 0 & -\frac{1}{4} \\ 0 & 1 & 0 & -\frac{5}{4} \\ 0 & 0 & 1 & -\frac{3}{4} \end{bmatrix}

x4=tx_4 = t is free, and x1=t4x_1 = \frac{t}{4}, x2=5t4x_2 = \frac{5t}{4}, x3=3t4x_3 = \frac{3t}{4}. Choose t=4t = 4 to clear the denominators: C3H8+5 O2→3 CO2+4 H2O\mathrm{C_3H_8} + 5\,\mathrm{O_2} \to 3\,\mathrm{CO_2} + 4\,\mathrm{H_2O}.

Automating it

Here is a small balancer: it builds the element matrix, asks SymPy for the null space, and scales the basis vector to the smallest whole numbers.

python · try it yourself
from math import lcm
from sympy import Matrix

def balance(reactants, products):
    molecules = reactants + products
    elements = sorted({e for m in molecules for e in m})
    rows = []
    for e in elements:
        rows.append([m.get(e, 0) for m in reactants] +
                    [-m.get(e, 0) for m in products])
    ns = Matrix(rows).nullspace()
    if len(ns) != 1:
        return f"{len(ns)} independent solutions"
    v = ns[0]
    v = v * lcm(*[term.q for term in v])     # clear denominators
    if v[0] < 0:
        v = -v
    return list(v)

propane = {"C": 3, "H": 8}
O2, CO2, H2O = {"O": 2}, {"C": 1, "O": 2}, {"H": 2, "O": 1}
print(balance([propane, O2], [CO2, H2O]))
[1, 5, 3, 4]

term.q is the denominator of a SymPy rational number. If all denominators are cleared and the vector entries share no common factor, this gives the smallest balanced equation (the null space vector here already has a 1 in the free position, so that holds).

A harder reaction

Potassium permanganate reacting with hydrochloric acid has six species, and balancing it by trial and error takes a while:

x1 KMnO4+x2 HCl→x3 KCl+x4 MnCl2+x5 H2O+x6 Cl2x_1\,\mathrm{KMnO_4} + x_2\,\mathrm{HCl} \to x_3\,\mathrm{KCl} + x_4\,\mathrm{MnCl_2} + x_5\,\mathrm{H_2O} + x_6\,\mathrm{Cl_2}
python · try it yourself
from math import lcm, gcd
from functools import reduce
from sympy import Matrix

def balance(reactants, products):
    molecules = reactants + products
    elements = sorted({e for m in molecules for e in m})
    A = Matrix([[m.get(e, 0) for m in reactants] + [-m.get(e, 0) for m in products]
                for e in elements])
    (v,) = A.nullspace()
    v = v * lcm(*[t.q for t in v])
    g = reduce(gcd, [int(t) for t in v])
    return [int(t) // g for t in v]

KMnO4 = {"K": 1, "Mn": 1, "O": 4}
HCl = {"H": 1, "Cl": 1}
KCl, MnCl2 = {"K": 1, "Cl": 1}, {"Mn": 1, "Cl": 2}
H2O, Cl2 = {"H": 2, "O": 1}, {"Cl": 2}
print(balance([KMnO4, HCl], [KCl, MnCl2, H2O, Cl2]))
[2, 16, 2, 2, 8, 5]

So 2 KMnO4+16 HCl→2 KCl+2 MnCl2+8 H2O+5 Cl22\,\mathrm{KMnO_4} + 16\,\mathrm{HCl} \to 2\,\mathrm{KCl} + 2\,\mathrm{MnCl_2} + 8\,\mathrm{H_2O} + 5\,\mathrm{Cl_2}. Check chlorine: 16 on the left, 2+4+10=162 + 4 + 10 = 16 on the right. ✓

Note: If the null space has dimension 2 or more, the "reaction" is really two independent reactions written together, and there is no single balanced equation. If it has dimension 0, the proposed reaction is impossible (only the trivial solution).

In software

The same pattern, "conservation laws become a homogeneous system, the answer is the null space", appears in many places: flow conservation in network-flow problems (what flows into a node flows out), Kirchhoff's current law in circuits (Resistor Networks), stoichiometric matrices in metabolic-network modelling, and dimensional analysis (Dimensionless Variables).

Exercises

Exercise 1: Rust

Balance x1 Fe+x2 O2→x3 Fe2O3x_1\,\mathrm{Fe} + x_2\,\mathrm{O_2} \to x_3\,\mathrm{Fe_2O_3} by hand.

Answer

4 Fe+3 O2→2 Fe2O34\,\mathrm{Fe} + 3\,\mathrm{O_2} \to 2\,\mathrm{Fe_2O_3}.

Solution
Fe: x1−2x3=0→x1=2x3O: 2x2−3x3=0→x2=3x32x3=2 (to clear the 12): x1=4, x2=3\begin{aligned} &\mathrm{Fe}\text{: } x_1 - 2x_3 = 0 &&\to x_1 = 2x_3 \\ &\mathrm{O}\text{: } 2x_2 - 3x_3 = 0 &&\to x_2 = \frac{3x_3}{2} \\ &x_3 = 2 \text{ (to clear the } \tfrac{1}{2}\text{)}\text{: } x_1 = 4,\ x_2 = 3 \end{aligned}

Exercise 2: Ethanol

Write the element matrix for x1 C2H6O+x2 O2→x3 CO2+x4 H2Ox_1\,\mathrm{C_2H_6O} + x_2\,\mathrm{O_2} \to x_3\,\mathrm{CO_2} + x_4\,\mathrm{H_2O} and balance it.

Answer

C2H6O+3 O2→2 CO2+3 H2O\mathrm{C_2H_6O} + 3\,\mathrm{O_2} \to 2\,\mathrm{CO_2} + 3\,\mathrm{H_2O}.

Solution
C:H:O:[20−10600−212−2−1]\begin{array}{r} \mathrm{C}\text{:} \\ \mathrm{H}\text{:} \\ \mathrm{O}\text{:} \end{array} \begin{bmatrix} 2 & 0 & -1 & 0 \\ 6 & 0 & 0 & -2 \\ 1 & 2 & -2 & -1 \end{bmatrix}
C: x3=2x1H: x4=3x1O: x1+2x2=2x3+x4=7x1→x2=3x1x1=1→(1,3,2,3)\begin{aligned} &\mathrm{C}\text{: } x_3 = 2x_1 \qquad \mathrm{H}\text{: } x_4 = 3x_1 \qquad \mathrm{O}\text{: } x_1 + 2x_2 = 2x_3 + x_4 = 7x_1 \to x_2 = 3x_1 \\ &x_1 = 1 \to (1, 3, 2, 3) \end{aligned}

Exercise 3: Photosynthesis (code)

Use the balance function from the lesson on x1 CO2+x2 H2O→x3 C6H12O6+x4 O2x_1\,\mathrm{CO_2} + x_2\,\mathrm{H_2O} \to x_3\,\mathrm{C_6H_{12}O_6} + x_4\,\mathrm{O_2}.

Answer

6 CO2+6 H2O→C6H12O6+6 O26\,\mathrm{CO_2} + 6\,\mathrm{H_2O} \to \mathrm{C_6H_{12}O_6} + 6\,\mathrm{O_2}.

Solution
python · try it yourself
from math import lcm, gcd
from functools import reduce
from sympy import Matrix

def balance(reactants, products):
    mols = reactants + products
    els = sorted({e for m in mols for e in m})
    A = Matrix([[m.get(e, 0) for m in reactants] + [-m.get(e, 0) for m in products] for e in els])
    (v,) = A.nullspace()
    v = v * lcm(*[t.q for t in v])
    g = reduce(gcd, [int(t) for t in v])
    return [int(t) // g for t in v]

print(balance([{"C": 1, "O": 2}, {"H": 2, "O": 1}],
              [{"C": 6, "H": 12, "O": 6}, {"O": 2}]))
[6, 6, 1, 6]

Exercise 4: Ammonia oxidation

Balance x1 NH3+x2 O2→x3 NO+x4 H2Ox_1\,\mathrm{NH_3} + x_2\,\mathrm{O_2} \to x_3\,\mathrm{NO} + x_4\,\mathrm{H_2O}, by hand or in code.

Answer

4 NH3+5 O2→4 NO+6 H2O4\,\mathrm{NH_3} + 5\,\mathrm{O_2} \to 4\,\mathrm{NO} + 6\,\mathrm{H_2O}.

Solution
N: x1=x3H: 3x1=2x4→x4=3x12O: 2x2=x3+x4=x1+3x12=5x12→x2=5x14x1=4→(4,5,4,6)Check O: 2⋅5=10=4+6✓\begin{aligned} &\mathrm{N}\text{: } x_1 = x_3 \qquad \mathrm{H}\text{: } 3x_1 = 2x_4 \to x_4 = \frac{3x_1}{2} \\ &\mathrm{O}\text{: } 2x_2 = x_3 + x_4 = x_1 + \frac{3x_1}{2} = \frac{5x_1}{2} \to x_2 = \frac{5x_1}{4} \\ &x_1 = 4 \to (4, 5, 4, 6) \\ &\text{Check O: } 2 \cdot 5 = 10 = 4 + 6 \checkmark \end{aligned}

Test yourself

What kind of linear system do you solve to balance a chemical equation?

  1. An inconsistent system
  2. A homogeneous system
  3. A system with a unique solution
  4. A system over GF(2)\mathrm{GF}(2)

The null space of a reaction's element matrix is {0}\{\mathbf{0}\}. What does that mean?

  1. The reaction is already balanced
  2. There are infinitely many balanced forms
  3. The reaction cannot be balanced
  4. One coefficient is zero

The null-space vector is (14,54,34,1)(\frac{1}{4}, \frac{5}{4}, \frac{3}{4}, 1). How do you get whole-number coefficients?

  1. Multiply by 4, the least common multiple of the denominators
  2. Round each entry
  3. Add 1 to each entry
  4. Take the reciprocal