% Oct15 2002
% This is the fixed point iteration 
% of the function f(x)=(x-1)^2/alpha
%
clear, close
disp('This is the fixed point iteration of the function f(x)=(x-1)^2/alpha.')
disp('For alpha>1.33333 the fixed point iteration will converge.')
disp('For 1.333>alpha>=0.5 the iteration is bounded but does not converge.')
disp('For alpha<0.5 the iteration diverges to infinity')
alpha=input('Enter a value of alpha: ');
n=input('Enter a value for n, the number of iterations (100 is suggested): ');
x=zeros(1,n+1);
x(1)=input('Enter an initial value x_0 between 0 and 2: ');
for i=1:n;
x(i+1)=(x(i).^2-2*x(i)+1)/alpha;
end
clf
subplot(2,1,1)
plot(x,'*','linewidth',2)
plot(x,'-*','linewidth',1)
      axis([0,n,0,2])
     title(sprintf('%4.0f iterations of g(x)',n))
     xlabel('Iteration Number, i')
     ylabel('x_i')
subplot(2,2,3)
xs=0:0.01:2;
fs=(xs.^2-2*xs+1)/alpha;
plot(xs,fs,xs,xs,'linewidth',2)
     axis([0,2,0,2])
     title('Plot of g(x) and x showing the fixed point')
subplot(2,2,4)
   fp=(2+alpha-sqrt(alpha^2+4*alpha))/2;
     plot(log10(abs(x-fp)),'-*','linewidth',1)
     axis([0,n,-20,10])
     title('Plot of the Error of the iterations')
     xlabel('Iteration, i','fontsize',18)
     ylabel('log_{10}|Error_i|','fontsize',18)
%     for i=1:n+1
%disp(sprintf('%8.6f',x(i)))
%end

