%NPSOL   Sequential quadratic programming code NPSOL by Gill et. al.
%        [x,f,g,c,cJac,inform,lambda,iter,istate]= ...
%           npsol(A,l,u,x,'funobj','funcon',msglvl,derlvl,verlvl)
%        solves the problem 
%                        minimize     f(x)
%                        subject to   ( )     (   x  )    ( )
%                                     (l) =<  (  Ax  ) =< (u)
%                                     ( )     ( c(x) )    ( )
%        On successful termination, x contains the optimal solution, and
%        f and g contain the final objective function value and gradient.
%        c and cJac contains the constraint and the constraint Jacobian.
%        Lambda contains the Lagrange multiplier vector of the final QP,
%        istate denotes the status of the constraints and iter denotes
%        the number of iterations. Inform contains information about the
%        exit status of NPSOL. Funobj is the name of the m-file for 
%        evaluating the objective function and funcon is the name of the
%        m-file for evaluating the constraint functions (without .m-extension).
%
%        The following two m-files must be supplied (where default names are
%        'funobj.m' and 'funcon.m' unless stated otherwise in the variables
%        funobj and funcon).
%
%        'funobj.m' containing function [f,gradf]=funobj(x) that
%                              computes the objective function f and the
%                              gradient gradf at the point x.
%
%        'funcon.m' containing function [c,cJac]=funcon(x) that
%                              computes the constraint value c and the
%                              constraint Jacobian cJac for the
%                              nonlinear constraints at the point x.
%
%        Msglvl determines the amount of output. Msglvl=1 (default) gives one
%        line of output per major iteration, msglvl=0 gives no output.
%
%        Derlvl controls which derivatives are given. (Default=0)
%        Verlvl controls the level of checking of gradients. (Default=0)
%
%        This version for Matlab 4.0 dated 19-Apr-1992.

function [x,f,g,c,cJac,inform,lambda,iter,istate]= ...
            npsol(A,l,u,x,funobj,funcon,msglvl,derlvl,verlvl)

%        M-file written by    Anders Forsgren
%                             Division of Optimization and Systems Theory
%                             Department of Mathematics
%                             Royal Institute of Technology
%                             S-100 44 Stockholm
%                             Sweden
%                             andersf@math.kth.se

if nargin < 9
   verlvl = 0;
end

if nargin < 8
   derlvl = 0;
end

if nargin < 7
   msglvl = 1;
end

if nargin < 5
   funobj = 'funobj';
   funcon = 'funcon';
end

[mA,nA] = size(A);
[lx] = length(x);
[ll] = length(l);
[lu] = length(u);

if ll ~= lu | ...
   ll < mA + lx | ...
   (nA > 0 & nA ~= lx),
   clear mA nA lx ll lu obj lambda istate iter inform msglvl ...
         c cJac funcon derlvl f g funobj verlvl
   disp('                                                              ')
   disp(' --- Error using NPSOL, incompatible dimensions of input data.')
   disp(' --- NPSOL not executed. Please check below and correct.      ')
   disp('                                                              ')
   whos
else
   [x,f,g,c,cJac,inform,lambda,iter,istate] = ...
      npsolmex(A,l,u,x,funobj,funcon,msglvl,derlvl,verlvl);
end
