%-----------------------------------------------------------------------
% Plot the fractions of three forms of a chemical species with pH.
%-----------------------------------------------------------------------
% Instructor: Nam Sun Wang
%-----------------------------------------------------------------------

% Start fresh ----------------------------------------------------------
      clear all

% Program header -------------------------------------------------------
      disp('This program finds the fractions of the three different forms')
      disp('when the chemical species can double dissociate.')

% Dissociation constants for glutamic acid -----------------------------
      pK1 = 2.10;
      pK2 = 9.47;

% Range pH and find the fractions of the three different forms ---------
%     pH = [0. :       12.];    % Corse divisions for table output or testing
      pH = [0. : 0.1 : 12.];    % Finer divisions for plotting
      fraction = zeros(3,size(pH,2));
      for i=1:size(pH,2)
        fraction(:,i) = aminofr1(pH(i), pK1, pK2);
      end

% Print a table --------------------------------------------------------
      disp(' ')
      disp('       --------Fractions--------')
      disp('   pH   Acation     A     Aanion')
      disp('  ------------------------------')
      for i=1:size(pH,2)
        fprintf(' %4.1f %8.4f %8.4f %8.4f\n', pH(i), fraction(:,i))
      end

% Plot and add legends -------------------------------------------------
      plot(pH,fraction)
      xlabel('pH')
      ylabel('Fraction')
      text(   0.2, 0.60, 'Cation +')
      text(   2.6, 0.70, 'Neutral' )
      text(  10.2, 0.80, 'Anion -' )
      text(   4.5, 0.90, 'Glutaminic Acid'    )
      text(pK1-1., 0.05, ['pK_1=' num2str(pK1)])
      text(pK2-1., 0.05, ['pK_2=' num2str(pK2)])
