% MATLAB version of FORTRAN program for example 3.3 on pages 93/4 in conte/deboor
% This program solves the equation
%    exp(-?) = sin(?)
% by fixed-point iteration, using the iteration function
%
%  Reference:
%
%    Samuel Conte, Carl de Boor,
%    Elementary Numerical Analysis,
%    Third Edition,
%    SIAM, 2017,
%    ISBN: 978-1-611975-19-2.
%
   G = @(x) exp(-x) - sin(x) + x;

XTOL = 1e-8; FTOL = 1e-8;
disp('        XNEW             F(XNEW)            ERROR')
XOLD = .6; Y = G(XOLD) - XOLD; 
fprintf('  %15.8e    %15.8e\n',[XOLD,Y])
for j=1:20
   XNEW = G(XOLD); Y = G(XNEW) - XNEW; ERROR = abs(XNEW-XOLD)/abs(XNEW);
   fprintf('  %15.8e    %15.8e    %15.8e\n',[XNEW,Y,ERROR])
   if ERROR<XTOL || abs(Y)<FTOL, break, end
   XOLD = XNEW;
end
if XOLD==XNEW, disp('failed to converge in 20 iterations'), end
