# How would chemical equations be balanced with matrices? [closed]

For example, I have this equation:

$$\mathrm{KMnO_4 + HCl = KCl + MnCl_2 + H_2O + Cl_2}$$

Then I get this:

$$a \cdot \mathrm{KMnO_4} + b \cdot \mathrm{HCl} = c \cdot \mathrm{KCl} + d \cdot \mathrm{MnCl_2} + e \cdot \mathrm{H_2O} + f \cdot \mathrm{Cl_2}$$

\begin{align} \mathrm{K}&: &a &= c \\ \mathrm{Mn}&: &a &= d \\ \mathrm{O}&: &4a &= e \\ \mathrm{H}&: &b &= 2e \\ \mathrm{Cl}&: &b &= c + 2d + 2f \end{align}

$$\begin{bmatrix} a&b&c&d&e&|&f\\ 1&0&-1&0&0&|&0\\ 1&0&0&-1&0&|&0\\ 4&0&0&0&-1&|&0\\ 0&1&0&0&-2&|&0\\ 0&1&-1&-2&0&|&2 \end{bmatrix}$$

How would I get the values of $a, b, c, d, e,$ and $f$ from here?

Side note: I'm following this.

• $n$ is a variable? And $f$ does not occur anywhere. Commented Nov 4, 2014 at 17:55
• @Nimda Where did you get n? And I corrected the problem with f Commented Nov 4, 2014 at 22:51
• Use row reduction (also known as Gaussian elimination). Technically, that first row isn't there (unless you put it there for labeling reasons) and the last column should be a column of all 0's once you move all the variable terms to the left side. But you have the right set up. Row reduce. You'll have a free variable at the end that you can set to $1$ or whatever convenient number gets rid of all the fractions. Commented Nov 4, 2014 at 23:28
• Somwhere I read dM * nCl2 Commented Nov 6, 2014 at 19:39
• @Nimda My bad, I was typing too fast and misspelt it Commented Nov 7, 2014 at 19:26

Here's a "mechanical" script that works for most chemical equations.

When $A_{ij} \ge 0$, product species in the solution vector will have negative coefficients, so there's no need to indicate them in the A-matrix.

Or if you (correctly) assign all product species with negative coefficients in the A-matrix, then the solution vector will be positive, i.e. $x_k \ge 0$. I'd rather let the script sort this out for me, but to each his own...

#!/usr/bin/env  julia
# Solve stoichiometry problem

function maxDen(r) # find largest denominator in a Rational array
m = 1; for p in r;  m = max(den(p), m); end; return m
end

A = readdlm("stoich-A.mat")  # A = rows are elements, cols are species
P = pinv(A)*A; P = eye(P)-P  # P = projector into nullspace of A
x = rand(size(P[1,:])) * P   # x = solution  vector

x /= minimum(abs(x))*sign(x[1]) # convert x to integer coeffs
N = (2*3*5)^2 * 7 * 11 * 13     # highly composite number
r = (round(Int, N*x)) // N
r *= maxDen(r);  r *= maxDen(r);  r *= maxDen(r)
r = round(Int, r)
s = "\nThe stoichiometric coefficients are   $(r) \n" @printf "%s" s  K: a = c Mn: a = d O: 4a = e H: b = 2e Cl: b = c + 2d + 2f How would I get the values of a, b, c, d, e, and f from here? Well... Reading the equations in the order they were given and using a as a parameter, one gets successively c = a, d = a, e = 4a, b = 2e = 8a, and 2f = b - c - 2d = 8a - a - 2a = 5a. This is solved by a = c = d = 2, e = 8, b = 16 and f = 5, thus, the balanced equation is $$\text{2 KMnO^4 + 16 HCl \to 2 KCl + 2 MnCl^2 + 8 H^2O + 5 Cl^2}$$ • I understand this method to solve the equation, but I'm trying to practice using matrices to solve it Commented Nov 5, 2014 at 13:09 • Why?$   \$
– Did
Commented Nov 5, 2014 at 13:10
• It's so that it can be mechanically solved by a program, and I can't find any other way that I can use to mechanically solve it Commented Nov 5, 2014 at 14:03