Code covered by the BSD License

# Stoichiometry

### Antonio Trujillo-Ortiz (view profile)

24 Nov 2006 (Updated )

Calculation of the quantities of reactants and products in a chemical reaction.

stoichiometry(varargin);
```function [C] = stoichiometry(varargin);
%STOICHIOMETRY Calculation of the quantities of reactants and products in a
%chemical reaction.
% -----WARNING: To apply this procedure it is strictly necessary to know
%      a priori the chemical species (reactants & products) that participate
%      in the reaction.------
% Stoichiometry derives from the Greek words stoikheion (element) and metria
% (measure, from metron). In ancient Greek, the word Stoichiometria was used
% by Nicephorus to refer to the number of line counts of the canonical books
% of the New Testament and some of the Apocrypha.
% Stoichiometry rests upon the law of conservation of mass, the law of definite
% proportions (i.e., the law of constant composition) and the law of multiple
% proportions. In general, chemical reactions will combine in definite ratios
% of chemicals. Since matter cannot be created or destroyed, the amount of each
% element must be the same throughout the overall reaction. For example, the
% amount of element X on the reactant side must equal the amount of element X
% on the product side. It is the calculation of quantitative (measurable)
% relationships of the reactants and products in chemical reactions (chemical
% equations).
% A chemical equation describes what happens in a chemical reaction. The equation
% identifies the reactants (starting materials) and products (resulting substance),
% the formulas of the participants, the phases of the participants (solid,
% liquid, gas), and the amount of each substance. Balancing a chemical equation
% refers to establishing the mathematical relationship between the quantity of
% reactants and products. The quantities are expressed as grams or moles.
% It takes practice to be able to write balanced equations. There are essentially
% three steps to the process:
% 1. Write the unbalanced equation.
%    - Chemical formulas of reactants are listed on the lefthand side of the
%      equation.
%    - Products are listed on the righthand side of the equation.
%    - Reactants and products are separated by putting an arrow between them
%      to show the direction of the reaction. Reactions at equilibrium will
%      have arrows facing both directions.
% 2. Balance the equation.
%    - Apply the Law of Conservation of Mass to get the same number of atoms
%      of every element on each side of the equation. Tip: Start by balancing
%      an element that appears in only one reactant and product.
%    - Once one element is balanced, proceed to balance another, and another,
%      until all elements are balanced.
%    - Balance chemical formulas by placing coefficients in front of them. Do
%      not add subscripts, because this will change the formulas.
% 3. Indicate the states of matter of the reactants and products.
%    - Use (g) for gaseous substances.
%    - Use (s) for solids.
%    - Use (l) for liquids.
%    - Use (aq) for species in solution in water.
%    - Write the state of matter immediately following the formula of the
%      substance it describes.
% A number of different techniques exist. Commonly are by Inspection, Half-
% Equation, Oxidation Number, and Algebraic. This last one is considered
% arguably more general and powerful. The unbalanced chemical equation is
% used to define a system of linear equations, which can then be solved to
% yield the stoichiometric coefficients.
%
% Mostly of the above information was taken from 'Balancing Chemical Equations'
% of Dr. Anne Marie Helmenstine. Available on internet:
% Also, I broadly recommend to take-a-look to a lot of interesting articles
% of chemistry related by her:
%
% Syntax: stoichiometry(varargin)
%
% Inputs:
%       (varargin) = (s1 = species 1(cell array),s2 = species 2(cell array)
%        ...sN = species N(cell array)
%       The species is the 'formula'
%
% Output:
%       A table of the stoichiometric coefficients. The sign minus or plus
%       indicates in what side of the reaction they are.
%
% Examples:
% We give some examples which need stoichiometry. Mostly of them were taken
% from Chemistry 145 (Van Bramer), Chapter 4 (lecture notes & lecture problems)
% available on Internet at URL address:
% http://science.widener.edu/~svanbram/chem145/ch04/index.html
%
% 1. Elixirs such as Alka-Seltzer use the reaction of sodium bicarbonate with
% citric acid in aqueous solution to produce a fizz. Balance its reaction.
%             C6H8O7 + NaHCO3 = H2O + CO2 + Na3C6H5O7
% [Sodium citrate (Na3C6H5O7), as well as calcium citrate, are commonly known
% as sour salt. As a conjugate base of a weak acid, it can perform as a
% buffer, resisting changes in pH.  So, it is used to control acidity in some
% substances].
%
% Calling on Matlab the function:
%
% [C] = stoichiometry('C6H8O7','NaHCO3','H2O','CO2','Na3C6H5O7')
%
%
% Stoichiometric coefficients appear in the showed table and are:
% C = 1     3    -3    -3    -1
%
% 2. Demonstrate mixing baking soda with vinigar makes a fizz [Sodium
% Bicarbonate + Acetic Acid (Hydrogen Acetate)]. (C2H3O2Na or CH3COONa, is
% named sodium acetate. It may be added to foods as a preservative; in this
% application it is usually labelled E262. Is a strong base which can
% neutralize many acids and, as the conjugate base of a weak acid, a solution
% of sodium acetate can act as a buffer to keep a relatively constant pH).
% Balance its reaction.
%              NaHCO3 + HC2H3O2 = H2O + CO2 + C2H3O2Na
%
% Calling on Matlab the function:
%
% [C] = stoichiometry('NaHCO3','HC2H3O2','H2O','CO2','C2H3O2Na')
%
%
% Stoichiometric coefficients appear in the showed table and are:
% C = 1     1    -1    -1    -1
%
% 3. Solid Rocket fuel used for the space shuttle is aluminum metal and
% ammonium perchlorate. These react to produce aluminum oxide, aluminum
% chloride, nitrogen monoxide, and water [SRB info from NASA:
% http://spaceflight.nasa.gov/shuttle/reference/basics/srb/index.html].
% Balance the chemical equation.
%             Al + NH4ClO4 = Al2O3 + AlCl3 + NO + H2O
%
% Calling on Matlab the function:
%
% [C] = stoichiometry('Al','NH4ClO4','Al2O3','AlCl3','NO','H2O')
%
%
% Stoichiometric coefficients appear in the showed table and are:
% C = 3     3    -1    -1    -3    -6
%
% 4. Sulfur nowadays is mainly a waste product of the petroleum industry.
% Raw natural gas contains usually 0,1 - 0.5% hydrogen sulfide (H2S). Since
% hydrogen sulfide gas is extremely poisonous and an environmental pollutant,
% it is usually washed in sulfuric acid. This process yields sulfur dioxide
% and sulfur. Balance the chemical reaction, according to the following two
% equations.
%
% Calling on Matlab the function:
%
% [C] = stoichiometry('H2S','H2SO4','S2O2','H2O')
%
% Stoichiometric coefficients appear in the showed table and are:
% C = 1     1    -1    -2
%
% [C] = stoichiometry('S2O2','S','SO2')
%
%
% Stoichiometric coefficients appear in the showed table and are:
% C = 1    -1    -1
%
% 5. Photosynthesis (photo=light, synthesis=putting together), generally,
% is the synthesis of sugar from light, carbon dioxide and water, with oxygen
% as a waste product. It is arguably the most important biochemical pathway
% known; nearly all life depends on it. It is an extremely complex process,
% comprised of many coordinated biochemical reactions. It occurs in higher
% plants, algae, some bacteria, and some protists, organisms collectively
% referred to as photoautotrophs. Photosynthesis uses the energy of light to
% make the sugar, glucose. Balance its reaction.
%            CO2 + H2O = C6H12O6 + O2
%
% Calling on Matlab the function:
%
% [C] = stoichiometry('CO2','H2O','C6H12O6','O2')
%
% The number of independent chemical equations in a proper set are: 2
%
%
% Stoichiometric coefficients appear in the showed table and are:
% C = 6     6    -1    -6
%
% [The overall chemical reaction involved, bbiochemicaly speaking,
% gives a balance of: 6 CO2 + 12 H2O (+ light) = C6H12O6 + 6 O2 + 6 H2O ]
%
% 6. Relating to the explosion of ammonium perchlorate, Segraves and Wickersham
% (1991) gives the component species NH4ClO4, Cl2, N2O, and NOCl to give the
% products HCl, H2O, N2, O2, and ClO2. Balance the chemical reaction. This is
% a case when they occur simultaneously a number of independent chemical
% reactions.
%
% Calling on Matlab the function:
%
% [C] = stoichiometry('NH4ClO4','Cl2','N2O','NOCl','HCl','H2O','N2','O2','ClO2')
%
%
% Stoichiometric coefficients appear in the showed table and are:
% C = 1     5     3    -7    -4     0     0     0     0
%     1     1     1    -3     0    -2     0     0     0
%     0     1     2    -2     0     0    -1     0     0
%     0    -2    -2     4     0     0     0    -1     0
%     0    -3    -4     8     0     0     0     0    -2
%
% [As you can see, it is produced 5 chemical reactions.]
%
% Created by A. Trujillo-Ortiz, R. Hernandez-Walls, K. Barba-Rojo
%            and A. Castro-Perez
%            Universidad Autonoma de Baja California
%            Apdo. Postal 453
%            Mexico.
%            atrujo@uabc.mx
%
%
% To cite this file, this would be an appropriate format:
% Trujillo-Ortiz, A., R. Hernandez-Walls, K. Barba-Rojo and A. Castro-Perez. (2006).
%   stoichiometry:Calculation of the quantities of reactants and products in a
%   chemical reaction. A MATLAB file. [WWW document]. URL http://
%
% Reference:
% Segraves, R. O. and Wickersham, D. (1991), Learn Lessons from the PEPCON
%        Explosions. Chemical Engineering Progress, 87:65-69.
%

A = forma(varargin);

[B,j] = rref(A);
n = size(A,2);
c = length(j);
m = n-c;
i = 1:n;
i(j) = [];
C = zeros(n,m);
C(j,:) = B(1:c,i);
C(i,:) = -eye(m,m);
cmin = min(abs(nonzeros(C)));
C = C/cmin;

if size(C,2) > 1
fprintf('The number of independent chemical equations in a proper set are: %i\n', size(C,2));
else
end

disp(' ');
disp('Stoichiometric coefficients appear in the showed table and are:');

C = C';

[N D] = rat(C);
if any(D~=1);
I = find(D~=1);
I = I(1);
II = D(I);
C = II*C;
else
C = C;
end

C = round(C);
C = C';

CC = zeros(size(C));
for i = 1:size(C,2)
CC(:,i) = C(:,i)./min(gcd(C(:,i),min(C(C(:,i)~=0,i))));
end

C = CC';
C = flipud(C);

S = cellstr(varargin);

f = figure;
set(f,'name','Stoichiometric Coefficients:')
width = 80;
height = 20;
[row col] = size(C);
c = [1:row]';
res = get( 0, 'ScreenSize' );
set(f,'Position',[(res(3)/2)-(col*width+3)/2,(res(4)/2)+(row*height)/2,col*width+3,row*height+3*height]);
set(f,'NumberTitle','off');
set(f,'Resize','off')

for i = 1:col
uicontrol('Style', 'pushbutton', 'String', S{i},...
'Position', [(i-1)*width+3,height*row+30, width, height] );
end

for i=1:row
for j=1:col
uicontrol('Style', 'text', 'String', num2str(C(i,j)),...
'Position', [(j-1)*width+3,height*i, width, height]);
end
end

C = flipud(C);

uiwait(f)

return,```