
% Example to solve exp(x)-2*x-1=0 with a solution within [1,2]
% 
% solve exp(x)-2*x-1 = 0
% vs. function iteration method 


% three rearrangements:
% (1) x = (exp(x)-1)/2
% (2) x = exp(x)-x-1
% (3) x = ln(2x+1)

clear, clc
format long g

% starting point: 
p1old = 1; p2old = 1; p3old = 1;
p1 = (exp(p1old)-1)/2;
p2 = exp(p2old)-p2old-1;
p3 = log(2*p3old+1);

tol=10e-6;
k=0;
% iteration
while  abs(p3-p3old)> tol %abs(p1-p1old)> tol & abs(p2-p2old)> tol &
    k=k+1
    p1old=p1;
    p2old=p2;
    p3old=p3;
    p1 = (exp(p1)-1)/2;
    p2 = exp(p2)-p2-1;
    p3 = log(2*p3+1);
end

disp('  #Iterations       eq1          eq2          eq3')
disp([k p1 p2 p3])