function [f, df, H] = grosenbrock(X)
%GROSENBROCK Generalised Rosenbrock function.
%
%       The generalised Rosenbrock's function is a N dimensional function :
%
%       f(X) = \sum_{i=2}^{N} 100*(x_i - x_{i-1}^2)^2 + (1 - x_{i-1})^2
%
%       [f df] = GROSENBROCK(X) gives f(X) and df/dX.
%

N = length(X) ;
xx = zeros(size(X)) ;
df = zeros(size(X)) ;
for i = 2:N
  xx(i) = (X(i) - X(i-1)*X(i-1)) ;
end

f = 100 * sum(xx(2:N).^2) + sum((1 - X(1:(N-1))).^2) ;

df(1) = -400 * xx(2) * X(1) - 2 * (1 - X(1)) ;
df(2:(N-1)) = -400 * xx(3:N) .* X(2:(N-1)) + 200 * xx(2:(N-1)) - ...
    2 * (1 - X(2:(N-1))) ;
df(N) = 200 * xx(N) ;
