%
 function [ val, val_Z] = obj_otis(Z,data)
%
% This is the objective function used by the minimization routine
% 
% Z is the array of  control and state values
%
%   |                             |
%   |  u_1   u_2  .... U_Npanels  |
%   |  x_1   x_2  .... x_Npanels  |
%   |                             |
%
% data is a structure that contains information about
% the problem.
%  
%  x_final= Z(1+data.N_controls:data.N_states+data.N_controls,end); 
   x_final= Z((data.N_panels-1)*(data.N_controls+data.N_states)...
              +data.N_controls+1:end); 
   val    = feval(data.Mayer,x_final);
%
% check to see of gradient is also required
%
  if ( nargout > 1 )
    tmp   = feval(data.Mayerj,x_final);
    val_Z = [zeros(data.N_z-data.N_states,1);
              tmp(:)];
%   val_Z = sparse([1+data.N_z-data.N_states:data.N_z], ...
%                  [ones(data.N_states,1)],feval(data.Mayerj,x_final)); 
  end % if nargout
%
% return
%
