function [xo,Ot,nS]=varmetr(method,S,x0,ip,G,linesearch,Lb,Ub,problem,tol,mxit)
//   Unconstrained optimization using variable metric methods.
//
//   [xo,Ot,nS]=varmetr(method,S,x0,ip,G,linesearch,Lb,Ub,problem,tol,mxit)
//
//   method: variabel metric method {'bfgs','dfp','gillmurray','steepdesc'}
//   S: objective function
//   x0: initial point
//   ip: (0) no plot (default), (>0) plot figure ip with pause, (<0) plot figure ip
//   G: gradient vector function
//   linesearch: (0) quadratic+cubic (default), (1) cubic
//   Lb, Ub: lower and upper bound vectors to plot (default = x0*(1+/-2))
//   problem: (-1): minimum (default), (1): maximum
//   tol: tolerance (default = 1e-4)
//   mxit: maximum number of iterations (default = 50*(1+4*~(ip>0)))
//   xo: optimal point
//   Ot: optimal value of S
//   nS: number of objective function evaluations

//   Copyright (c) 2001 by LASIM-DEQUI-UFRGS
//   $Revision: 1.0 $  $Date: 2001/07/12 17:40:12 $
//   Argimiro R. Secchi (arge@enq.ufrgs.br)

 if nargin < 3,
   error('varmetr requires three input arguments!');
 end
 if nargin < 4 || isempty(ip),
   ip=0;
 end
 if nargin < 5 || isempty(G),
   grad=0;
 else
   grad=1;
 end
 if nargin < 6 || isempty(linesearch),
   linesearch=0;
 end
 if nargin < 7 || isempty(Lb),
   Lb=-x0-~x0;
 end
 if nargin < 8 || isempty(Ub),
   Ub=2*x0+~x0;
 end
 if nargin < 9 || isempty(problem),
   problem=-1;
 end
 if nargin < 10 || isempty(tol),
   tol=1e-4;
 end
 if nargin < 11 || isempty(mxit),
   mxit=50*(1+4*~(ip>0));
 end

 x0=x0(:);
 n=size(x0,1); 

 if ip && n == 2,
   figure(abs(ip));
   [X1,X2]=meshgrid(Lb(1):(Ub(1)-Lb(1))/20:Ub(1),Lb(2):(Ub(2)-Lb(2))/20:Ub(2));
   [n1,n2]=size(X1);
   f=zeros(n1,n2);
   for i=1:n1,
    for j=1:n2,
      f(i,j)=feval(S,[X1(i,j);X2(i,j)]);
    end
   end
   mxf=max(max(f));
   mnf=min(min(f));
   df=mnf+(mxf-mnf)*(2.^(([0:10]/10).^2)-1);
   contour(X1,X2,f,df); hold on;
//   clabel(v,h);
//   h1=plot(x0(1),x0(2),'ro');
   h1=xplot(x0,0);
   legend(h1,'start point');

   if ip > 0,
     disp('Pause: hit any key to continue...'); pause;
   end
 end

 if problem == 1,
   pr='-feval(''';
 else
   pr='feval(''';
 end;
  
 v=version;   // Matlab version
 
 if v(1) > '3',
   op=optimset('LargeScale','off','HessUpdate',method,'MaxFunEvals',inf,...
               'MaxIter',mxit,'Display','iter','TolFun',tol,'TolX',tol);
   
   fun=[pr S ''',x)'];
   
   if grad,
     op=optimset(op,'gradobj','on');
     if ip && n == 2,
       fun={fun,['[' pr G ''',x);xplot(x,' int2str(ip) ')*[]]']};
     else
       fun={fun,[pr G ''',x)']};
     end
   else
     op=optimset(op,'gradobj','off');
     if ip && n == 2,
       fun=['[' fun ';xplot(x,' int2str(ip) ')*[]]'];
     end
   end

   if linesearch,
     op=optimset(op,'LineSearch','cubicpoly');
   else
     op=optimset(op,'LineSearch','quadcubic');
   end   
   
   [xo,yo,exitflag,out] = fminunc1(fun,x0,op);
   nS=out.funcCount;
   it=out.iterations;
 else 
 // version 4
 end

 Ot=-yo*problem;
 
 if it >= mxit,
   disp(['Warning ' method ': reached maximum number of iterations!']);
 elseif ip && n == 2,
   figure(abs(ip)); h2=plot(xo(1),xo(2),'r*');
   legend([h1,h2],'start point','optimum');
 end

 endfunction
 