function x = newtontc(x,F,DF,reltol,abstol)
% MATLAB template for Newton method with affine invariant termination
% based on (affine invariant) simplified Newton correction
% x supplies the initial guess, F and DF are function handles of type @(x) 
% to the function F and its Jacobian, reltol/abstol are the absolute and
% relative tolerances to be met by the final approximation solution, which
% is returned in x.
f = F(x);
while (true)
  % Save computational effort by precomputing LU-decomposition of Jacobian
  J = DF(x); [L,U] = lu(J);
  % Compute Newton correction and new iterate
  s = U\(L\f); x = x - s;
  f_old = f; f = F(x);
  % simplified Newton correction
  st = U\(L\f); stn = norm(st);
  if ((stn < abstol) || (stn < reltol*norm(x))), return; end
end