%
% FIXED POINT ITERATION PROGRAM
%
% FUNCTION WHOSE FIXED POINT WE WANT TO FIND
% AND THE INITIAL GUESS

clear x
close all

%g =@(x) x - (1/2+x.^2/4-x.*sin(x)-1/2*cos(2*x))./(x/2-x.*cos(x)-sin(x)+sin(2*x));
g = @(x) 1./(x.^2+1) - 0.2*x.^2;
%g = @(x) 1/2*(x-1).^2+3/4;
x0 = -.9;

% MAXIMUM NUMBER OF ITERATIONS AND TOLERANCE
plot([-1:.01:1],g([-1:.01:1]));
hold on
plot([-1:.01:1],[-1:.01:1])
pause
Nmax = 20;   tol = 1e-6;

% START THE ITERATIONS

x(1) = x0;
for i=2:Nmax
    x(i) = g(x(i-1)); 
    plot(x(i-1), g(x(i-1)),'o') 
    drawnow
    pause    
    if abs(x(i)-x(i-1))<tol, break, end
end

format long
x = x(:)


E  = abs(x-x(end));
E1 = E(1:end-2);
E2 = E(2:end-1);
%plot(log(E1),log(E2),log(E1),log(E2),'.'),axis equal,grid