      function fraction=aminofr(pH,pK1,pK2)
%-----------------------------------------------------------------------
% Programming Note:
%   Solve a set of linear algebraic equations of the form A*x=b
%-----------------------------------------------------------------------
% Instructor: Nam Sun Wang
%-----------------------------------------------------------------------

% Definition of pH & pK
      H  = 10^(-pH);
      K1 = 10^(-pK1);
      K2 = 10^(-pK2);

% Equations:
%     K1*Acation - H*A            = 0  % Definition for K1
%                 K2*A - H*Aanion = 0  % Definition for K2
%        Acation   + A   + Aanion = 1  % Conservation of mass
      A = [ K1 -H  0; 0 K2 -H; 1 1 1];
      b = [ 0; 0; 1];
      fraction = A\b;
