Contents

LU dekompozice

smysl = kazdou matici A lze rozepsat jako soucin dvou zarucene trojuhelnikovych matic.

vyuziti : napr. Gauss-Jordanova metoda pracuje prave s maticemi v trojuhelnikovem tvatu

V podstate kod teto fce jen rozepisuje pomoci mnoha FOR cyklu vzorce, uvedene v referenci.

for reference see >>> L-U dekompozice ve skriptech

na vstupu pozadujeme regularni, ctvercovou matici

04.03.2019

    A = load ('matice.txt');  % lze predat na vstupu do fce : lu_decomposition (A)
    n = size(A,1);
    U = zeros(n);       %prepared : nulova
    L = eye(n);     %prepared : na diag jednicky

    for diagonala = 1:n     %predstavuje radek (j)
        for sloupec = diagonala:n     %predstavuje sloupec (j)
            soucet = 0;
            for k = 1:(diagonala-1)
                soucet = soucet + L(diagonala, k) * U(k, sloupec);
            end
            U(diagonala,sloupec) = A(diagonala,sloupec) - soucet;
        end

        for radek = (diagonala + 1):n
            soucet = 0;
            for k = 1:(diagonala - 1)
                soucet = soucet + L(radek,k) * U(k, diagonala);
            end
            L(radek,diagonala) = (A(radek,diagonala) - soucet) / U(diagonala,diagonala);
        end
    end

%vystup:
   disp('matrix A was LU-decomposed to matrix L and U >>>')
   A
   L
   U
matrix A was LU-decomposed to matrix L and U >>>

A =

     1     1     2     2     3
     1     2     3     4     5
     5     4     3     2     1
     1     2     3     2     1
     1     3     1     5     1


L =

   1.000000000000000                   0                   0                   0                   0
   1.000000000000000   1.000000000000000                   0                   0                   0
   5.000000000000000  -1.000000000000000   1.000000000000000                   0                   0
   1.000000000000000   1.000000000000000                   0   1.000000000000000                   0
   1.000000000000000   2.000000000000000   0.500000000000000  -1.000000000000000   1.000000000000000


U =

     1     1     2     2     3
     0     1     1     2     2
     0     0    -6    -6   -12
     0     0     0    -2    -4
     0     0     0     0    -4

kontrolni soucin

   'A = '
   L*U
ans =

A = 


ans =

     1     1     2     2     3
     1     2     3     4     5
     5     4     3     2     1
     1     2     3     2     1
     1     3     1     5     1

determinanty - zvlastni nesrovnalost

   det_L = det(L);
   det_U = det(U);
   det_A = det_L * det_U;
   disp(det_A);
   disp(det(A));

   if (det(A) ~= det_A)
       error('chyba poctu!!!')
   end
Error using ==> takepicture at 58
chyba poctu!!!