% iterate2Ln.m
% Iteration of the sequence
% x(n+1) = 2^(n+1) * ( sqrt(1+x(n)/2^n) - 1 )
% starting with an initial value x(0).
% The sequence is expected to converge to ln(x(0)+1).
% This version iterates on y(n) = x(n)/2^n instead,
% and cures the cancellation problem by rationalization.
% K. Ming Leung, 01/18/2005

clear; format long g;
x0 = 2;
y = x0;
exact = log(x0+1),

for n = 1:55
    y = y/(sqrt(1+y)+1);
    fprintf('n = %2d    x(n) = %18.16f    error = %10.2e \n',n,2^n*y, 2^n*y-exact);
    % one square root and 3 arithmetic operations per iteration
end

    