home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 2: PC
/
frozenfish_august_1995.bin
/
bbs
/
d07xx
/
d0759.lha
/
ChemBalance
/
ChemBalance.doc
< prev
next >
Wrap
Text File
|
1992-11-06
|
9KB
|
246 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, v1.0, (Copyright 1992) 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 and
the program name and version number. 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 $3.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. I have already detected an annoyance of Chembalance crashing
on 2.0 on specific problems that my 1.3 system handles without a hitch.
WARNING: This program will overwrite the files ram:list and
ram:mylist 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
8) use the "+" only as a binary operator
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
ChemBalance does have its limitations. It won't do very simple stuff
like O_2 + O_2 = O_3. It can't solve nonregular eq without your assistance.
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
The last thing I want to mention is that ChemBalance doesn't do windows
and it doesn't do nonregular eq's. Consider the nonregular eq:
CH_4 + O_2 = C_2H_2 + CO + H_2
It has five terms but only three elements. If you want to get ChemBalance
to solve it, you must help out by putting in at least two coef's yourself.
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 = C_2H_2 + 2CO + H_2
This would give us 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! So strike one up for
ChemBalance. By using ChemBalance I learned that nonregular eq's may have
nonequivalent solutions, thus proving the didactic as well as computational
value of the program.