function [gfnew,gn] = goalgra(V,neqcstr,FUNfcns,GRADfcns,WEIGHT,GOAL,x,varargin);
%GOALGRA Utility function to translate gradient in goal-attainment problem.
%   Intermediate function used to translate goal attainment
%   problem into constrained optimization problem.
%   Used by ATTGOAL and CONSTR. 
%
%   See also GOALFUN.

%   Copyright (c) 1990-98 by The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 1997/11/29 01:23:14 $
%   Andy Grace 7-9-90.

% Note: gg not used by attgoal; gg is used by minimax
nx=length(V)-1;
x(:)=V(1:nx);
lambda=V(nx+1);
gg=[];

[gf,gg,msg]=opteval(x,GRADfcns,varargin{:});
ncstr=length(WEIGHT);

GL = -ones(1,ncstr+neqcstr);
for i=1:ncstr
  if WEIGHT(i)~=0
    gf(:,i)=gf(:,i)/WEIGHT(i);
    if i<=neqcstr,gf(:,i+ncstr)=-gf(:,i); end 
  else
    GL(1,i)=0;
  end
end
gfnew=[zeros(nx,1);1];
gn=[gf;GL];
gs=size(gg);
% Put gg first to allow equality constraints in minimax
if gs(1)>0, gn=[[gg;zeros(1,gs(2))],gn]; end
