New to MATLAB, trying to make elemental combinations for possible chemical reactions

2 views (last 30 days)
John
John on 30 Aug 2015
Answered: Tim Jackman on 1 Sep 2015
I have 4 base chemical elements and 5 possible products I can make from the elements.I want to combine all the elements and products in different combination and stoichiometric ratios to give me all the possible reactions. For example, if I have hydrogen, oxygen and carbon, I want the code to spit out H2O, CO2, CH3, ect, but I do not want to include CH3 because that is not one of the 5 possible products for consideration. Also I want the products to be able to be reactants, like CO2 losing an oxygen to make CO.
My idea was to write down the possible charges for the base elements, iterate them with different stoichiometric coefficient so the the sum of the charges are 0, then some kind of constraint so that it doesn't compute forever for the products that I am not looking for.
I have no idea on how to put this on MATLAB, help will be appreciated.

Answers (1)

Tim Jackman
Tim Jackman on 1 Sep 2015
If I understand your question correctly, you have 4 elements (e.g. C, N, O, and H) and you want to find all possible simple products (e.g. CH4 etc.) and not longer, more complicated molecules like ethanol or octane.
If this is correct, you should be able to use a for loop to check each combination without much concern for performance. The following code snippet will loop through and check the charge of each combination of elements. If the total charge sum is equal to zero AND if the total number of elements is more than one and less than or equal to five, then this code will record the number of each elements that can be used to make each product. The variable "out" contains the number of each element that corresponds to C, N, O, and H respectively.
elements = {'C','N','O','H'};
charge = [-4 -3 -2 1];
sizeprob = 5^4;
out = [];
for ii = 0:sizeprob-1
letternum = [mod(ii,5), mod(floor(ii/5),5), mod(floor(ii/25),5), ...
mod(floor(ii/125),5)];
chargesum = sum(letternum.*charge);
if chargesum == 0
if sum(letternum) <= 5 && sum(letternum) > 0
out(end+1,:) = letternum;
end
end
end
If you need to examine more complicated molecules with an unknown number of side chains, then this approach may not be suitable given the number of iterations that would be required. You would need to apply constraints to your problem to limit the number possibilities your script would need to check. In any case, this should hopefully give you a starting point for creating a script to solve your problem.
One additional note I have is that this code will not identify CO2, given that summing the ionic charge does not equal zero. For products such as these, you may need to incorporate additional checks in a similar manner to summing the charges. Hope this helps.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by