Solving Chemical Equations Part 6a: The Solution … Finally

Almost done. We will start with work to get the coefficients out of our row echelon matrix and end with a check and displaying the balanced equation.



Leading Non Zero ….

I was hoping this would not happen, but I have encountered something that I was not expecting. It is not something that I even said that we wanted, but I still wanted it.

For the leading non zero elements in the row echelon, I want that non zero to actually be positive, non zero and non-negative. This can be handled in two functions.


Least Common Multiple

The least common multiple of two or more numbers is simply the smallest number that is still divisible by all the numbers. It can be based on the Greatest Common Divisor. Below is how to calculate it. Try to justify why this is true yourself.

\[LCM(A,B)=\frac{A \cdot B}{GCD(A,B)}\]

We will do two functions just like we did for GCD, one for two and another for two or more.


Adding Two Lists

We will need just one function to combine the values of two lists (vectors).


The Solution

Hopefully, you did the examples that I laid out in part 1. Either way, I want to go over an example to illustrate what we will have to code for next time. This is an example of two degrees of freedom. While I have not encountered a chemical equation that yields this, It is mathematically possible. I will highlight the key features of the matrix, leading “positives” in red and other non zero in blue.

\[
\begin{bmatrix}
\color{red}{15}&0&0&\color{blue}{-2}&0&0&\color{blue}{-5}\\
0&\color{red}{12}&0&\color{blue}{-3}&0&0&\color{blue}{-7}\\
0&0&\color{red}7&\color{blue}{-1}&0&0&\color{blue}{-3}\\
0&0&0&0&\color{red}3&0&\color{blue}{-4}\\
0&0&0&0&0&\color{red}5&\color{blue}{-2}
\end{bmatrix}
\]

It is the blue columns that will be of primary import. Each on there own will represent a solution. Let’s first take a look at the solution that comes from the first column.

I will note numerators 2,3,1 are placed because of the column location of the leading positive term. The placement of the lone s comes from the column location of the nonleading non zero terms. I made the s variables green for readability.

\[
\begin{bmatrix}
\frac{\color{blue}2}{\color{red}{15}}\color{green}s \\ \frac{\color{blue}3}{\color{red}{12}}\color{green}s \\ \frac{\color{blue}{1}}{\color{red}7}\color{green}s \\ \color{green}s \\ 0 \\ 0 \\ 0
\end{bmatrix}
\]

Since these solutions represent coefficients of a chemical equation, we always want the solution to be in the lowest possible terms. So all we have to do is choose s such that we are left with no fractions. This can be done by finding the LCM(15,12,7)

\(GCD(15,12)=3\)
\(LCM(15,12)= \frac{15 \cdot 12}{3} = \frac{180}{3} = 60 \)
\(GCD(60,7)=1\)
\(LCM(60,7)=\frac{60 \cdot 7}{1} = 420\)
\(LCM(15,12,7)=420\)

So s should be 420

\[
\begin{array}{c}
\begin{bmatrix}
\frac{2}{15}420 \\ \frac{3}{12}420 \\ \frac{1}{7}420 \\ 420 \\ 0 \\ 0 \\ 0
\end{bmatrix}
=
\begin{bmatrix}
56 \\ 105 \\ 60 \\ 420 \\ 0 \\ 0 \\ 0
\end{bmatrix}
\end{array}
\]

Let’s do the same for the other solution.

\[
\begin{bmatrix}
\frac{\color{blue}5}{\color{red}{15}}\color{green}t \\ \frac{\color{blue}7}{\color{red}{12}}\color{green}t \\ \frac{\color{blue}3}{\color{red}7}\color{green}t \\ 0 \\ \frac{\color{blue}4}{\color{red}3}\color{green}t \\ \frac{\color{blue}2}{\color{red}5}\color{green}t \\ \color{green}t
\end{bmatrix}
\]

I will leave it to you to confirm, but t should be 420 again. 3 and 5 were already factors of 15,12,7.

\[
\begin{array}{c}
\begin{bmatrix}
\frac{5}{15}420 \\ \frac{7}{12}420 \\ \frac{3}{7}420 \\ 0 \\ \frac{4}{3}420\\ \frac{2}{5}420 \\ 420
\end{bmatrix}
=
\begin{bmatrix}
140 \\ 245 \\ 180 \\ 0 \\ 560 \\ 168 \\ 420
\end{bmatrix}
\end{array}
\]

