echo off
disp(sprintf('**** Graphical presentation of 1-D fixed point iteration ***'));

%f(x)=a*x+c=(m-n)*x+c
%g(x)=(n/m)*x-(1/m)*c=xi
a=1/3; c=-1;
m=2/3; n=1/3;
x=-5:0.1:5
p=x
f=a*x+c
g=(n/m)*x-(1/m)*c
figure(1);
hold on; 
grid on;
axis on;
axis([0 4 -1 4]);
plot(x,p,'-black'); %plot p
plot(x,f,'-b'); %plot f
plot(x,g,'-r'); %plot g
xi=1 	%initial guess
%plot(xi,0,'g.'); %plot initial guess
gi=(n/m)*xi-(1/m)*c
plot(xi,gi,'b.'); %plot fix point
li=[xi xi]
lgi=[0 gi]
line(li,lgi)
for i=1:10
    li(1)=li(2);
    lgi(1)=lgi(2);
    xi=(n/m)*xi-(1/m)*c
    plot(xi,xi,'black.'); 
    li(2)=xi;
    line(li,lgi);
    li(1)=li(2);
    lgi(1)=lgi(2);
    gi=(n/m)*xi-(1/m)*c
    plot(xi,gi,'r.'); %plot fix point
    lgi(2)=gi;
    line(li,lgi);
end
xi - (-c/a)	% error in the solution 
