% Newton's method example: to compute f(x) for table of x values
% (x = 0.00:0.01:1.00) if y = f(x) is defined as the solution of
%     y - e^{-y} = x

% uncomment to see first case:
%x = 0, y = 0.5, for k = 1:10, y = y - (y-exp(-y)-x) / (1+exp(-y)), end

% uncomment to see slowish convergence when first guess is bad:
%x = 0, y = -5, for k = 1:10, y = y - (y-exp(-y)-x) / (1+exp(-y)), end

% build table, now that we see how to start and how quick Newton is:
printf('   x  y\n')
y = 0.57;
for x = 0.00:0.01:1.00
  for k = 1:3  % given that we start close, only 3 steps gets full accuracy
    y = y - (y - exp(-y) - x) / (1 + exp(-y));
  end
  printf('%4.2f  %16.14f\n',x,y)      % print table value
end