Now for the final solution, we will combine the two “smaller” solutions.

\[
\begin{array}{c}
\begin{bmatrix}
56 \\ 105 \\ 60 \\ 420 \\ 0 \\ 0 \\ 0
\end{bmatrix}
+
\begin{bmatrix}
140 \\ 245 \\ 180 \\ 0 \\ 560 \\ 168 \\ 420
\end{bmatrix}
=
\begin{bmatrix}
196 \\ 350 \\ 240 \\ 420 \\ 560 \\ 168 \\ 420
\end{bmatrix}
\end{array}
\]

Multiplying our row echelon matrix with the solution should yield all zeroes.

\[
\begin{array}{cc}
&
\begin{bmatrix}
196 \\ 350 \\ 240 \\ 420 \\ 560 \\ 168 \\ 420
\end{bmatrix}
\\
\begin{bmatrix}
15&0&0&-2&0&0&-5\\
0&12&0&-3&0&0&-7\\
0&0&7&-1&0&0&-3\\
0&0&0&0&3&0&-4\\
0&0&0&0&0&5&-2
\end{bmatrix}
&
\begin{bmatrix}
15 \cdot 196 -2 \cdot 420 -5 \cdot 420 = 0 \\
12 \cdot 350 -3 \cdot 420 -7 \cdot 420 = 0 \\
7 \cdot 240 -1 \cdot 420 -3 \cdot 420 = 0 \\
3 \cdot 560 -4 \cdot 420 = 0 \\
5 \cdot 168 -2 \cdot 420 = 0
\end{bmatrix}
\end{array}
\]

Perfect

I want to take a step back now and look at the row echelon matrix and the two “solutions.” I will once again color things to make the connections clearer.

\[
\begin{bmatrix}
\color{red}{15}&0&0&\color{blue}{-2}&0&0&\color{blue}{-5}\\
0&\color{red}{12}&0&\color{blue}{-3}&0&0&\color{blue}{-7}\\
0&0&\color{red}7&\color{blue}{-1}&0&0&\color{blue}{-3}\\
0&0&0&0&\color{red}3&0&\color{blue}{-4}\\
0&0&0&0&0&\color{red}5&\color{blue}{-2}
\end{bmatrix}
\begin{array}{c}
\begin{bmatrix}
\frac{\color{blue}2}{\color{red}{15}}\color{green}s \\ \frac{\color{blue}3}{\color{red}{12}}\color{green}s \\ \frac{\color{blue}{1}}{\color{red}7}\color{green}s \\ \color{green}s \\ 0 \\ 0 \\ 0
\end{bmatrix}
&
\begin{bmatrix}
\frac{\color{blue}5}{\color{red}{15}}\color{green}t \\ \frac{\color{blue}7}{\color{red}{12}}\color{green}t \\ \frac{\color{blue}3}{\color{red}7}\color{green}t \\ 0 \\ \frac{\color{blue}4}{\color{red}3}\color{green}t \\ \frac{\color{blue}2}{\color{red}5}\color{green}t \\ \color{green}t
\end{bmatrix}
\end{array}
\]

The lone s and t come directly from nonleading non zero columns. The locations of the red/blue pairs come from the column location of the red (leading positive). Finally, we determined s and t by finding the least common multiple of the corresponding denominator (nonleading non zero) elements.

This is getting coded in the next part. You have been warned. It really won’t be that bad, as long as everything in this section made sense.


A Check

Hopefully, everything at this point is solid. If not, I want to create a final check by comparing how many of each element are produced on each side of the equation with the just obtained coefficients.


Displaying The Balanced Equation

Recall that when entering formulas into our equation data structure we imposed an ordering. This is the same ordering that we used to generate the matrix and is the same ordering that the solution will be in. We also conveniently stored the original text of all of the formulas, so displaying the balanced equation should be a cinch compared to everything else that has happened. One function.


Finally

After all this, everything should be wrapped up with a nice bow. This might actually be in a part C or even a part 7. I am not sure yet.


Before You Go

Compared to everything else, this coding will not be bad at all. However, this is dependent on your understanding of the Math in part 1. If “The Solution” section made sense, then you should be fine. Otherwise, I will leave it up to you if you should take another look at part one or pour over this post again.

Untill Next Time …