% Vypocet inverzni matice pomoci LU-rozkladu:
% --------------------------------------------

% KaSk: Pr.10.3/90
A=[3,2,1;-1,2,2;2,1,-2]
A =
      3            2            1
     -1            2            2
      2            1           -2
b=[2;-1;1]
b =
      2
     -1
      1
det(A)
ans =
    -19
% Matice je regularni
% Nalezeni inverze LU-rozkladem
% =============================
[L,U,p,jb] = LUdemo(A)
m =
      3
n =
      3
A =
      3            2            1            1
     -1            2            2            2
      2            1           -2            3
Vlozte polohu dalsiho schodu ve tvaru [r,s]: [2 1]
r =
      2
jb =
      1
k =
      1
PREHOZENI k-ho a r-teho RADKU:
A =
     -1            2            2            2
      3            2            1            1
      2            1           -2            3
ULOZENI TRANSFORMACNICH SKALARU MISTO NUL DO s-teho SLOUPCE:
A =
     -1            2            2            2
     -3            2            1            1
     -2            1           -2            3
ELIMINACE ZBYVAJICICH SLOUPCU s+1,...,n:
A =
     -1            2            2            2
     -3            8            7            1
     -2            5            2            3
Posledni eliminovany sloupec:
s =
      1
Vlozte polohu dalsiho schodu ve tvaru [r,s]: [3 2]
rs =
      3            2
r =
      3
jb =
      1            2
k =
      2
PREHOZENI k-ho a r-teho RADKU:
A =
     -1            2            2            2
     -2            5            2            3
     -3            8            7            1
ULOZENI TRANSFORMACNICH SKALARU MISTO NUL DO s-teho SLOUPCE:
A =
     -1            2            2            2
     -2            5            2            3
     -3           8/5           7            1
ELIMINACE ZBYVAJICICH SLOUPCU s+1,...,n:
A =
     -1            2            2            2
     -2            5            2            3
     -3           8/5         19/5           1
Posledni eliminovany sloupec:
s =
      2
Vlozte polohu dalsiho schodu ve tvaru [r,s]: 3
rs =
      3
jb =
      1            2            3
L =
      1            0            0
     -2            1            0
     -3           8/5           1
U =
     -1            2            2
      0            5            2
      0            0          19/5
p =
      2
      3
      1
jb =
      1            2            3
% Kontrola
A(p,:)
ans =
     -1            2            2
      2            1           -2
      3            2            1
L*U
ans =
     -1            2            2
      2            1           -2
      3            2            1
B=eye(3)
B =
      1            0            0
      0            1            0
      0            0            1
PB=B(p,:)
PB =
      0            1            0
      0            0            1
      1            0            0
% Resime L*Z=PB:
Z = L\PB
Z =
      0            1            0
      0            2            1
      1          -1/5         -8/5
% Resime U*X=Z
X=U\Z
X =
     6/19        -5/19        -2/19
    -2/19         8/19         7/19
     5/19        -1/19        -8/19
% Kontrola
inv(A)

ans =

     6/19        -5/19        -2/19
    -2/19         8/19         7/19
     5/19        -1/19        -8/19

% Reseni
x=X*b

x =

    15/19
    -5/19
     3/19

% Kontrola
x=A\b

x =

    15/19
    -5/19
     3/19

diary off
