I’m fairly certain that solving chemical equations is a part of most if not all high school curriculums, so most people probably at least remember doing it if not exactly how they did it. About a year ago I had the opportunity to work with some local high school students and this very subject came up. It was only at this point that I recalled that the method that is taught to them is guess and check. That’s not very exciting. Let’s use some math and a little (I mean a LOT) of Python to make it more exciting.
This is the start of series of posts that will conclude with a full-fledged Python program that will take an unbalanced chemical equation and output a balanced one. There will be a lot of moving parts so hold onto your mouse.
The Math
We will use Linear Algebra as a main driver to solve an example chemical equation. This will give a basic structure of how we will handle the program. With no further delay, let’s look at the example equation for us to balance.
\[HClO_{4} + P_{4}O_{10} \to H_{3}PO_{4} + Cl_{2}O_{7}\]
We will assign an arbitrary variable to represent each coefficient.
\[xHClO_{4} + yP_{4}O_{10} \to zH_{3}PO_{4} + wCl_{2}O_{7}\]
The name of the game is to have the same number of each respective element on both sides of the equation. We will now create four equations for our four elements (H, Cl, O, P)
\[
\begin{matrix}
\\
H: \\
Cl: \\
O: \\
P:\\
\end{matrix}
\begin{matrix}
x & y & & z & w\\
x & & = &3z &\\
x&&=&&2w \\
4x&+10y&=&4z&+7w \\
&4y&=&z&\\
\end{matrix}
\]
Let’s move all these terms to the left hand side, so we have something equal to zero.
\[
\begin{matrix}
\\
H: \\
Cl: \\
O: \\
P:\\
\end{matrix}
\begin{matrix}
x & y & z & w\\
x & &-3z & &=&0\\
x&&&-2w&=&0 \\
4x&+10y&-4z&-7w&=&0 \\
&4y&-z&&=&0\\
\end{matrix}
\]
Now, let’s convert to a matrix dropping the zeroes for now. Don’t worry, we will add them back in later.
\[
\begin{bmatrix}
1&0&-3&0 \\
1&0&0&-2 \\
4&10&-4&-7 \\
0&4&-1&0 \\
\end{bmatrix}
\]
Now we will transform this matrix into a kind of reduced row echelon (don’t worry if you are not familiar with this term). What we want is the following:
If you find your eyes glassing over and your head starting to nod off as you read this list, try rereading it after you get to the end of the post. It might make more sense then, especially if this is your first exposure to reduced row echelon.
- All nonzero rows are above any rows of all zeroes
- the leading coefficient of a nonzero row is always strictly to the right of the leading coefficient of the row above it
- Every leading nonzero is the only nonzero entry in its column
- Leading nonzero coefficients need not be 1 and all elements should be retianed as integers
First, let’s use the 1 in the upper left-hand corner (location 1,1) to get rid of all the other entries in the same column.
negative one times row one plus row two to yield a new row two
negative four times row one plus row three to yield a new row three
\(-R_{1} + R_{2} \to R_{2}\)
\(-4R_{1} + R_{3} \to R_{3}\)
\[
\begin{bmatrix}
1&0&-3&0 \\
0&0&3&-2 \\
0&10&8&-7 \\
0&4&-1&0 \\
\end{bmatrix}
\]
Now let’s rearrange things and use the 4 in the second column to blast the other entries in that column. We will also multiply by 4 to the parts we are wanting to blast to make later math easier.
\(R_{2} \leftrightarrow R_{4}\)
\(4R_{3} \to R_{3}\)
\[
\begin{bmatrix}
1&0&-3&0 \\
0&4&-1&0 \\
0&40&32&-28 \\
0&0&3&-2 \\
\end{bmatrix}
\]
Use that 4 to blast everything else in that column to ashes.
\(-10R_{2} + R_{3} \to R_{3}\)
\[
\begin{bmatrix}
1&0&-3&0 \\
0&4&-1&0 \\
0&0&42&-28 \\
0&0&3&-2 \\
\end{bmatrix}
\]
Let’s reduce row three.
\(\frac{1}{14}R_{3} \to R_{3}\)
\[
\begin{bmatrix}
1&0&-3&0 \\
0&4&-1&0 \\
0&0&3&-2 \\
0&0&3&-2 \\
\end{bmatrix}
\]
We now set up to do our last round of blasting of columns. target blaster is the three in the third-row third-column (location 3,3). We will multiply row two by three to make the math easier, row one does not need it since negative 3 already has a factor of three.
\(3R_{2} \to R_{2}\)
\[
\begin{bmatrix}
1&0&-3&0 \\
0&12&-3&0 \\
0&0&3&-2 \\
0&0&3&-2 \\
\end{bmatrix}
\]
3,3 three blasts everything to ash!!!
\(R_{3}+R_{1} \to R{1}\)
\(R_{3}+R_{2} \to R{2}\)
\(-R_{3}+R_{4} \to R{4}\)
\[
\begin{bmatrix}
1&0&0&-2 \\
0&12&0&-2 \\
0&0&3&-2 \\
0&0&0&0 \\
\end{bmatrix}
\]
Lastly, let’s reduce row 2.
\(\frac{1}{2}R_{2} \to R_{2}\)
\[
\begin{bmatrix}
1&0&0&-2 \\
0&6&0&-1 \\
0&0&3&-2 \\
0&0&0&0 \\
\end{bmatrix}
\]
I will re-address Reduced Row Echelon. Every leading nonzero (1,6,3) is to the right of the leading nonzero of the row that precedes it. Alos, every leading nonzero (1,6,3) is the only nonzero in that column.
Before we move forward I want to point out something. We have one degree of freedom. We have one column that has multiple nonzero elements. Elements in this column do not correspond to leading nonzero elements. Note that this is our \(w\) column.
Back to equations
\[
\begin{matrix}
x & y & z & w\\
x & & & -2w&=&0\\
&6y&&-w&=&0 \\
&&3z&-2w&=&0 \\
\end{matrix}
\]
Now we will let \(w\) be \(s\), remember that degree of freedom. We will also move all of those terms to the right-hand side.
\[
\left\{
\begin{array}{ccc}
x&=&2s \\
6y&=&s \\
3z&=&2s \\
w&=&s
\end{array}
\right.
\]
Solve for x,y,z,w respectively
\[
\left\{
\begin{array}{ccc}
x&=&2s \\
y&=&\frac{1}{6}s \\
z&=&\frac{2}{3}s \\
w&=&s
\end{array}
\right.
\]
Let’s chose something smart for \(s\) to be, six should be good. Then we get
\[
\left\{
\begin{array}{ccc}
x&=&12 \\
y&=&1 \\
z&=&4 \\
w&=&6
\end{array}
\right.
\]
And voila we can construct our balanced chemical equation.
\[12HClO_{4} + P_{4}O_{10} \to 4H_{3}PO_{4} + 6Cl_{2}O_{7}\]
A quick inspection tells us the equation is balanced.
Final Things
This is a good stopping point for now. I will continue this series by wrestling Python to do everything we did above for us. I will leave you with two things. An example of a matrix that has two degrees of freedom and some practice equations to balance yourself. Both of these will be useful for future posts.
Two Degrees of Freedom
\[
\begin{bmatrix}
1&0&0&-2&0&0&-5\\
0&1&0&-3&0&0&-7\\
0&0&1&-1&0&0&-3\\
0&0&0&0&1&0&-4\\
0&0&0&0&0&1&-2
\end{bmatrix}
\]
Associating each column with variable \(x_1,x_2, … x_7\) see if you can tell how we obtain the following equations.
\[
\left\{
\begin{array}{ccc}
x_1&=&5s+2t\\
x_2&=&7s+3t \\
x_3&=&3s+t \\
x_4&=&t\\
x_5&=&4s\\
x_6&=&2s\\
x_7&=&s\\
\end{array}
\right.
\]
For a solution, any choice of values for s and t will yield a solution. However, since the goal is to find solutions for chemical equations, we will always choose something simple, one is a good choice for both s and t.
Problems
Try to do these exactly how I did it above. I will provide the reduced row echelon matrix and the final balanced equation for your use.
Problem 1
\[CaCN_{2} + H_{2}O \to CaCO_{3} + NH_{3}\]
\[
\begin{bmatrix}
2&0&0&-1 \\
0&2&0&-3 \\
0&0&2&-1 \\
0&0&0&0 \\
0&0&0&0 \\
\end{bmatrix}
\]
\[CaCN_{2} + 3H_{2}O \to CaCO_{3} + 2NH_{3}\]
Problem 2
\[FeCl_{3}+NH_{4}OH \to Fe(OH)_{3}+NH_{4}Cl\]
\[
\begin{bmatrix}
3&0&0&-1 \\
0&1&0&-1 \\
0&0&3&-1 \\
0&0&0&0 \\
0&0&0&0 \\
\end{bmatrix}
\]
\[FeCl_{3}+3NH_{4}OH \to Fe(OH)_{3}+3NH_{4}Cl\]
Recent Comments