function [x,f] = Newton_unc(fun,x)
% Método de Newton
%
 rho = 0.1; beta = 0.5;        % Parámetros de line search
 maxit = 100;
 for i=1:maxit                 % Proceso interativo
    [f,g,H] = fun(x);
    p = -H\g;
    if abs(p'*g) < 1e-8, break, end
    alpha = 1;
    for k=1:50                % Cálculo inexacto amplitud de paso
       xnew  = x+alpha*p;
       fxnew = fun(xnew);
       if fxnew < f+alpha*rho*g'*p, break
       else alpha=alpha*beta; 
       end
    end
    x = x + alpha*p;
    fprintf('%1$3.0f %2$13.8e %3$13.8e %4$13.8e %13.8e\n',i,x,f,alpha);
 end
end

function [f,g,H] = objfun_min2(x)
 A = [1 3; 1 -3; -1 0]; b = -0.1*[1; 1; 1];
 f = sum(exp(A*x+b)); if nargout<2, return, end
 g = A'*exp(A*x+b); H = A'*diag(exp(A*x+b))*A;
end