home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 5
/
FreshFish_July-August1994.bin
/
bbs
/
misc
/
chembalance-3.0.lha
/
ChemBalance-3.0
/
ChemBalance.doc
< prev
next >
Wrap
Text File
|
1994-06-08
|
12KB
|
304 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, v3.0, (Copyright 1994) 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 found on FredFish 916. The
current version can solve equations with as many as nine terms in it.
It can also deal with terms having multiple asterisks as shown later in
this writeup. The reason the program is shorter is because of the new
recursive procedure for doing determinants.
I am not expecting to release improved versions, although I haven't ruled-
out the possibility in the distant future. If you have any questions or
suggestions, please feel free to write me at
Patrick Reany Phone: (602) 995-1637
2408 W Myrtle #26
Phoenix AZ 85021
USA
What follows is a brief introduction to ChemBalance, but if you'd like a
more thorough explanation, send me $3.00 and I'll send you a copy of the
user's manual for ChemBalance and a writeup on the history and
controvery over using the algebraic method rather than the
oxidation-reduction method.
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 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 9 terms, perhaps more
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. It also won't do equations with ions in them. It
won't do equations like
HAuCl_3+K_4Fe(CN)_6=KAu(CN)_4+KAu(CN)_2+KAu(CN)_2Cl_2+KCl+HCl+
+[4Fe(CN)_3*3Fe(CN)_2]
the problem being the last term. Since C and N always appear as a
multiple of CN, (CN) can be replaced by X, say, yielding
HAuCl_3+K_4FeX_6=KAuX_4+KAuX_2+KAuX_2Cl_2+KCl+HCl+*4FeX_3*3FeX_2
Note that with additional changes we have brought the final term into
correct form. Remember that parentheses cannot follow an asterisks.
Unfortunately, this last eq is nonregular and it is not easy to find
any solutions for it. User-defined coefs are necessary.
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.
The simplest way to enter the skeletal equations presented in this
writeup is to 1) split the screen between the shell window on top
and the Ed window on bottom, 2) highlight one of the skeletal eqs and then
press right-amiga key and 'c'. This will copy the highlighted phrase to
to the clipboard. Then activate the shell window and press the
right-amiga and 'v', which will copy the clipboar