function arcss = arsearch(fname,nmax,nextk,guess,RR,tol)

% Apdaptive Random Search 
% (Heuckroth & Gaddy, AIChE Journal, Vol 22, No. 4, July 1976)

%% "R" changed to "RR" JH 1997/09/08 
%%   - conflicts with (l)adcp edit command r.m on PC 

  RR = RR(:);
  nchk  = 100;
  fchk  = 1.0e30;
  npts  = 1;

  f0   = fchk;
  x0   = guess(:);
  nvar = length(guess);
  xn   = x0;

  nn = 0;
  kn = 1;
  kk = 0;
  dx = RR(:,ones(1,nextk)) .* (2 * rand(nvar,nextk)-1);

disp(' trial     offset     rate     error');
for jj = 1:nmax,

   fn = eval([fname,'(xn)']);

   if (fn < f0)   % It is an improvement.
      x0 = xn;
      f0 = fn;

    %  if abs(fchk - f0) < tol,  break, end;
      fchk = f0;

   end   %/* end of if (fn < f0) */

   nn = nn + 1;
   if nn == nchk,
      nn = 0;
%      fprintf(1,'%3.0f  %10.5f  %10.5f  %10.6f\n', ...
%                        jj, x0(1), x0(2), f0);
      fprintf(1,'%3.0f', jj);
      fprintf(1,'  %10.5f', x0);    % print all parameters
      fprintf(1,'  %10.6f\n', f0);
   end


   kk = kk + 1;
   if kk == nextk,
      kk = 1;
      kn = kn + 2;
      dx = RR(:,ones(1,nextk)) .* (2 * rand(nvar,nextk)-1) .^kn;
   end

   xn = x0 + dx(:,kk);
end % /* end of for */

arcss = x0;

%/******* End of ARSearch.m file ***********/ 
