% This program corresponds to exercise 3 in sheet 1
% Function for approximating ln(2) with tolerance tol
% (assumed to be around 10^(-3), but can be changed
% Please note: this is converging really slowly (only after 722 iterations)
% This is somehow expected because the series converges with 1/n
function test3=sum(tol,n)
%initializes the exact function and the sum
sum=0;
exact=-log(2);
for i=1:n 
    error=abs(exact-sum)/abs(exact);
    if error < tol 
        disp('procedure successful')
        disp('number of iterations:')
        disp(i)
        disp('error:')
        disp(error)
        break
    else
        if i==n 
            disp('procedure unsuccessful: increase the number of iterations')
        disp('error:')
        disp(error)
        else
            if rem(i,2)==0
                sum=sum+1/i;
            else
                sum=sum-1/i;
            end
        end
    end
end
end