LU分解(LU Decomposition)是矩阵分解的一种,可以将一个矩阵分解为一个下三角矩阵和一个上三角矩阵的乘积。
主要的算法思路是从下至上地对矩阵A做初等行变换,将对角线左下方的元素变成零,这些行变换的效果等同于左乘一系列单位下三角矩阵,这个单位下三角矩阵就是L.而变换最后得到的上三角矩阵就是U.
分解算法如下:(其中 alpha 代表 L矩阵的元素,beta代表U矩阵的元素)
Matlab实现:
% 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 = zeros(n,n); % initial the n*n matrix m zeros
for i = 1:n; m(i,i) = 1; end % let the diagonal elements be 1
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
其实Matlab也内置有LU分解的函数,说明如下:
- [L,U] = lu(A)
将矩阵A分解的上三角矩阵保存在U当中,将一个“心理学上的”下三角矩阵(例如一个下三角矩阵和置换矩阵的乘积)保存在L中,满足
A=L*U,注意A不必须是方阵。 - [L,U,P] = lu(A)
返回三个矩阵,下三角矩阵L、上三角矩阵U和一个置换矩阵P,满足P*A=L*U。 - [L,U,p] = lu(A,’vector’)
将使用向量而不是矩阵的方式返回置换信息,p是一个行向量,满足A(p,:) = L*U,如果使用[L,U,P] = lu(A,’matrix’)则返回的P是向量,这也是默认的形式