% Stoichiometry Examples
%
% 
% Balancing
%  ___ HNO3 + ___ CH3OH ==> ___ C3H7NO2 + ___ CO2 + ___ H2O
%
% Let
%  1 HNO3 + a CH3OH ==> b C3H7NO2 + c CO2 + d H2O
%
%
% Then set up 4 elemental balances
%
%   H:   1 + 4 a = 7 b + 0 c + 2 d
%
%   N:   1 + 0 a = 1 b + 0 c + 0 d
%
%   O:   3 + 1 a = 2 b + 2 c + 1 d
% 
%   C:   0 + 1 a = 3 b + 1 c + 0 d
%
% Rearranging
%
%   H:   4 a - 7 b - 0 c - 2 d = -1
%
%   N:   0 a - 1 b - 0 c - 0 d = -1
%
%   O:   1 a - 2 b - 2 c - 1 d = -3
% 
%   C:   1 a - 3 b - 1 c - 0 d =  0
%
% Thus
%     [A] [x] = [b] matrices are defined
%

A = [ +4 -7 0 -2; 0 -1 0 0; 1 -2 -2 -1;  1 -3 -1 0]
b = [ -1; -1; -3; 0]        % a little sloppy having a vector "b" and a coeff "b"

% [x] = [A]^-1 [b]

Ainv = inv(A)
x = Ainv * b

% check
bCheck = A * x


% Second Case  1 C2H5OH + a SO4^2- ==> b CH3COO^- + c HS^- + d H^+ + e H2O
% A2 = [0 +2 0 0 0; 0 3 1 1 2; -4 2 0 0 1; -1 0 1 0 0; +2 -1 -1 +1 0]
% b2 = [2; 6; 1; 0; 0]

% A2inv = inv(A2)
% x2 = A2inv * b2




