Trương Anh
Trương Anh

Reputation: 155

Maple, chemical equation balancing

input is a chemical equation like that

aKMnO4 + bHCl = cKCl + dMnCl2 + eH2O + fCl2
// a, b, c, d, e, f are numbers.

I tried to solve this problem by solve some equations:

a = c // K
a = d // Mn
4*a = e // O
b = e // H
b = c + 2*d + 2*f // Cl

It works, but i have to enter these equations as a input in my code.

Is there any way to find a, b, c, d, e, f just by the chemical equation input?

Upvotes: 1

Views: 462

Answers (1)

John M
John M

Reputation: 295

You can do this, but you need to rewrite your chemical equation with + and * in the compounds so H2O becomes H*2+O e.g. Your equation

a KMnO4 + b HCl = c KCl + d MnCl2 + e H2O + f Cl2

here becomes:

eq:=a*(K+Mn+O*4) + b*(H+Cl) = c*(K+Cl) + d*(Mn+Cl*2) + e*(H*2+O) + f*(Cl*2);

They you can get equations for a-f by equating coefficients:

elems := [ K, Mn, O, H, Cl ];
eqr := collect(expand(rhs(eq)), elems);
eql := collect(expand(lhs(eq)), elems);
eqs := zip(`=`, map2(coeff, eql, elems), map2(coeff, eqr, elems));

It would be an interesting project to build something that could recognize the chemical symbols and parse them more automatically into equations.

Upvotes: 1

Related Questions