% EVALUATE THE FUNCTION
%
% This matlab file will calculate the result f(x,y)
% of a certain function f, evaluated in the point (x,y).
% 
% This matlab file will calculate the result gradient of
% of a certain function f, evaluated in the point (x,y).
% Also the gradient of this function will be calculated.
%
% The file can also calculate the values of Z, for plotting
% the figure.


function [fxy,grad,Z]=example2(xinput,X,Y)
  
  
  %----------------------------------------------
  
  if (nargin==1)
    % Pay attention: xinput is generically a vector.
    x=xinput(1);
    y=xinput(2);
    
    % In order to calculate this we define here the function,
    % as well as the gradient (in column form).
    fxy=(x+y)^2;
    
    
    % The gradient is a column vector with the first component:
    % df/dx, and the second component df/dy evaluated in x,y.
    grad(1,1)= 2*(x+y);
    grad(2,1)= 2*(x+y);
    
    % REMARK, if there is an error here in the evaluation of the gradient,
    % probably there will occur an error in the steepest descent method.
  end
  
  %----------------------------------------------------------
  
  % This last function is necessary for plotting the figures:
  % REMARK, put a dot before the ^, i.e. use .^ instead of ^
  if (nargin==3) % nargin equals the number of input elements
    fxy=0;grad=0;
    Z=(X+Y).^2;
  end
  