You are on page 1of 15

SLAC Computation Group CGTM-139

CREME -- A Program for

Balancing Chemical Equations

by

Stephen N. Au

September 15, 1972


CHEME

I. Introduction

On October, 1968 George Copleyl wrote an article on balancing


chemical equations 'by using matrix algebra. This article aroused
my interest in using a computer to solve sYtitems of chemical
equations since Copley provided a simpler, me> re complete, and
more precise method to deal 1.-1ith the problem than the other-s2.
Although modifications have been made to simplify the calculations,
the basic m.athmatical set-up of the solution followed closely the
method described by Copley.

II. Math~_atical Background


An undcrstandine; and operation in reducing a ma.trix or solving
e. set of homogene o us equations is essential in balancing chemical
equations i~, this program. A discussion of solving a set of
homogeneous equations is discussed by Paul Shields 3 .
For purpose of illustration, three chemical systems are use~

as examples. The subBcrj.pt 'p' al"d 'r' stanc1s for product, and
reactant, respectively.
A. Consider a chemical system marIe up of .11.1 °
2 3
, }r-, H2 0 awl
Al+J. If we let a, b, c, anr'l d to repres e nt the stoichio-
1

metric factor of the species, we would have the following:


a A1 0 + b IV + c H 0 + d Al+3 =
2 3 2
°
Be'tlare that the above is only mathl!latically correct and is
chemically invalid. It would make sense in Chemistry, as
shOl'ln later, if some of the variables arc neEative and all of
the negative terms are being placed on the other side of the
equation.
By the conservation of matter and charge in a chemical equa-
tion, the follo",ing is deduced:
CHEME 2

Al Al or Al AlI' - 0
I' P P
H
I'
H
P
or H H =0
P I'
0 0 or 0 0 = 0
I' P P I'
Relating the above to the overall chem:i.cal equati.on, we
can set b + (as in H+) + 3d + (as in Al+ 3 ) = 0
2a Al (as in A1 0 ) + d Al (as in Al+3) 0
2 3
b H (as in tr · )
l
+ 2c H (as in H O) = 0
2
3a 0 (as in A1 0 ) + c 0 (as in }~O) 0
2 3
The positive si gn is used in here ot her than the negative
sign because a, b, c, and d can take in negative values
as well. Note that + denotes the positive charge.
Expressing in matrix form gives:

A_120 3
}[I· H2 O Al+3
Al 2 0 0 1 a 0
0 3 0 1 0 b 0
==
H 0 1 2 0 c 0
+ 0 3 1 d 0 0
It can be seen that the vertical column refe:cs to the
partic ular compound and the hori.zontal I'm! gives the
number of the parti.culax element present in the compound.
For example, A120 has 2 Al, 3 0, 0 H, and 0 -I- If
3
the matrix multiplication is carrie d out, i.t is easily
seen that the matrix expression is just another way of
stating the algebraic equations. Actually, if the user
is not familiar with the matrix algebra, he can read i ly
solve the equations by solving a system of simu).taneous
equations.
Reducing the matrix gives
- ---; -

j]
1 0 0 1/2 a
0 1 0 3/2 b
0 0 1 -3 c
0 0 0 0 d
CHEME ·3

or .
200 I a o
o 2 0 6 b o
o 0 2-3 c o
o 0 0 0 d o
In other words)
2a == -d
2b 6d
2c = -3d
Letting d=2 and rearranging the negative terms to the
other side of the original overall equation) a balanced
chemical equation is thus obtained;

B. As another example, examine the follOl-ling system;


a 10 - + b ~In+2 + c H 0 + d Mn04 + e 10 - + f H+ == 0
4 2 3
At the first glance) i t may be tempting to set up
the matrix as shOl'!D belml;

10 - Mn+ 2 Hn04 H 0 10 - H+
4 2 3
I 1 000 I o a o
o 4 o I 3 o b o
I o o o I o c = 0
H 0 o 2 o o I d o
Mn 0 I o I o o e o
+ 0 2 o o o I f o

However, a second thought i'lOuld reft'te the above


