home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d9xx / d916 / chembalance.lha / ChemBalance / ChemBalance.doc < prev    next >
Text File  |  1993-10-04  |  10KB  |  257 lines

  1.  
  2.  
  3. In 1986, while I was tutoring a chemistry student on the art of balancing
  4. chemical equations, it occurred to me that the undetermined coefficients
  5. we were looking for were just algebraic unknowns that could be solved
  6. for by solving a system of linear equations for them. I soon figured
  7. out how to explain my quasi-algorithm to others, but I also felt that
  8. creating and distributing ChemBalance might encourage people to use the
  9. algebraic approach themselves.
  10.  
  11. This program, ChemBalance, v2.0, (Copyright 1993) is distributed free
  12. of charge to all who want to use it. Anyone may redistribute it as they
  13. please as long as due acknowledgement is given to the author, the program
  14. name, and the version number. Version 2.0 is an improvement on version 1.0
  15. (FredFish 759). The current version is much better at error checking the
  16. user's unbalanced equation, and it now prints out the final balanced equation
  17. in the original order when UDCs are used. Most important, it now solves
  18. nonregular equations and puts the solutions in the file `ram:solutions'.
  19.  
  20. I am expecting to put out improved versions based on proved weaknesses
  21. of the current program, so please feel free to write me at
  22.  
  23.      Patrick Reany                    Phone: (602) 995-1637
  24.      2408 W Myrtle #26
  25.      Phoenix  AZ  85021
  26.      USA
  27.  
  28. if you have any suggestions for improvements. What follows is a brief
  29. introduction to ChemBalance, but if you'd like a more thorough explanation,
  30. send me $2.00 and I'll send you a copy of the user's manual for ChemBalance.
  31. Also, please let me know if you'd be interested in improved versions
  32. of ChemBalance.
  33.  
  34. WARNING: This program will overwrite the files ram:list, ram:mylist,
  35. and ram:solutions if you have them.
  36.  
  37. To run the program, you must have ARexx ready to go. With that done,
  38. put ChemBalance in ram: then open a shell or CLI and enter
  39. "rx ram:chembalance".
  40.  
  41. Before we start we need to establish some terminology.
  42.  
  43. 1) "RHS" = right hand side
  44. 2) "LHS" = left hand side
  45. 3) "total number of terms" means the sum of terms
  46.    from both sides of the equation
  47. 4) "eq" is short for equation
  48. 5) "coef" is short for coefficient
  49. 6) the "terminal coef" is the coef of the rightmost
  50.    term of the RHS
  51. 7) an eq that has at least as many elements as one less
  52.    than the total number of terms is said to be "regular"
  53. 8) if an eq is not regular it is said to be "nonregular"
  54. 9) "ansatz" means a trial solution
  55.  
  56.  
  57. Some of the syntax rules for ChemBalance are
  58.  
  59. 1) use "=" to separate LHS from RHS
  60. 2) use "_" to indicate a subscript
  61. 3) use "*" to preface a (generalized) hydration such as ...*6H_2O
  62.    or ...*NH_3
  63. 4) use any single capital or any single capital
  64.    followed by a small-case letter as an element
  65. 5) nested parentheses are not allowed
  66. 6) allowable input characters are all upper- and lower-case
  67.    letters, digits, "+","=","_","*"," ","(",")"
  68. 7) parentheses cannot follow an asterisk in a given term
  69.  
  70.  
  71. Some of the formation rules are
  72.  
  73. 1) each eq has exactly one equal sign
  74. 2) both sides of every eq have at least one term
  75. 3) each eq has at least one plus sign
  76.    (Thus each eq has at least 3 terms (total).)
  77. 4) an eq can have up to 7 terms
  78. 5) every element in each eq must be found on both sides
  79. 6) only 1 or 2 digits may immediately follow an asterisk
  80. 7) only 1, 2, or 3 digits may immediately follow an "_"
  81.    in input or in internal calculation
  82. 8) use the "+" only as a binary operator on terms
  83.  
  84.  
  85. ChemBalance does have its limitations. It won't do very simple stuff
  86. like O_2 + O_2 = O_3. If anyone would like the program to do 8 terms total,
  87. please let me know.
  88.  
  89. Now for a bit of explanation. It's pretty obvious that every unbalanced
  90. eq that has a solution, has an infinite number of solutions derived
  91. by multiplying the solved eq thru by any positive integer you like.
  92. Thus the coef's are determined only up to their mutual ratios. We can derive
  93. a unique solution then (at least for regular eq's) by setting any one of
  94. the coef's to a fixed integer. For convenience ChemBalance always chooses
  95. to set the terminal coef equal to 1.
  96.  
  97. Let's try an example. Consider the eq
  98.  
  99.          CO + O_2 = CO_2
  100.  
  101. We put a variable in front of each term to be solved for, except that
  102. we put a 1 in front of the terminal term, thus giving us
  103.  
  104.           xCO + yO_2 = 1CO_2
  105.  
  106. Now, we have two unknowns, so we need two elements in the eq to solve for.
  107. We're in luck, since we have carbon, 'C', and oxygen, 'O'. (Thus we have
  108. a regular eq.) Next we balance the eq one element at a time!
  109.  
  110.   element    term1   term2   term3
  111.  
  112.     C:        1x   +   0y   =  1
  113.     O:        1x   +   2y   =  2
  114.  
  115. If you know some linear algebra you can solve these types of problems
  116. easily, perhaps by using Cramer's rule (which is what ChemBalance uses),
  117. though the present problem can be easily solved by inspection.
  118.  When you do you get
  119.  
  120.      x = 1,  y = 1/2
  121.  
  122. This fraction was introduced because of a mathematical object called
  123. a transformation determinant. It's there to keep us honest: If the
  124. transformation determinant is different from zero you get a solution.
  125. If it's equal to zero, you don't---at least you don't for the combination
  126. of elements you've chosen. In the present example it's do or die because
  127. there are no other elements to choose from.
  128.  
  129. Now, ChemBalance does not like to deal with fractions (a trait no doubt
  130. inherited from me), so it eliminates them by multiplying the balanced
  131. eq thru by the value of the transformation determinant. The form we get
  132. then is
  133.  
  134.  
  135.   Lcoef_1 x Lterm_1 + ... + Lcoef_L x Lterm_L =
  136.           Rcoef_1 x Rterm_1 + ... + det x Rterm_(terminal_term#)
  137.  
  138. where all the coef's are integers. Well, we did have "unique" coef's
  139. but we changed them. That's OK. We may change them again to put the answer
  140. in "reduced" form, a form in which there is no prime integer which will
  141. evenly divide all the coef's. Actually, it's the existence of a unique
  142. reduced eq for regular eq's that is of importance to us.
  143.  
  144. ChemBalance finds the elements it needs to set up the simultaneous
  145. eq's in the elements_list. In the last problem it contained only 'C' and 'O'.
  146. Now let's look at the eq
  147.  
  148.           SO_2 + Cl_2 = SO_2Cl_2.
  149.  
  150. It will contain the three elements 'S O Cl' in the elements_list, and let's
  151. say, for the sake of argument, that they're in it in that particular order.
  152. Then when ChemBalance goes to form the 2 linear eq's it needs, by taking
  153. the two leftmost elements in the list, after "balancing" for 'S' and 'O'
  154. it will have completely ignored the second term on the left. This leads to
  155. a 'det = 0' problem. What ChemBalance does about this is fairly good in
  156. practice, but not perfect in theory.
  157.  
  158. The perfect thing to do is to make sure that ChemBalance takes at least one
  159. element to balance from each term, but for this version all it does is
  160. to cyclicly permute the elements_list and then try again. Don't laugh
  161. ---it seems to work very well. In this case the new list would be in the
  162. order 'O Cl S'. Then ChemBalance would balance for 'O' and 'Cl', which brings
  163. all terms to bear, and the solution is found.
  164.  
  165. Now let's show some typical forms for input.
  166.  
  167. ------->  (NH_4)_2Cr_2O_7 = Cr_2O_3 + H_2O + N_2
  168.  
  169. ------->  CoCl_2*6H_2O = CoCl_2*4H_2O + H_2O
  170.  
  171. To exit ChemBalance enter "quit" on the input line.
  172.  
  173. I wrote ChemBalance with the hope that people would use it, like it, and
  174. then try out its algebraic quasi-algorithm. It's good for instruction,
  175. but it's even better for actual use. Compare solving the following
  176. using ChemBalance to doing them yourself by any method you like.
  177.  
  178.  
  179. NaCl + H_2SO_4 + MnO_2 = Na_2SO_4 + MnSO_4 + H_2O + Cl_2
  180.  
  181. RCHO + CuSO_4 + NaOH = RCOONa + Cu_2O + Na_2SO_4 + H_2O
  182.  
  183. HCl + KMnO_4 = H_2O + KCl + MnCl_2 + Cl_2
  184.  
  185. C_7H_16 + O_2 = CO_2 + H_2O
  186.  
  187. C_12H_22O_11 + H_2SO_4 = C + H_2SO_4*H_2O
  188.  
  189. C_8H_18 + O_2 = CO_2 + CO + H_2O
  190.  
  191. KCN + FeCl_2 = K_4Fe(CN)_6 + KCl
  192.  
  193. P + Fe_2O_3 = P_4O_10 + Fe
  194.  
  195. C_15H_32 + O_2 = CO_2 + H_2O
  196.  
  197.  
  198.  
  199. Unlike version 1.0, ChemBalance 2.0 can solve nonregular eq's on its own.
  200. But first we'll see how to it uses user-defined coefficients to help
  201. solve them. Consider the nonregular eq:
  202.  
  203.      CH_4 + O_2 = C_2H_2 + CO + H_2
  204.  
  205. It has five terms but only three elements. One way to get ChemBalance
  206. to solve it is for the user to put in at least two coef's. If you try
  207. the ansatz
  208.  
  209.      CH_4 + 1O_2 = C_2H_2 + 2CO + H_2
  210.  
  211. for instance, it's true you've balanced for 'O', but you've also effectively
  212. (though not actually) knocked out 'O' from the elements_list, since the
  213. two fixed terms combine to form a single constant term, resulting in another
  214. nonregular eq having four terms but only two elements, 'C' and 'H',
  215. to use. So that didn't work.
  216.  
  217. But if we try to balance it for 'C', depending on how we do it, we could
  218. try the ansatz
  219.  
  220.      4CH_4 + O_2 = 1C_2H_2 + 2CO + H_2
  221.  
  222. which gives the solution
  223.  
  224.      4CH_4 + O_2 = C_2H_2 + 2CO + 7H_2
  225.  
  226. But if we try the alternative ansatz for carbon
  227.  
  228.      6CH_4 + O_2 = 2C_2H_2 + 2CO + H_2
  229.  
  230. we get the different solution
  231.  
  232.      6CH_4 + O_2 = 2C_2H_2 + 2CO + 10H_2
  233.  
  234. What's neat about this solution is that it is not proportional to the
  235. previous solution. They are in fact nonequivalent! Notice that the two
  236. solutions differ by the balanced equation
  237.  
  238.      2CH_4 = C_2H_2 + 3H_2
  239.  
  240. The second way to let ChemBalance solve nonregular equations is to simply
  241. type in the equation and ChemBalance will prompt the user from there.
  242.  
  243. A typical input would be:
  244.  
  245.      CH_4 + O_2 = C_2H_2 + CO + H_2
  246.  
  247. After a bit ChemBalance will return with a long message about how
  248. to proceed. You have two choices: 1) hit return and enter the UDC's
  249. yourself, or 2) enter the lower and upper coefficients for ChemBalance
  250. to try (if you enter just one number ChemBalance will regard it as the
  251. upper value to try). If you use method 2) then ChemBalance will try out
  252. a bunch of coefficients and each time a solution is found it is written to
  253. the file `ram:solutions'.
  254.  
  255. Finally, I can imagine many high-school chemistry students finding
  256. ChemBalance a lot of fun to play with.
  257.