%
% Display and Fitting Program for Exercise 8.2
%
% see function in file "klaf.m" for the model
% being fit.
%
% the interpolation and especially the fitting
% takes some seconds so be a little patient.
%
% (c) Bob Newell, February 1999
%
global do;
global vp;
global doe;
%
% load the data files
load vp119.dat;
load do119.dat;
%
% interpolate data to same time base
disp( 'Interpolating data ...' );
t = 0:0.005:5.8;
do = spline( do119(:,1), do119(:,2), t );
clear do119;
vp = spline( vp119(:,1), vp119(:,2), t );
clear vp119;
%
% plot the data
plot( t, [ do; vp ] );
set( gca, 'FontSize', 16 );
xlabel( 'Time in hours' );
ylabel( 'VP in [0,1] and DO in mg/l' );
%
% now do the fit ( adjust p0 if you add more
% parameters to the Kla vs. VP model )
disp( 'Fitting data ...' );
p0 = [ 0 0 ];
p = fmins( 'klaf', p0 );
%
% display the fitted parameter values
disp( [ 'Estimated OUR = ' num2str(p(1)) ] );
for i = 2:length(p),
   disp( [ 'Kla vs. VP model parameter ' num2str(i-1) ' = ' num2str(p(i)) ] );
end;
%
% add the estimated DO to the plot
hold on;
plot( t, doe, 'r-' );
hold off;
legend( 'Actual DO', 'Valve Position', 'Estimated DO', 4 );
%
