function xNew = Iteration(f, x0, tol)
% IterationE(f, x0, tol) calculates root of equation f(x)=0 using method
% of iterations, with initial condition x0 and tolerance tol

fprintf('\n *** Iteration method solver *** \n\n');

horzLine = '_________________________________________________';
fprintf(' n |        x(n)        |x(n)-x(n-1)|  f(x(n)) \n%s\n', horzLine);
xOld = x0;

for n = 1:100
    xNew = f(xOld);
     
    fprintf('%2d | %18.12g | %9.2e | %9.2e \n', n, xNew, abs(xNew-xOld), f(xNew)-xNew);  
    if  abs(xNew - xOld) < tol
        return
    end

    xOld = xNew;
end
fprintf('*** Iteration method failed to converge after 100 iterations');