function [f, g, h] = rbfun(x)

% Computes the value, gradient, and
% Hessian of Rosenbrock's function.

f = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;

if nargout > 1  % Compute g only if it actually
                       % needs to be evaluated.
    
    g = [400*x(1)^3 - 400*x(1)*x(2) + 2*x(1) - 2, ...
            200*x(2) - 200*x(1)^2];
    
    if nargout > 2  % Compute h only if it actually
                           % needs to be evaluated.
            
        h = [1200*x(1)^2 - 400*x(2) + 2, -400*x(1); ...
                -400*x(1), 200];  
        
    end
    
end

% Update the table of iteration points:
global iterations
iterations = [iterations; x];