1. LU decomposition
It has been proved in linear algebra that if the square matrix is non-singular, that is, the determinant of is not 0, the LU decomposition always exists, and its form is:
For example, for a fourth-order matrix, it can be decomposed into:
Find the pattern:
- The first row has no dependent factors, so it can be directly concluded that the first column only depends on the first row, so after the first row is obtained, the first column can also be calculated.
- In the same way, the second main row is the same. You can calculate the main row first, and then calculate the main column, which is calculated in the order of the solid line first, and then the dotted line as shown in the figure below.
- Observe carefully, in fact, as long as the main diagonal element of the U matrix is calculated first, it does not matter whether the row or the column is calculated first, because the unknown lu element of the corresponding row and column does not depend on other parameters.
- Each element in the original matrix is used only once. After the corresponding one is calculated , its information has been diffused into the entire calculation structure and can be discarded. Subsequent calculations are no longer dependent. For computer program calculations, the advantage is to optimize the memory of the algorithm and provide a grip.
According to the law found above, try to derive the calculation formula as follows:
Combine linearly according to the line of U
get:
In addition, according to the linear combination of the columns of L
So get:
For the inner matrix, because of time ,, so, in order to find the value of time, you can calculate:
and so:
Because of the time ,, so, in order to find the value of time, you can calculate:
So, get:
According to the above two formulas, coding on octave is realized as:
% find the LU factorization of the matrix
% input:
% a: the matrix need to be factorize
% n: the number of the low or column in the matrix
% output:
% no output
function LU(a,n)
m=eye(n); %genernate the unit matrix.
for j = 1 : n-1
if abs(a(j,j))<eps;
error('zero pivot encountered'); % when the zero pivot happens,end the process
end
for i = j+1 : n
mult = a(i,j)/a(j,j);
m(i,j) = mult;
for k = j:n
a(i,k) = a(i,k) - mult*a(j,k);
end
end
end
disp(' L='); disp(m);
disp(' U='); disp(a);
disp(' LU='); disp(m*a); % to check if the result is right
The result of the operation is:
The above program requires that each order of the matrix is not 0, otherwise the program will report an error. E.g:
This problem has time to be fixed.