Code covered by the BSD License  

Highlights from
Balancing

image thumbnail
from Balancing by Antonio Trujillo-Ortiz
Balancing of Chemical Reactions when the Reactions are Unknown.

balancing(varargin);
function [B] = balancing(varargin);
%BALANCING Balancing of Chemical Reactions when the Reactions are Unknown.
% -----WARNING: This procedure is effective even when reactions are 
%      not known a priori (reactants & products that participate in
%      the reaction.)------ 
% This m-file presents a method for identifying and balancing any number of
% chemical reactions. The procedure is based on the singular-value-
% decomposition of the formula matrix for the reaction mixture. The method
% is effective even when the reactions are not known a priori.
% all the method description and fundamentals were taken from the J.M. 
% Haile's PDF document available by purchase on Internet at the URL address:
% http://www.macatea.com/downloads/dnldpag1.shtml
%
% Syntax: balancing(varargin) 
%      
% Inputs:
%       (varargin) = (s1 = species 1(cell array),s2 = species 2(cell array)
%        ...sN = species N(cell array)
%       The species is the 'formula'. eg. 'CH4'
%
% Output:
%       A table of the balancing coefficients. The sign minus or plus
%       indicates in what side of the reaction they are.
%
% Example. From the Haile's paper, we consider a closed chemical reactor
% from which we draw a simple and find C=6 species (CH4,CO2,CO,H2O,H2,and O2)
% formed of m=3 elements (C,H,and O), and we can form an (m X C) array of
% integers that specifies the distribution of elements among species. From
% this we want to determine the number of independent chemical reactions,
% the identities of all possible independent reactions, and the stoichiometric
% coefficients for all the reactions.
%
% Calling on Matlab the function: 
%
%    [B] = balancing('CH4','O2','CO2','H2O','CO','H2')
%
% Answer is:
%
% The number of different reactions are: 3
% 
% Possible balancing formula(s) appear in the showed table and are:
%
% B =
%     4     4    -1    -3    -3    -5
%     0     3    -8     2     8    -2
%     0     4    -1    -7     1     7
%     
% Created by A. Trujillo-Ortiz, R. Hernandez-Walls, K. Barba-Rojo
%            and A. Castro-Perez 
%            Facultad de Ciencias Marinas
%            Universidad Autonoma de Baja California
%            Apdo. Postal 453
%            Ensenada, Baja California
%            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).
%   balancing:Balancing of Chemical Reactions when the Reactions are Unknown.
%   A MATLAB file. [WWW document]. URL http://www.mathworks.com/matlabcentral/
%   fileexchange/loadFile.do?objectId=13105
%
% Copyright. October 27, 2006.
%

A = forma(varargin);

[r c] = size(A);

if r == c,
    A;
else
    if r < c,
        A = [A;zeros(r,c)];
    else (r > c),
        A = [A;zeros(c,r)];
    end
end

R = c-rank(A);

disp(' ');
fprintf('The number of different reactions are: %i\n', R);

[U S V] = svd(A);

P = V(:,~any(S,1));

NZ = [];
for i = 1:size(P,2),
    nz = min(abs(nonzeros(P(:,i))));
    NZ = [NZ nz];
end

l = diag(NZ);

F = [];
for i = 1:length(NZ),
    f = round((-1/l(:,i))*P'*10)/10;
    F = [F;f];
end

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

F = round(F);
FF = F';

disp(' ');
disp('Possible balancing formula(s) appear in the showed table and are:'); 

B = -1*FF';
B = flipud(B);

S = cellstr(varargin);

f = figure;
set(f,'name','Stoichiometric Coefficients:')
width = 80;
height = 20;
[row col] = size(B);
resol = get( 0, 'ScreenSize' );
set(f,'Position',[(resol(3)/2)-(col*width+3)/2,(resol(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(B(i,j)),...
        'Position', [(j-1)*width+3,height*i, width, height]);
    end
end

B = flipud(B);
uiwait(f)

return,

Contact us at files@mathworks.com