There are some days when technology just doesn’t agree with one, so today’s post is more scientific in nature. There are many instances when chemical equations need to be balanced, and perhaps mentioning them outside of school is geeky enough, but this isn’t going to be a science lesson. Most of us learned how to balance chemical equations sometime in high school and off-hand, might have a hard time trying to put an algorithm to something which seemed to take different approaches dependent on the problem. The question, then, is how does a computer balance a chemical equation?
One sure-fire way to balance chemical equations is algebraically. Each species is assigned a variable, and equations are written to equate the elements on the products side with those on the reactants side. This results in a problem with multiple unknowns which can be solved by setting one variable to an arbitrary value, and using substitution to solve the remaining unknowns. For most reasonably difficult (5-6 species, without an ‘obvious’ answer) this can be solved in a few minutes using pencil and paper.
It is rather a challenge to get a computer to solve systems of equations, however, by transcribing these equations into matrix form, the task becomes considerably easier. Keep in mind that this approach provides the ratio of the coefficients, so one less equation is needed than the number of unknowns.
There are typically three basic row operations used for matrices (switch two rows, add/subtract, multiply/divide) and the same will be used for this algorithm.
The basic procedure to implement this on a computer would be as follows:
- Parse the input equation – count the number of each element in each species
- Create an array with one ‘column’ per species and one ‘row’ per equation.
- Ensure the first row has a non-zero value in the first column, if not, switch the row with one that does
- Divide every value in the first row by the value of the first column of row one.
- Goto the second row
- Ensure the second row has a non-zero value in the second column, if not, switch the row with another row (>2), that does
- If the first column of the second row has a non-zero value, subtract, from each value in row two, the corresponding value in row one, multiplied by the value in the first column of row two (before this, column one of row one should be equal to 1).
- Divide every value in the second row by the value of the second column of row two.
- Repeat for each successive row, ensuring that all columns less than the row number is zero (otherwise follow multiply & subtract), and ensure that the column equal to the row is non-zero, otherwise switch with another row.
The final result should leave all zeros in the first few columns, except, every row should have a row in the column matching its row number. Additionally, the last column will have numbers other than one and zero. These represent the ratio between each species.
If fractional/decimal answers are acceptable, no further steps are needed. Otherwise, it is advisable to keep track of division operations to get an easy to work with denominator (fractional representation instead of decimal), from which it is easy to produce a lowest common integer multiple.
A further point of mention, it is likely advisable to determine the order of the equations before attempting to solve them – this will prevent any conditions arising where there are no valid equations remaining. Additionally, if extra equations exist at the end, they can be discarded
PDF Example: Balancing Chemical Equations Using Matrices