% CVICENI - 6

% Metoda Monte-Carlo
IN = 0;
N = 10^6;
for i=1:N
    x = rand(1,2);
    if (norm(x,2)<1)
        IN = IN+1;
    end
end

S = IN / N * 4
Sorig = pi

%------- Gaussova eliminace-------------%

A = [1 1 1; 2 2 2; 0 1 2]
b = [1;5;7]

x = gauss_elimination(A,b)
% overeni
x1 = A\b


function [x] = gauss_elimination(A,b)
% ... lze jeste vylepsit
n  = size(A,1);
Ab = [A b];

% DOPREDNY CHOD %%%%%%%%%%%%%%%%%%%%%%
for i = 1:n-1    
    % prohozeni radku
    k = 0;
    while Ab(k+i,i) == 0 && k < n-i
        k = k+1;
    end
    if k >= n-i
        fprintf("Matice je singularni")
        x = Inf(n,1);
        return;
    else
        pom = Ab(i,:);
        Ab(i,:) = Ab(k+i,:);
        Ab(k+i,:) = pom;
    end
    % vynulovani i-teho sloupce
    for j = i+1:n
        if Ab(j,i) ~= 0
            Ab(j,:) = Ab(j,:) - Ab(j,i) / Ab(i,i) * Ab(i,:);
        end
    end
end

% ZPETNY CHOD %%%%%%%%%%%%%%%%%%%%%%
if Ab(end,end-1) == 0
    fprintf("Matice je singularni")
else
    x  = zeros(1,n);
    bb = Ab(:,end)
    AA = Ab(:,1:end-1)
    for i=0:n-1
        res = AA(n-i,:) * x';
        x(n-i) = (bb(n-i) - res) / AA(n-i,n-i);
    end
end
%---------------------------------------------------%