Skip to content

A Python program that balances chemical equations by determining the correct stoichiometric coefficients for each reactant and product.

Notifications You must be signed in to change notification settings

khangvum/chemicalbalancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Chemical Equation Balancer

A Python program that balances chemical equations by determining the correct stoichiometric coefficients for each reactant and product.

Features

  • 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.

Matrix Algebra for Balancing Equations

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:

  1. 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:

$$ Fe_3O_4 + HNO_3 \rightarrow Fe(NO_3)_2 + Fe(NO_3)_3 + H_2O $$

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
  1. Gaussian Elimination

The table above forms a system of linear equations, expressed as a matrix equation which needs solving:

$$ Ax = 0 $$

where A is the coefficient matrix, and x is the vector of unknown stoichiometric coefficients.

Step 1: Convert to Augmented Matrix Form

Since the equation is homogeneous ($Ax = 0$), a zero column vector is appended to the matrix:

$$\begin{bmatrix} 3 & 0 & -1 & -1 & 0 \\\ 4 & 3 & -6 & -9 & -1 \\\ 0 & 1 & 0 & 0 & -2 \\\ 0 & 1 & -2 & -3 & 0 \end{bmatrix} \begin{bmatrix} x_1 \\\ x_2 \\\ x_3 \\\ x_4 \\\ x_5 \end{bmatrix} = \begin{bmatrix} 0 \\\ 0 \\\ 0 \\\ 0 \end{bmatrix}$$

Step 2: Transform to Reduced Row Echelon Form (RRRF)

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:

$$ \begin{bmatrix} 1 & 0 & 0 & 0 & b_1 \\ 0 & 1 & 0 & 0 & b_2 \\ 0 & 0 & 1 & 0 & b_3 \\ 0 & 0 & 0 & 1 & b_4 \end{bmatrix} $$

In this case, the coefficient matrix should be:

$$ \begin{bmatrix} 1 & 0 & 0 & 0 & -\frac{1}{4} \\ 0 & 1 & 0 & 0 & -2 \\ 0 & 0 & 1 & 0 & -\frac{1}{4} \\ 0 & 0 & 0 & 1 & -\frac{1}{2} \end{bmatrix} $$

Step 3: Extract the Solution

Since the system is underdetermined (more variables than equations), some variables have to be expressed in terms of others:

$$ x_1 = \frac{1}{4} x_5, \quad x_2 = 2 x_5, \quad x_3 = \frac{1}{4} x_5, \quad x_4 = \frac{1}{2} x_5 $$

A free variable is then chosen (e.g. $x_5 = 1$) and substitute back to solve for integer values for all coefficients.

Step 4: Integer Scaling

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:

$$ \left(\frac{1}{4}, 2, \frac{1}{4}, \frac{1}{2}, 1\right) $$

we multiply by 4 to get:

$$ \left(1,8,1,2,4\right) $$

which are the final stoichiometric coefficients, resulting in the balanced equation:

$$ Fe_3O_4 + 8HNO_3 \rightarrow Fe(NO_3)_2 + 2Fe(NO_3)_3 + 4H_2O $$

Demonstration

The following examples illustrate chemical equations that have been balanced using the algorithm:

  1. 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$

  2. 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$

  3. 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$

Collaboration

This project is a collaboration between:

About

A Python program that balances chemical equations by determining the correct stoichiometric coefficients for each reactant and product.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •