Sciencemadness Discussion Board
Not logged in [Login ]
Go To Bottom

Printable Version  
Author: Subject: Stoichiometry calculator help
chempyre235
Hazard to Self
**




Posts: 56
Registered: 21-10-2024
Location: Between Niobium and Technetium
Member Is Offline


[*] posted on 7-3-2025 at 08:25
Stoichiometry calculator help


I'm trying to make an intuitive calculator in Excel for stoichiometric ratios, while also producing MW and enthalpy, but I'm having a little trouble figuring formulas for more complex reactions.

Na2SO4 + CaCl2 --> CaSO4 + NaCl

A reaction like this will be fairly easy, I just search each reactant and product for symbols that match those in the periodic table, then find the quantities for each element based on the following text in the string. Then, all that's left to do is find the LCMs of each element, which gives me the total number of atoms of each element on each side of the equation:

  • Na = 2
  • S = 1
  • O = 4
  • Ca = 1
  • Cl = 2

Then I'll divide those values by the number of each element in each compound (e.g., Na2SO4 has 2 sodium atoms, so the LCM of 2 gets divided by 2). This should give me the molar values of each compound.

The issue arises for multiple compounds using the same elements. See the decomposition of gunpowder, for example:

KNO3 + S8 + C --> CO2 + SO2 + K2CO3

How would I set this up to solve? Any suggestions would be much appreciated.
View user's profile View All Posts By User
chempyre235
Hazard to Self
**




Posts: 56
Registered: 21-10-2024
Location: Between Niobium and Technetium
Member Is Offline


[*] posted on 7-3-2025 at 14:03


I don't think I made myself very clear. I was not asking for the stoichiometry for that problem in particular, but was stumped at developing formulas for a spreadsheet that permits auto calculation of stoichiometric ratios based on the inputs of reactants and products.

Thanks for the help, though.

P.S. This is not a homework problem. I've made several complex calculators and contraptions in Excel, including a modular search engine for my coin collection, a bit calculator, and a program that draws acyclic, branched, saturated or unsaturated hydrocarbons by typing the name. :D

P.P.S. Haha! I didn't even see that I'd left out the nitrogen! Good catch!;)

[Edited on 3/7/2025 by chempyre235]
View user's profile View All Posts By User
Sulaiman
International Hazard
*****




Posts: 3779
Registered: 8-2-2015
Member Is Offline


[*] posted on 7-3-2025 at 14:08


sorry, I deleted my post to edit it, and you replied too quickly for me.



CAUTION : Hobby Chemist, not Professional or even Amateur
View user's profile View All Posts By User
j_sum1
Administrator
********




Posts: 6371
Registered: 4-10-2014
Location: At home
Member Is Offline

Mood: Most of the ducks are in a row

[*] posted on 7-3-2025 at 16:31


Well, if you are doing this for fun, then have at it.
It has been done before: you may find ideas here.
https://www.sciencemadness.org/whisper/viewthread.php?tid=79...

If you just want to balance equations, then the link I posted in that thread works well. Probably there are much more sophisticated solvers available by now.

https://www.webqc.org/balance.php
View user's profile View All Posts By User
woelen
Super Administrator
*********




Posts: 8079
Registered: 20-8-2005
Location: Netherlands
Member Is Offline

Mood: interested

[*] posted on 8-3-2025 at 11:52


This is not a trivial problem, unless you have some knowledge of linear algebra.

The problem of solving your chemical equation can be converted to a homogeneous linear equation of the form Ax = 0, where x is a vector of coefficients for the different reagents (molecules, ions, or electric charges) and A is a matrix, depending on the precise formulae of the different reagents and products. When you have this equation, then the next step is to find the null-space of A. That is the complete set of elements of x, so that A times x is a zero-vector. For a well-behaved uniquely determined reaction, this null-space is one-dimensional and you have solved your problem, up to some constant factor. But for some equations, the result is higher-dimensional. In that case, there are multiple reactions, which can occur in any ratio and not a single reaction equation can be given.

An example of the first one is: a C4H10 + b O2 --> c CO2 + d H2O
Here the vector x is [a b c d].

An example of a reaction with a 2-dimensional null-space is the following: a Cu + b HNO3 --> c Cu(NO3)2 + d H2O + e NO + f NO2
Here the vector x is [a b c d e f].
It is not possible to write the reaction equation for this uniquely up to some factor.
The reaction could only give NO (for very dilute HNO3), or only NO2 (for hot and highly concentrated HNO3), or anywehre between these extremes.

Have a look at this: https://woelen.homescience.net/chemeq/
Also click the help button to get additional background info, which demonstrates that the general problem is not that easy.
I wrote code for determining the null-space of a matrix, and a web application around that for the equation solver.
Try the reaction with copper and nitric acid for yourself in my web application, then you'll understand what I mean here.

There are even reactions, with a null-space of dimension more than 2.

There also are reactions, with a null-space of dimension 0. The only solution of x is the trivial vector [0 0 0 ... 0].

An example of such a reaction is the following:

a PCl3 + b H2O --> c POCl3 + d HCl

This reaction cannot occur, simply because mathematically it is not possible. No knowledge of chemistry is needed to show that this reaction is impossible.

Compare this with a PCl5 + b H2O --> c POCl3 + d HCl.

The latter reaction mathematically is possible (and in reality it indeed occurs).

[Edited on 8-3-25 by woelen]




The art of wondering makes life worth living...
Want to wonder? Look at https://woelen.homescience.net
View user's profile Visit user's homepage View All Posts By User
Rainwater
National Hazard
****




Posts: 986
Registered: 22-12-2021
Member Is Offline

Mood: Break'n glass & kick'n a's

[*] posted on 8-3-2025 at 17:20


I have to disagree with woelen, i think an easier method would be the use of a power
set algorithmn, this would require phrasing the chemical formula into an element
list, then sorting that list into an STD container like a multi map, or a new one ive
been playing with, the mdspan.

Doing something like that is beyond what excel was designed for.
If sticking with excel is a requirement,
i recommend looking into VBscrips. You can do a lot more there.
This is a multi part question, so first, your going to have to break the formula down
into its elements, then list every possible permutation. You will have to set up limits
to iterations and catch deadlock loops, but once you have a list of balanced
equations, you can begin weeding out duplicates and begin the next step which
would be determining if the reaction is spontaneous.

So rewinding a little, by phasing each element in the formula into a data structure
that can be used as a power set you can quickly
generate all these combinations , but their will be duplicates that you will need to
remove.

Now that you have a list, you will have to descide how to determine if a formula is spontaneous or now.

Good luck, you got one big task on your hands.

[Edited on 9-3-2025 by Rainwater]




"You can't do that" - challenge accepted
View user's profile View All Posts By User
chempyre235
Hazard to Self
**




Posts: 56
Registered: 21-10-2024
Location: Between Niobium and Technetium
Member Is Offline


[*] posted on 10-3-2025 at 20:02


Thanks for all the input! I kind of figured that this project would likely require some kind of matrix setup, but I was hoping otherwise. I now realize that this is more of an undertaking than I initially thought. I might still try it at some point, but not this week. Too much going on for that!
View user's profile View All Posts By User
woelen
Super Administrator
*********




Posts: 8079
Registered: 20-8-2005
Location: Netherlands
Member Is Offline

Mood: interested

[*] posted on 11-3-2025 at 10:42


@Rainwater: I do not believe that your method is a sure fire technique. It's behavior sounds highly unpredicatable to me, and it sounds more like trial and error.

The easiest method I think still is just solving an homegeneous equation of the form Ax = 0. This can be done with standard techniques and lots of code is available on the internet for this in many programming languages.

I myself wrote a Java-program, which solves the matrix equation and I wrote some code around it for parsing formulae (also things like K4Fe(CN)6.3H2O), which breaks this down to a vector with multiupliers for each element and charge in the formula. On top of that, I added a web application, using standard servlet technology and this leads to the web application, to which I provided a link in my previous post.

I am in the process of making all my hobby code available through my website, and this will include the complete Java code for solving chemical equations. You then can write your own application around it.




The art of wondering makes life worth living...
Want to wonder? Look at https://woelen.homescience.net
View user's profile Visit user's homepage View All Posts By User
Rainwater
National Hazard
****




Posts: 986
Registered: 22-12-2021
Member Is Offline

Mood: Break'n glass & kick'n a's

[*] posted on 12-3-2025 at 13:26


Quote: Originally posted by woelen  
Its behavior sounds highly unpredicatable to me, and it sounds more like trial and error.

Yep, but not just a normal trial, but a methodical one, exploring every possible combination.
For example, the combustion of hydrogen and oxygen gas.
For the most part produces water, but traces of ozone and hydrogen peroxide1.
Reaction conditions are not mentioned in the original post, but are worth exploring if software is doing all the work. STP being the default goto but should not limit the search.
I can not remimber the groups/guys names, but they discovered more planets than anyone. But not the first extertrestial planet. They missed out on that discovery because they rejected short orbital periods as a possibility. After the first planet was discovered to have a very short orbital period, they changed their computer algorithm, reprocessed decades worth of data, and discovered 100s of planets, most if not all where later confirmed.

I try to apply this type of logic to everything, usually makes things over complicated but has its uses

[1] https://www.nature.com/articles/157266a0




"You can't do that" - challenge accepted
View user's profile View All Posts By User
woelen
Super Administrator
*********




Posts: 8079
Registered: 20-8-2005
Location: Netherlands
Member Is Offline

Mood: interested

[*] posted on 13-3-2025 at 01:47


What I did in my software is purely mathematical. It just balances equations, whether the reaction is actually possible or not, is not in the scope of the tool.

E.g. If I feed the tool with a H2 + b O2 --> c H2O5, then it comes up with a = 2, b = 5, c = 2. This reaction is not possible from a chemical point of view, but purely mathematically, this is a valid balancing of the equation.

So, my tool is not a replacement of chemical knowledge, it just is a tool to automate tedious and error prone calculation of balanced equations, once the input reagents and output compounds are known.




The art of wondering makes life worth living...
Want to wonder? Look at https://woelen.homescience.net
View user's profile Visit user's homepage View All Posts By User
j_sum1
Administrator
********




Posts: 6371
Registered: 4-10-2014
Location: At home
Member Is Offline

Mood: Most of the ducks are in a row

[*] posted on 13-3-2025 at 02:06


I am going to reiterate the link I posted upthread.
This balances equations quite reliably.
It also gives mass quantities so it speeds up practical calculations.
It takes a variety of inputs including common chem names is words.
And, for at least some reactions, it requires input only: it figures out likely products for you.

I am not saying it is foolproof. I am saying I had to stop givjng the link to my students because they were bypassing learning stoichiometry skills.

To the OP, this may well be a worthwhile enterprise for you.
Or, you may just be reinventing the wheel.
View user's profile View All Posts By User

  Go To Top