Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Balance Chemistry Equations

Lore:
Now that my chemistry class has gone past memorizing equations and whatnot and begun with, example, balancing chemical equations. I could sit down all day long balancing equations, but as programming is my passion I would love to get a program working for me to solve these. This is more or less a pet-project and more for fun rather than giving me an edge in chemistry class. But the more I dwelt into it the more complex it became. I don't really know how to begin this crusade and have instead worked on the parser and data set, which is in pretty good set to have my head wrapped around it right.

Question:
What I don't know is how to utilize matrices to solve equations (balance equations to conserve mass*) and convert that into whole numbers valid in chemistry.

Code/objects:

class Element {
    constructor(name,quantity) {
        this.name = name;
        this.quantity = quantity;
        if (this.quantity == 0) {
            this.quantity = 1;
        }
    }
}
class Molecule {
    constructor() {
        this.elements = [];
        this.multiplier = 1;
    }
    addElement(newEl) {
        this.elements.push(newEl);
    }
    list() {
        this.elements.forEach(el => {
            console.log(el.name,el.quantity);
        });
    }
    getMultiplier() {
        return this.multiplier;
    }
    getElements() {
        var a = [];
        this.elements.forEach(el => {
            a.push([el.name,el.quantity*this.multiplier]);
        });
        return a;
    }
}

Code/data structure:

printFormula(moleculeList);
for (var i=0;i<moleculeList[0].length;i++) {
    console.log("Mol "+(i+1))
    moleculeList[0][i].list();
}
console.log("==>");
for (var i=0;i<moleculeList[1].length;i++) {
    console.log("Mol "+(i+1))
    moleculeList[1][i].list();
}

Code/output:

'C6H14 + O2 ==> CO2 + H2O'
Mol 1
C 6
H 14
Mol 2
O 2
==>
Mol 1
C 1
O 2
Mol 2
H 2
O 1
like image 407
Simon Avatar asked Oct 20 '25 14:10

Simon


1 Answers

I am a chemist and have exactly zero familiarity with Javascript so as to explain it in detail. But the method is fairly simple to show by hand. This link nails it down in a clean manner.

The only reason why I am linking to it without going through the steps in detail is because Stackoverflow does not have LaTeX math in order to make the text easy to follow, so I think you'll benefit better from the text in the link. Also, I don't think this is a full answer, but I do not have enough reputation to put this on a comment, where it belongs.

I think the hardest bit is finding/coding the appropriate subroutine for row-echelon reduction of the matrix in step-5. But I also believe that there is a vast number of algorithms appropriate for doing that which you can find in the internet. Very handily, this very website has a question related to just that with a python code to sweeten the deal.

I hope this helps you at least wrap your head around it enough to implement the code you need.

Cheers

like image 133
urquiza Avatar answered Oct 22 '25 02:10

urquiza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!