home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 2: PC
/
frozenfish_august_1995.bin
/
bbs
/
d09xx
/
d0916.lha
/
ChemBalance
/
ChemBalance.doc
< prev
next >
Wrap
Text File
|
1993-10-04
|
10KB
|
257 lines
In 1986, while I was tutoring a chemistry student on the art of balancing
chemical equations, it occurred to me that the undetermined coefficients
we were looking for were just algebraic unknowns that could be solved
for by solving a system of linear equations for them. I soon figured
out how to explain my quasi-algorithm to others, but I also felt that
creating and distributing ChemBalance might encourage people to use the
algebraic approach themselves.
This program, ChemBalance, v2.0, (Copyright 1993) is distributed free
of charge to all who want to use it. Anyone may redistribute it as they
please as long as due acknowledgement is given to the author, the program
name, and the version number. Version 2.0 is an improvement on version 1.0
(FredFish 759). The current version is much better at error checking the
user's unbalanced equation, and it now prints out the final balanced equation
in the original order when UDCs are used. Most important, it now solves
nonregular equations and puts the solutions in the file `ram:solutions'.
I am expecting to put out improved versions based on proved weaknesses
of the current program, so please feel free to write me at
Patrick Reany Phone: (602) 995-1637
2408 W Myrtle #26
Phoenix AZ 85021
USA
if you have any suggestions for improvements. What follows is a brief
introduction to ChemBalance, but if you'd like a more thorough explanation,
send me $2.00 and I'll send you a copy of the user's manual for ChemBalance.
Also, please let me know if you'd be interested in improved versions
of ChemBalance.
WARNING: This program will overwrite the files ram:list, ram:mylist,
and ram:solutions if you have them.
To run the program, you must have ARexx ready to go. With that done,
put ChemBalance in ram: then open a shell or CLI and enter
"rx ram:chembalance".
Before we start we need to establish some terminology.
1) "RHS" = right hand side
2) "LHS" = left hand side
3) "total number of terms" means the sum of terms
from both sides of the equation
4) "eq" is short for equation
5) "coef" is short for coefficient
6) the "terminal coef" is the coef of the rightmost
term of the RHS
7) an eq that has at least as many elements as one less
than the total number of terms is said to be "regular"
8) if an eq is not regular it is said to be "nonregular"
9) "ansatz" means a trial solution
Some of the syntax rules for ChemBalance are
1) use "=" to separate LHS from RHS
2) use "_" to indicate a subscript
3) use "*" to preface a (generalized) hydration such as ...*6H_2O
or ...*NH_3
4) use any single capital or any single capital
followed by a small-case letter as an element
5) nested parentheses are not allowed
6) allowable input characters are all upper- and lower-case
letters, digits, "+","=","_","*"," ","(",")"
7) parentheses cannot follow an asterisk in a given term
Some of the formation rules are
1) each eq has exactly one equal sign
2) both sides of every eq have at least one term
3) each eq has at least one plus sign
(Thus each eq has at least 3 terms (total).)
4) an eq can have up to 7 terms
5) every element in each eq must be found on both sides
6) only 1 or 2 digits may immediately follow an asterisk
7) only 1, 2, or 3 digits may immediately follow an "_"
in input or in internal calculation
8) use the "+" only as a binary operator on terms
ChemBalance does have its limitations. It won't do very simple stuff
like O_2 + O_2 = O_3. If anyone would like the program to do 8 terms total,
please let me know.
Now for a bit of explanation. It's pretty obvious that every unbalanced
eq that has a solution, has an infinite number of solutions derived
by multiplying the solved eq thru by any positive integer you like.
Thus the coef's are determined only up to their mutual ratios. We can derive
a unique solution then (at least for regular eq's) by setting any one of
the coef's to a fixed integer. For convenience ChemBalance always chooses
to set the terminal coef equal to 1.
Let's try an example. Consider the eq
CO + O_2 = CO_2
We put a variable in front of each term to be solved for, except that
we put a 1 in front of the terminal term, thus giving us
xCO + yO_2 = 1CO_2
Now, we have two unknowns, so we need two elements in the eq to solve for.
We're in luck, since we have carbon, 'C', and oxygen, 'O'. (Thus we have
a regular eq.) Next we balance the eq one element at a time!
element term1 term2 term3
C: 1x + 0y = 1
O: 1x + 2y = 2
If you know some linear algebra you can solve these types of problems
easily, perhaps by using Cramer's rule (which is what ChemBalance uses),
though the present problem can be easily solved by inspection.
When you do you get
x = 1, y = 1/2
This fraction was introduced because of a mathematical object called
a transformation determinant. It's there to keep us honest: If the
transformation determinant is different from zero you get a solution.
If it's equal to zero, you don't---at least you don't for the combination
of elements you've chosen. In the present example it's do or die because
there are no other elements to choose from.
Now, ChemBalance does not like to deal with fractions (a trait no doubt
inherited from me), so it eliminates them by multiplying the balanced
eq thru by the value of the transformation determinant. The form we get
then is
Lcoef_1 x Lterm_1 + ... + Lcoef_L x Lterm_L =
Rcoef_1 x Rterm_1 + ... + det x Rterm_(terminal_term#)
where all the coef's are integers. Well, we did have "unique" coef's
but we changed them. That's OK. We may change them again to put the answer
in "reduced" form, a form in which there is no prime integer which will
evenly divide all the coef's. Actually, it's the existence of a unique
reduced eq for regular eq's that is of importance to us.
ChemBalance finds the elements it needs to set up the simultaneous
eq's in the elements_list. In the last problem it contained only 'C' and 'O'.
Now let's look at the eq
SO_2 + Cl_2 = SO_2Cl_2.
It will contain the three elements 'S O Cl' in the elements_list, and let's
say, for the sake of argument, that they're in it in that particular order.
Then when ChemBalance goes to form the 2 linear eq's it needs, by taking
the two leftmost elements in the list, after "balancing" for 'S' and 'O'
it will have completely ignored the second term on the left. This leads to
a 'det = 0' problem. What ChemBalance does about this is fairly good in
practice, but not perfect in theory.
The perfect thing to do is to make sure that ChemBalance takes at least one
element to balance from each term, but for this version all it does is
to cyclicly permute the elements_list and then try again. Don't laugh
---it seems to work very well. In this case the new list would be in the
order 'O Cl S'. Then ChemBalance would balance for 'O' and 'Cl', which brings
all terms to bear, and the solution is found.
Now let's show some typical forms for input.
-------> (NH_4)_2Cr_2O_7 = Cr_2O_3 + H_2O + N_2
-------> CoCl_2*6H_2O = CoCl_2*4H_2O + H_2O
To exit ChemBalance enter "quit" on the input line.
I wrote ChemBalance with the hope that people would use it, like it, and
then try out its algebraic quasi-algorithm. It's good for instruction,
but it's even better for actual use. Compare solving the following
using ChemBalance to doing them yourself by any method you like.
NaCl + H_2SO_4 + MnO_2 = Na_2SO_4 + MnSO_4 + H_2O + Cl_2
RCHO + CuSO_4 + NaOH = RCOONa + Cu_2O + Na_2SO_4 + H_2O
HCl + KMnO_4 = H_2O + KCl + MnCl_2 + Cl_2
C_7H_16 + O_2 = CO_2 + H_2O
C_12H_22O_11 + H_2SO_4 = C + H_2SO_4*H_2O
C_8H_18 + O_2 = CO_2 + CO + H_2O
KCN + FeCl_2 = K_4Fe(CN)_6 + KCl
P + Fe_2O_3 = P_4O_10 + Fe
C_15H_32 + O_2 = CO_2 + H_2O
Unlike version 1.0, ChemBalance 2.0 can solve nonregular eq's on its own.
But first we'll see how to it uses user-defined coefficients to help
solve them. Consider the nonregular eq:
CH_4 + O_2 = C_2H_2 + CO + H_2
It has five terms but only three elements. One way to get ChemBalance
to solve it is for the user to put in at least two coef's. If you try
the ansatz
CH_4 + 1O_2 = C_2H_2 + 2CO + H_2
for instance, it's true you've balanced for 'O', but you've also effectively
(though not actually) knocked out 'O' from the elements_list, since the
two fixed terms combine to form a single constant term, resulting in another
nonregular eq having four terms but only two elements, 'C' and 'H',
to use. So that didn't work.
But if we try to balance it for 'C', depending on how we do it, we could
try the ansatz
4CH_4 + O_2 = 1C_2H_2 + 2CO + H_2
which gives the solution
4CH_4 + O_2 = C_2H_2 + 2CO + 7H_2
But if we try the alternative ansatz for carbon
6CH_4 + O_2 = 2C_2H_2 + 2CO + H_2
we get the different solution
6CH_4 + O_2 = 2C_2H_2 + 2CO + 10H_2
What's neat about this solution is that it is not proportional to the
previous solution. They are in fact nonequivalent! Notice that the two
solutions differ by the balanced equation
2CH_4 = C_2H_2 + 3H_2
The second way to let ChemBalance solve nonregular equations is to simply
type in the equation and ChemBalance will prompt the user from there.
A typical input would be:
CH_4 + O_2 = C_2H_2 + CO + H_2
After a bit ChemBalance will return with a long message about how
to proceed. You have two choices: 1) hit return and enter the UDC's
yourself, or 2) enter the lower and upper coefficients for ChemBalance
to try (if you enter just one number ChemBalance will regard it as the
upper value to try). If you use method 2) then ChemBalance will try out
a bunch of coefficients and each time a solution is found it is written to
the file `ram:solutions'.
Finally, I can imagine many high-school chemistry students finding
ChemBalance a lot of fun to play with.