function d=Distance(aa)
% DISTANCE function to be used in fminsearch
%   d=Distance(aa) computes for a given parameter set in vector aa the
%   distance between the measured values df for the years yf contained
%   in global variables to the solution of the ODE model.

global yf df a x0;

a(1:length(aa))=aa;                 % put the current parameter choice
[t,x]=ode45('F',yf,x0);             % solve the model with it
subplot(1,2,1)
semilogy(t,x(:,2),'-',yf,df,'o');   % plot data and current fit
subplot(1,2,2)
semilogy(yf,abs(x(:,2)-df),'-');    % plot the error
drawnow                             % draw to see while optimizing
d=norm(x(:,2)-df,'inf');            % compute some norm of the error