function [x,OPTIONS]=minimax(FUN,x,OPTIONS,VLB,VUB,GRADFUN,varargin)
%MINIMAX Finds a minimax solution of a function of several variables.
%   MINIMAX has been replaced with FMINIMAX.  MINIMAX currently works but
%   will be removed in the future.  Use FMINIMAX instead.
%
%   MINIMAX solves the following problem:
%   min (max {FUN(X} )  where FUN and X can be vectors or matrices.
%    X 
% 
%   X=MINIMAX('FUN',X0) starts at X0 and finds a minimax solution to 
%   the function which is described in FUN (usually an M-file: FUN.M).
%   The function 'FUN' should return the values, at X, of the functions to  
%   be minimized and a vector of constraints G evaluated at X: 
%   [F,G]=FUN(X). F is minimized such that G <= zeros(G).
%
%   X=MINIMAX('FUN',X,OPTIONS) allows a vector of optional parameters to 
%   be defined. For more information type HELP FOPTIONS.
%   
%   X=MINIMAX('FUN',X,OPTIONS,VLB,VUB) defines a set of lower and upper
%   bounds on the design variables, X, so that the solution is always in 
%   the range VLB <= X <= VUB. 
%
%   X=MINIMAX('FUN',X,OPTIONS,VLB,VUB,'GRADFUN') allows a function 
%   'GRADFUN' to be entered which returns the partial derivatives of the 
%   functions at X stored column-wise:  GRADS = GRADFUN(X). 
%
%   X=MINIMAX('FUN',X,OPTIONS,VLB,VUB,'GRADFUN',P1,P2,..) passes the 
%   problem-dependent parameters P1,P2,... directly to the functions FUN 
%   and GRADFUN: FUN(X,P1,P2,...) and GRADFUN(X,P1,P2,...).  Pass
%   empty matrices for OPTIONS, VLB, VUB, and 'GRADFUN' to use the 
%   default values. 
%
%   [X,OPTIONS]=MINIMAX('FUN',X0,...) returns the parameters used in the 
%   optimization method.  For example, options(10) contains the number 
%   of function evaluations used.
%

%   Copyright 1990-2000 The MathWorks, Inc. 
%   $Revision: 1.21 $  $Date: 2000/06/16 22:24:52 $
%   Andy Grace 7-9-90.

%  This function is old, the current version should be used.
WarningStr=sprintf(...
['The function MINIMAX is obsolete and has been replaced by\n',...
'FMINIMAX.  MINIMAX will be removed in a future release of the\n',...
'Optimization Toolbox.  Update your code to call FMINIMAX to\n',...
'suppress this warning message.  See "Converting your code to\n',...
'Version 2 Syntax" in the Optimization Toolbox User''s Guide\n',...
'for more information.\n']);

warning(WarningStr)




%
if nargin < 2, error('minimax requires two input arguments'); end
if nargin < 3, OPTIONS=[]; end
if nargin < 4, VLB=[]; end
if nargin < 5, VUB=[]; end
if nargin < 6, GRADFUN=[]; end

lenopt = length(OPTIONS); 
if ~lenopt, OPTIONS=0; end

OPTIONS=foptions(OPTIONS);
OPTIONS(7) = ~OPTIONS(7);
neqcstr=OPTIONS(15); 

xnew=x(:);

lenVarIn = length(varargin);
% goalgra and goalobj also take: neqcstr,funfcn,gradfcn,WEIGHT,GOAL,x
goalargs = 6;  

% Convert to inline function as needed.
gradflag = 0;
[funfcn, msg] = prefcnchk(FUN,'minimax',lenVarIn,gradflag);
if ~isempty(msg)
  error(msg);
end
[ffun, msg] = prefcnchk('goalobj','minimax',lenVarIn+goalargs,gradflag);
if ~isempty(msg)
  error(msg);
end
if ~isempty(GRADFUN)
  gradflag = 1;
  [gradfcn, msg] = prefcnchk(GRADFUN,'minimax',lenVarIn,gradflag);
  if ~isempty(msg)
    error(msg);
  end  
  [gfun, msg] = prefcnchk('goalgra','minimax',lenVarIn+goalargs,gradflag);
  if ~isempty(msg)
    error(msg);
  end
else
  gradfcn = [];
  gfun = [];
end

% Set up WEIGHT and GOAL for goalobj and goalgra
if funfcn{3} == 1
  [f,g]=feval(funfcn{1},x,varargin{:});
elseif funfcn{3} == 2
  f=feval(funfcn{1},x,varargin{:});
end
ncstr=length(f(:));
WEIGHT = ones(ncstr,1);
GOAL = zeros(ncstr,1);

[xnew, OPTIONS]=nlconstold(ffun,[xnew;0],OPTIONS,VLB,VUB,gfun,neqcstr,funfcn,gradfcn,WEIGHT,GOAL,x,varargin{:});

OPTIONS(7) = ~OPTIONS(7);
x(:) = xnew(1:length(xnew)-1);