because of the fact that posjtive charge and negative
charge may not be conserved in the reaction; although
it is true that the net charge is conserved. To
solve the problem, ,-le have to use the net charge in
the equation, namely that
(net charge)
== (net charge)
r p
(positive charge + negative charge)r
== (positive charge + negative charge )
p
CREME 4

or
((posiUve charge) - (positive charge) )
p I'
+ ((ne gative charge) - (negative charge) ) = 0
P I'
and vic e versa. In terms of the stoichiometric coeffi-
cient matrix, this means that
rOItT
I'm', = 0
+
since both'positive charge and negative charge are
represented by positive nillnbers. Follm'Ting the discussion,
th e system should be written as
10 - Mn+2 H O Mn04 10 - H+
4 2 3
1 1 0 0 0 1 0 a 0
0 L~ 0 1 4 3 0 b 0
-1 2

~~
E 0 0 -1 1 c
H 0 0 2 0 0 1 d
Mn 0 1 0 1 0 0 e
f
_I
where <: is the net. charge of the compound. A more con-
venient ''lay to interpret E is to follml the convention
that positive number represent positive charge ,·,hile
negative. charge are represented by negative numbers.
Reducing , we can have
''-- 1
1 0 0 0 0 5/ 6 a 0
0 1 0 0 0 1/3 b o
0 0 1 0 0 1/2 c = o
0 0 0 1 0 -1/3 d o
0 0 0 0 1 -5/6 e o
f
or equivalent ly, .1

6 0 0 0 0 5 a
- o
0 6 0 0 0 2 b o
0 0 6 0 0 3 c o
0 0 0 6 0 -2 d o
0 0 0 0 6 -5 e o
f ,l
--1
CHEME 5

Setting f to be 6, we can read off the solution as


a = -5
b = -2
c = 3
d = 2

e = 5
Referring back to the overall equation, the foll.oi-ling
is obtained;
5 10 -
3

C. The above examples can be think of as a one-variable-


-dependence system (vocaJ...ity=l) because the equation can
be solved. if the numerical value of one of the variables
is known. In this example, 8. multi-variable.·dellendcnce
system (vocali ty greater than or equal to 2) is shmm
in which case the numerical values of more than one
variable must be knmm fO}.' the solution of the equations.
A system such as
a e- + t H+ + cO
2
+ d H 0
2
+ e 0 o
3
gives the follovling set-up.
e H+ O
2
}~O

:1~U~
1 o o
1 o 2 o
o 2 1 3
dl
!
ei
__ I I

After l'educting, v7e have


1 0 020
-. a
o 1 020
o 0 1 1/2 3/2 c
d

e
CHEME 6

Converting to integers) we have

~~~~; :-~m
d
e
This means ·that
2a -I- 4d -- °
2b -I- 4d == °
2c + d -I- 3e == °
As Can be seen) th e equation can be solved only when
the numerical value of tvlo of the variable s is knOlofD.
In thi s case) it is easier to sub sti tute foi.' d and
e. By letting d to be 1 and e to be 1) v1e have
2 e - + 2 H+ -I- 202 == H 0 + 03
2
since a - -2
b == -2
c == -2
Another possjble substitution vlould be d=-l and e 1
vlhich leads to
a == 2
b =2
c = -1
The balanced equation for this set is
==
By varying d and e) v1e can have e ntirely different
chemical equations (and many others more !;:). liTith
this kind of system) proper choice of substitution is
essential to the solution of the problem.
CHEME 7

III. Programming Aspect of Crlli~£

CHEHE is a pL/I coded program. It was initiated in the


