function [xo,Ot,nS]=univarg(S,x0,ip,Lb,Ub,problem,tol,mxit)
//   Unconstrained optimization using univariate search (gold section).
//
//   [xo,Ot,nS]=univarg(S,x0,ip,Lb,Ub,problem,tol)
//
//   S: objective function
//   x0: initial point
//   ip: (0): no plot (default), (>0) plot figure ip, (<0) plot clean figure ip
//   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 stages (default = 100)
//   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/04 21:00:05 $
//   Argimiro R. Secchi
 
 if nargin < 2,
   error('univar requires two input arguments');
 end
 if nargin < 3,
   ip=0;
 end
 if nargin < 4,
   Lb=-x0-~x0;
 end
 if nargin < 5,
   Ub=2*x0+~x0;
 end
 if nargin < 6,
   problem=-1;
 end
 if nargin < 7,
   tol=1e-4;
 end
 if nargin < 8,
   mxit=100;
 end
 
 x0=x0(:);
 y0=feval(S,x0)*problem;
 
 n=size(x0,1);
 E=eye(n);

 if ip & n == 2,
   figure(abs(ip));
   if ip < 0,
     clf;
   end
   [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;
   contour(X1,X2,f); hold on;
   plot(x0(1),x0(2),'ro');
 end
 
 x=x0; it=0;
 while it < mxit,
  for i=1:n,
   [stepsize,xo,Ot]=aurea(S,x,E(:,i),problem,tol);

   if ip & n == 2,
     plot([x(1) xo(1)],[x(2) xo(2)],'r');
   end
   
   x=xo;
  end
   
  if abs(Ot-y0) < tol & norm(xo-x0) < tol,
    break;
  end

  y0=Ot;
  x0=xo;
  it=it+1;
 end

 if ip & n == 2 & it < mxit,
   plot(xo(1),xo(2),'r*');
 end
 
 endfunction
 