% function [f,g,H] = f_18_3(x)
%
%  Computes the function 
%
%     f(x) = exp(x1 x2 x3 x4 x5) - 0.5*x1^3 + x2^3 +1)^2
%
%  in Problem 18.3 in Nocedal and Wright.
%
%  The gradient of f at x and its Hessian can also be computed on demand.
function [f,g,H] = f_18_3(x)
ep = exp(prod(x));
f = ep - 0.5*(x(1)^3+x(2)^3+1)^2;

if (nargout>1)
 g = [ x(2)*x(3)*x(4)*x(5)*ep - 3*x(1)^2*(x(1)^3+x(2)^3+1)
       x(3)*x(4)*x(5)*x(1)*ep - 3*x(2)^2*(x(1)^3+x(2)^3+1)
       x(4)*x(5)*x(1)*x(2)*ep
       x(5)*x(1)*x(2)*x(3)*ep
       x(1)*x(2)*x(3)*x(4)*ep ];
end;

if (nargout>2)
H=...
[ x(2)^2*x(3)^2*x(4)^2*x(5)^2*ep-9*x(1)^4-6*(x(1)^3+x(2)^3+1)*x(1), x(3)*x(4)*x(5)*ep+x(2)*x(3)^2*x(4)^2*x(5)^2*x(1)*ep-9*x(2)^2*x(1)^2, x(2)*x(4)*x(5)*ep+x(2)^2*x(3)*x(4)^2*x(5)^2*x(1)*ep, x(2)*x(3)*x(5)*ep+x(2)^2*x(3)^2*x(4)*x(5)^2*x(1)*ep, x(2)*x(3)*x(4)*ep+x(2)^2*x(3)^2*x(4)^2*x(5)*x(1)*ep;
x(3)*x(4)*x(5)*ep+x(2)*x(3)^2*x(4)^2*x(5)^2*x(1)*ep-9*x(2)^2*x(1)^2, x(1)^2*x(3)^2*x(4)^2*x(5)^2*ep-9*x(2)^4-6*(x(1)^3+x(2)^3+1)*x(2), x(1)*x(4)*x(5)*ep+x(1)^2*x(3)*x(4)^2*x(5)^2*x(2)*ep, x(1)*x(3)*x(5)*ep+x(1)^2*x(3)^2*x(4)*x(5)^2*x(2)*ep, x(1)*x(3)*x(4)*ep+x(1)^2*x(3)^2*x(4)^2*x(5)*x(2)*ep;
x(2)*x(4)*x(5)*ep+x(2)^2*x(3)*x(4)^2*x(5)^2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