%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all
n=100
h=1/(n+1);
A=gallery('poisson',n)/h^2;
S=chol(A);
S1=cholinc(A,0.1);
S2=cholinc(A,0.01);
S3=cholinc(A,0.001);
% figure(1)
% subplot(2,2,1),spy(S1)
% title('Fact. Incomp. Cholesky eps=0.1')
% subplot(2,2,2),spy(S2)
% title('Fact. Incomp. Cholesky eps=0.01')
% 
% subplot(2,2,3),spy(S3)
% title('Fact. Incomp. Cholesky eps=0.001')
% 
% subplot(2,2,4),spy(S)
% title('Fact. complete Cholesky ')
figure(2)
max_it=400;
tol=1.e-10%%%%%%%%%%%%%%%%%%%%%%%;
b=ones(n*n,1);
zed=zeros(n*n,1);

M=speye(n*n,n*n);
[x0, error0, iter0, flag0] = cg(A, zed, b, M, max_it, tol);
M=S1'*S1;
[x1, error1, iter1, flag1] = cg(A, zed, b, M, max_it, tol);

M=S2'*S2;
[x2, error2, iter2, flag2] = cg(A, zed, b, M, max_it, tol);

M=S3'*S3;
[x3, error3, iter3, flag3] = cg(A, zed, b, M, max_it, tol);

it0=1:length(error0);it1=1:length(error1);it2=1:length(error2);it3=1:length(error3);
semilogy(it0,error0,'-k',it1,error1,'-g',it2,error2,'-b',it3,error3,'-r')
title('Grd. Conj. Prec. Chol. Incomplet eps=0.1, 0.01 et 0.001')
xlabel('iterations')
ylabel('Residu')
legend('No prec','eps=0.1','eps=0.01','eps=0.001',-1)


