Chemical Equation Balancer

Insert your chemical equation here

How to use:

The input must be a chemical equation written in one of these forms:

  • A + B = C + D (double exchange reaction)
  • A = B + C + Dn… (decomposition reaction)
  • A + B = C (synthesis reaction)
  • A + B + Cn… = D + E + Fn… (general chemical reaction)
  • A^-x + B = C^-x + D (single replacement)
  • A^-x = B + e^- (oxydation reaction)
  • A^+x + e^- = B (reduction reaction)

You can play aroud balancing the following chemical equations:

These are only examples, you can insert your own equations with a correct syntax.

  • Ca(NO3)2 + KCl = KNO3 + CaCl2
  • S + O2 = SO3
  • Fe + Cl2 = FeCl3
  • KMnO4 + HCl = KCl + MnCl2 + H2O + Cl2
  • KNO3 = KNO2 + O2
  • K4Fe(CN)6 + KMnO4 + H2SO4 = KHSO4 + Fe2(SO4)3 + MnSO4 + HNO3 + CO2 + H2O
  • CH4 + O2 = CO2 + H2O
  • Na2S2O3 + I2 = Na2S4O6 + NaI
  • CuSO4*5H2O + Fe = FeSO4 + H2O + Cu
  • FeS2 + O2 = Fe2O3 + SO2
  • NH4OH + H3PO4 = (NH4)3PO4 + H2O

Reactions with ions:

  • H2O = O2 + H^+ + e^-
  • Cr2O7^-2 + H^+ + e^- = Cr^+3 + H2O
  • Fe^3+ + I^- = Fe^2+ + I2
  • H2O + e^- = H2 + OH^-
  • Pb^2+ + e^- = Pb

Single replacement reactions:

  • S^-2 + I2 = I^- + S

You can also play with redox reactions:

  • Cl^- = Cl2 + e^-
  • H^+ + e^- = H2

Note: copy one of the equations and paste it inside the input above. Or insert an equation you want to balance. But note that some equations are not interpreted by this tool (read the next paragraph for more info about the limitations).

Syntax rules

  • Hydration groups must be written after a molecule and must start with asterisk. Then you have to specify the number of water molecules and insert the formula of water. (Hydration group correct form CuSO4*5H2O)
  • Electric charges must be inserted after this symbol: ^ (e.g. Mg^2+ / Cl^- / H^+). Other syntaxes are not supported.
  • Electrons must be inserted with this symbol e^-

Unfortunately simbols like MetOH (methanol), EtOH (ethanol) or Ph-OH / PhOH (phenol) are not recognized.

Notes and limitations:

The first version of this balancer didn’t support:

  • Hydration groups like CuSO4*5H2O
  • Electrons e^-
  • Anions and cations like S^-2 or Mg^+2

Now these syntaxes are supported, as you imagined reading the above equations I provided as examples.

The only limitation is the following one:

  • Initials like Ph-OH / PhOH (phenol), EtOH (ethanol) and MetOH (methanol) are not recognized

The next versions of this balancer will support more and more features.

How it works?

This equation balancer works thanks to an interpreter and a mathematical procedure that involves solving matrices.

Here there are all the steps involved during the balancing of an equation:

  1. First of all, the chemical equation is interpreted. The library, through a regular expression, will recognize each element / molecule involved in the reaction.
  2. Then a matrix is built (bidimensional array). Each row of the matrix is the single chemical element that we find along the reaction. For each molecule that we have, we count the number of atoms for that element. The matrix is necessary for the next step (a mathematical procedure called RREF).
  3. The matrix is then reduced in row echelon form thanks to the Math-PHP library used in this tool.
  4. From the solved matrix a system of equations is built. Solving this equation I’ll get all the coefficients for each molecule to balance the equation.

Note: Obviosly the real procedure made by this library is a bit more complicated and articulated. For simplicity I missed some crucial steps to make this toolkit work. I also missed the description of the mechanism of the code that builds a phrase from the balanced equation that now is not available.