function priklad_multigrid_2d()

global A_array Lambda_array I_array A0 smoothingSteps
global L

prec = 1e-6;
smoothingSteps = 3;


c = 1;
d = 1;

h = 0.1;
[uzly,prvky,ID,IN,hrany,okrHrany] = refineMesh(c,d,h); %diskretizuj_obdelnik_pro_multigrid(c,d,h);
n = length(uzly);

m = size(prvky,2);
f = ones(1,m);

[A,b] = mkp_2d(uzly,prvky,f,ID);
A0 = A;

figure
u = A\b;
trisurf(prvky',uzly(1,:),uzly(2,:),u)
hold on
pause

for l=1:5
     
%     [noveUzly,noveHrany,novePrvky,noveOkrHrany] =
%     refine2d(uzly,hrany,prvky,okrHrany);
    h = h/2;
    [uzly1,prvky1,ID,IN,hrany,okrajoveHrany,I1]  = refineMesh(c,d,h);
    I_array{l} = I1;
    n = length(uzly1);
    
    m = size(prvky1,2);
%     f1 = ones(1,m);
    f1 = zeros(1,m);
    for i=1:m
        ti = sum(uzly1(:,prvky1(:,i)),2)/3;
        f1(i) = sin(8*pi*ti(1))+sin(4*pi*ti(1)) + sin(pi*ti(1));
    end

    [A1,b1] = mkp_2d(uzly1,prvky1,f1,ID);
    A_array{l} = A1;
    
    Lambda1 = Gershgorin(A1);
    Lambda_array{l} = Lambda1;
    
    [u,k] = multigrid_iter(l,A1,b1,prec);
    
    L = l;
    [u,kpcg] = preconditioned_conjugate_gradients(A1,b1,'multigrid_pro_pcg',zeros(length(b),1),prec);

    plot(uzly1,u,'x')
    hold on
    
    log = sprintf('uroven %d: %d iteraci multigridu, %d iteraci pcg',l,k,kpcg);
    disp(log)
    pause
    
    uzly = uzly1;
    prvky = prvky1;
    
end


figure
order=1:10;
for i=order
    prec = 10^(-i);
    [u,k] = multigrid_iter(l,A1,b1,prec);
    iterations(i) = k;
end
plot(order,iterations);
title('Zavislost poctu iteraci na rel. presnosti')

