%
% Fitting function for Exercise 8.2
%
% Euler integration of dissolved oxygen balance
% with simple linear model for Kla vs. valve
% position.
%
% p(1) is oxygen uptake rate (assumed constant)
% p(2) is Kla vs. vp parameter
% tau  is process hydraulic residence time
%
% do   is dissolved oxygen vector
% vp   is valve position vector
% doe  is estimated DO vector
% sos  is sum of squares of residual between
%      the actual DO and the estimated DO
%
% (c) Bob Newell, February 1999
%
function sos = klaf( p )
%
global do;
global vp;
global doe;
%
tau = 0.5;
%
n = length( vp );
doe(1) = do(1);
for i =  2:n,
   kla = p(2) * vp(i);   
   ddodt = ( 0 - doe(i-1) ) / tau ...
      + kla * ( 10 - doe(i-1) ) ...
      - p(1);
   doe(i) = doe(i-1) + 0.005 * ddodt;
end;
%
e = do - doe;
sos = e * e';
%