summer of 1971 and got undervTaY in January) 1972. Roughly
speakir:g) CHEHE can be divided into two parts. The first part
interprets the input and the second part performs the necessary
computations.
A. Input. CHE,ME reads in input cards. Only 72 colwnns
out of the 80 cohlJDnS are read by CHEME (i. e. col 1 to
coL 72). The input is the empirical formula) \'rritten
in a special notation) of all the involved chernical
species. 'l'he special not.ation consj sts of
denotes linkage behTeen element.s as they are
in the same compound) and
* denotes subscript of the element.
Parenthes e s can also b e use r) together with th e chemical
formulas. For example)
Chemi.stry Notation Input Notatio12
H 0 H-*2=0
2
CO C==ox-2
2
H H
, +
H H==+
OH O=H=-
A1 0 AL-x2=0~-3
2 3
CH3NII2 C=H*3==N=H'x-2
HNNN H~N:=::N=N (or II=N-x-3)
(NH2 )2 CO (N=H*2)-l<2=C:=:0
(CH3)2CHCOOH (C==H-*3) -);2=C=H=C=0==0==H
(Pt(mI3)6)Cl!~ (PT== (N==H-"'3 )-:+6 )=cL-x-4
Blanks) comma) and semicolon can be used to seperate
chemical formulas from one another. Different sets of
chemical system can be solved in the same run by placing
a dollar sign in column 72 behTeen dU'ferent sets and
starting the next set in the follm'ling card (only 72
columns are used.) It is not necessary nor advisa.ble
to place the dollar sign in coh1olln 72 ..Then the last set
is reached or else the program attempt s to r e ad in
CHEME 8

another set of data \'lhich Hould result in the raisin£;


of an error condition (i.e. end of file).
It is important to note that each formula can be \'1ri tten
only once in the same set. As the program stands nov1)
it can not differentiate between products and reactants
\o1hile reading in the input, and such kind of information
is not accepted by the program. The difference beh1een
products and reactants is determined by the user aft.er
the program runs because the program merely solve the
mathrnatical portion of the problern) in which case the
reacta.nt s and the products are rnathmati cal equivalent
in thi s program.

B. Output. There are tr,w output formats for the balanced


equation depending on the solut.ion of the problem.
Simulation of' the computer out.puts arc shmm belOl'I.
1. One-variable-dependence system. 'fhe chemical
system in Example 11. is used and the follO'lling
outp~t is produced.

REACTANTS PRODUCTS
2 AL=+""j 1 AL*2,-=0.y' 3

3 H*2:=:0 6 H=+

For Examp Ie B) the output looks l i kc :

REACTANTS PRODUCTS
2 MN=0*4=- 5 1=0·*4==-

5 1=0*3=- 2 Mllj"",+~2

6 H:=:+ 3 H-*2::0
As stressed before) the term 'reactants ·' and
'products' are used loosely and may not have any
real ~ignificance in the Chemistry sense . It is
obviou$ that the balanced chemical equation can
easily be deduced from the output.
CHEME 9

2. Multi-variable-dcpendence system. The output for


this system is not so obvious as that one before.
Simple algebraic manipulation by the user is needed
in order to obtain the desired balanced equation.
A sample of the output for Example C is given below:

srrOICHIOM1~TRIC FACTOR CllliK( CArJ SPECIES


+ 4D 2A
+ ~-D - 2B H=t
+ lD + 3E - 2C 0*2
lD lD . H~c2=-=0

lE lE 0*3
The sett, B, C, D, ••••. , Ttl, X, Y, z} stands for
any algebr8.:i.c variable and ...,ith proper choice of
munerical substitution for the variables, one can
set the specic either to be the reactant or the
product. Reactants are represented by negative
numbers "lhile products are rep:C'csented by positve
nurnbers in this program. The user can reverse the
interpretation as he sees fit.
From. the output, an ec~ui valence is implied between
the STOICHIOI,f~TRIC FAC'l'OR and the coefficient of
the CHEI,nCAL SPECIES, i. e.
+ 4D 2A
+ 4D 2B
+lD + 3E 2C
lD 1])

lE = lE

Substituting for D and E as in Example C lead to the


soltl.tion of A, B, and C. Aftc:c the muneric8_1 solution
of the variables has been knovTn, the user can easily
deduce the chemical equation. Remember that the
overall equation is "'lritten jon the form of
A X
n n = 0
CHElvIE 10

where A is the stoichiometric factor,


X is the involved chemical species, and
N is the number of chemical species.

There are some cases in '\'Thich the chemical equation can


not be balanced. In that situation, a messar;e
'H,xNO CHEMICAL EQUATION IS POSSIBLEH-:t!
is printed out. FollO'\'ling the print out of the balanced
equation, the molecular weiGhts of the formulas are also
available.

