function [y,dg] = g(x)
y = cos(x(1)+x(2))+sin(x(1))+cos(x(2));

if nargout == 2                    % Compute the gradient only when necessary. 
  dg = [-sin(x(1)+x(2))+cos(x(1));
      -sin(x(1)+x(2))-sin(x(2))];
end

  
  