echo off
disp(sprintf('**** Graphical presentation of 1-D fixed point iteration ***'));
a=2
b=0.8
c=3
%f(x)=a*x+c=(a-b)*x+b*x+c
%g(x)=-b/(a-b)*x-c/(a-b)=xi
axis equal;
x=-5:0.1:5
p=x
f=a*x+c
g=-b/(a-b)*x-c/(a-b)
figure(1);
hold on; 
grid on;
axis on;
plot(x,p,'-black'); %plot p
plot(x,f,'-b'); %plot f
plot(x,g,'-r'); %plot g
xi=3 	%initial guess
%plot(xi,0,'g.'); %plot initial guess
gi=-b/(a-b)*xi-c/(a-b)
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=-b/(a-b)*xi-c/(a-b)
    plot(xi,xi,'black.'); 
    li(2)=xi;
    line(li,lgi);
    li(1)=li(2);
    lgi(1)=lgi(2);
    gi=-b/(a-b)*xi-c/(a-b)
    plot(xi,gi,'r.'); %plot fix point
    lgi(2)=gi;
    line(li,lgi);
end
mi=-c/a	%direct solution
xi		% iterative solution after 5 iterations

echo off
disp(sprintf('**** 5-D linear stationary method - Jacobi ***'));
md=2*ones(5,1);
ud=rand(4,1);
ld=rand(4,1);
A=diag(md)+diag(ud,1)+diag(ld,-1)
InvA=inv(A)
B=A-diag(md)
D=A-B
c=[2 4 1 0 3]'	
%F(X)=A*X+c=D*X+B*X+c
%G(X)=-inv(D)*B*X-inv(D)*c=Xi
Xi=[1 1 1 1 1]'	%initial guess
for i=1:7
   Xi=-inv(D)*B*Xi-inv(D)*c;
end
mi=A\-c	%matlab solution
Xi		% iterative solution after 7 iterations