C. structure. This section is prepared for people 1'1110

are interested in the program struct.ure of CHEME and


may not be of interest to the general users.
Roughly speaking, CHEr.iE can be di vicled into b10

parts. The first part intel'prets the input and the


second pa.rt performs the necessary computations. A
simplified overall flovlchart of CHEl-j}~ can be seen in
Fig. 1.
There are eight sub:cout i ne s in CHElvill. Their
functions are briefly described below.
1. REDUS REDUS reduces a given matrix to its
reduced form. The subroutine helps in solving
a set of homogeneous equations and constitutes
the main backbone in computation.
2. 01:JrPUT This is a t"l-lO-dimensional matrix
output rOlJ.tine. It is used in con;junction
with REDUS.
3. PROPO'l'. This block converts the stoichiometric
coefficient matrix (in a one-dimensional linear
vector form) to an integer set. The accuracy
desired in this case is +/ - .02 (e. g. l~. 98
and 5.02 vlould both be intcrFceted as 5).
CHEtvIE 11

4. DATA This procedure reads in the input as charac-


ter strings · and determines compound formulas i'Thich
are seperated from each other by blanks, a comma,
or a semicolon.
5. SlTBSIP SlTBSIP determines the subscript of each
element in a compolmd formula. Thi s procedure
contributes to the set-up of the stoichiometric
coefficient matrix.
6. PAREN PAREN is a parenthesis scanner which picks
out the locations of parentheses in the strin~.

After the locations s.re found, matching is performed


to pair up the left and right parentheses. It
is used together "Ti tn SlTBSIP to determine paren-
thesized elements.
7. ELEHE:N From a given compound forInule., ELE!tlEN
finds out all the elements, including the charge,
that make up the compound.
8. LENG LENG is a ' length' function "lhich determines
the 'length' of a given string. Length in this
prog~am refers to the number of 'non-blank'
characters, including imbedded blanks and excluding
trailing blanks, assigned to a g:iven string. It
is used in SlTBSIP to determine the length of' the
character string vlhich represent the element.

D. Limitation. There are several major limitations within


this program. ~l.'hey are listed below.
L The program does not check for valid chemical formulas
nor equations thus produced can be evo.luated as
chemically valid.
2. The program does not check for valid input i'lhich
leaves the program 'unprotected' to mistakes marIe
by users.
CHEME 12

E. Conclusion. A progrmn has been produced vlhich can be usen


to balance chemical equations. It can be used to balance
all types of equations incl1.Jding redox, aCid-base, and many
others as well. It is hoped that this program can give
students in Chemistry a better understanding of the relations
betvTeen Chemistry, Mathmatics, and Computer Science.
CHErviE 13

F(XYCNOTES
1
George Novello Copley, 'Linear Alegbra of Chemical Formulas
and Equations', Chemistrx, Vol In, No 9, October, 1968.

2There are some other methods in solving the problem, like using
minors. But they tend to be quite tedious and are not suitable for
computer programming. These methods "Jere released by Chemistry.

3paul C. Shields, Elementary Linear Algebr,:, pp. 1-12, NOI"


York, Worth Publishers, inc., 1970:
CHEME 14

CHEM8

FIG. 1. OVERALL
- 7 - - -. ClLLL~
DATA 1'0
SIMPLU'IED
FLOH'CHART
READ IN FOR
L....L_Il'_.fP(JJ' CHE1,rE

--±---]'
CAr.r.s
ELF.i·2H TO , .
PICK OUT
ELEt·i2NTS _

~--t---
SETS UP \
STO}CHIOME'l'l-nC
COEF?1 CIEm'
HATRIX
J
---\li-------
J
rn
AT~LS
REDUS TO
REDUCE
MlI.l'HIX
-r;---

.~:~ia~ ~~EGJ
U10NVERS:~9~J
t

~
tOMPUTES
l'iOIECULAR
-\'TEIGH'l'S

f
OUTPUTS BAL-
Ar.rCED
J
EQUATION

otJl.' pur s
NOIECULA.R
I,/EIGIiTS

NO
- - ---- <---- 'l':T.S
COHI?r..:r~'l' t,
./
"'-/

You might also like