A Python program that balances chemical equations by determining the correct stoichiometric coefficients for each reactant and product.
- Accepts chemical equations in a familiar format (e.g.
Fe3O4 + HNO3 -> Fe(NO3)2 + Fe(NO3)3 + H2O
). - Decomposes compounds into individual elements and their respective counts.
- Correctly processes compounds with parentheses (
()
) and brackets ([]
) to account for nested groups and their multipliers.
Chemical equation balancing can be formulated as a system of linear equations, which can be solved using matrix algebra. The process involves the following steps:
- Equation Representation as a Matrix
Each chemical equation is parsed and then represented as a coefficient matrix where:
-
Rows represent elements (e.g.
$Fe$ ,$O$ ,$H$ , etc.). - Columns represent compounds (reactants and products).
- Each entry represents the number of atoms of a given element in a compound.
- Example:
The matrix is set up by counting atoms of each elements:
Element | Fe3O4 | HNO3 | Fe(NO3)2 | Fe(NO3)3 | H2O |
---|---|---|---|---|---|
Fe | 3 | 0 | -1 | -1 | 0 |
O | 4 | 3 | -6 | -9 | -1 |
H | 0 | 1 | 0 | 0 | -2 |
N | 0 | 1 | -2 | -3 | 0 |
- Gaussian Elimination
The table above forms a system of linear equations, expressed as a matrix equation which needs solving:
where A is the coefficient matrix, and x is the vector of unknown stoichiometric coefficients.
Since the equation is homogeneous (
Gaussian elimination is applied to transform A into an RREF matrix, where:
- All rows having only zero entries are at the bottom.
- The leading entry (the left-most nonzero entry) of every nonzero row, called the pivot, is on the right of the leading entry of every row above.
- The leading entry in each nonzero row is 1 (called a leading one).
- Each column containing a leading one has zeros in all its other entries.
After row reduction, the matrix should have a form of:
In this case, the coefficient matrix should be:
Since the system is underdetermined (more variables than equations), some variables have to be expressed in terms of others:
A free variable is then chosen (e.g.
Since coefficients cannot be in fractions or decimals, they need scaling by multiplying them by the least common multiple (LCM) of denominators to obtain the smallest integer solution.
In this case, the solution from step 3 is:
we multiply by 4 to get:
which are the final stoichiometric coefficients, resulting in the balanced equation:
The following examples illustrate chemical equations that have been balanced using the algorithm:
-
Equation:
$C_{57}H_{110}O_6 + O_2 \rightarrow CO_2 + H_2O$ Solution:
$2C_{57}H_{110}O_6 + 163O_2 \rightarrow 114CO_2 + 110H_2O$ -
Equation:
$C_{12}H_{22}O_{11} + KNO_3 \rightarrow K_2CO3 + N_2 + CO_2 + H_2O$ Solution:
$5C_{12}H_{22}O_{11} + 48KNO_3 \rightarrow 24K_2CO3 + 24N_2 + 36CO_2 + 55H_2O$ -
Equation:
$K_4[Fe(SCN)_6] + K_2Cr_2O_7 + H_2SO_4 \rightarrow Fe_2(SO_4)_3 + Cr_2(SO4)_3 + CO_2 + H_2O + K_2SO_4 + KNO_3$ Solution:
$6K_4[Fe(SCN)_6] + 97K_2Cr_2O_7 + 355H_2SO_4 \rightarrow 3Fe_2(SO_4)_3 + 97Cr_2(SO4)_3 + 36CO_2 + 355H_2O + 91K_2SO_4 + 36KNO_3$
This project is a collaboration between: