问题描述
我正在尝试主要基于部分分解的LU分解来处理lu分解旋转Matlab
function [L,U,P] = lup(A)
n = length(A);
L = eye(n);
U = zeros(n);
P = eye(n);
for k=1:n-1
% find the entry in the left column with the largest abs value (pivot)
[~,r] = max(abs(A(k:end,k)));
r = n-(n-k+1)+r;
A([k r],:) = A([r k],:);
P([k r],:) = P([r k],:);
L([k r],:) = L([r k],:);
% from the pivot down divide by the pivot
L(k+1:n,k) = A(k+1:n,k) / A(k,k);
U(k,1:n) = A(k,1:n);
A(k+1:n,1:n) = A(k+1:n,1:n) - L(k+1:n,k)*A(k,1:n);
end
U(:,end) = A(:,end);
end
它似乎适用于大多数矩阵(等于matlab lu函数),但是以下矩阵似乎会产生不同的结果:
It seems to work for most matrices (equal to the matlab lu function), however the following matrix seems to produce different results:
A = [
3 -7 -2 2
-3 5 1 0
6 -4 0 -5
-9 5 -5 12
];
我只是不知道出了什么问题.在链接的文章中提到的矩阵上似乎可以正常工作
I just can't figure out what is going wrong. It seems to work fine on the matrices mentioned in the linked post
推荐答案
您非常接近.我一共换了三行
you were pretty close. I changed three lines total
for k=1:n-1
变为for k=1:n
,我们不执行-1,因为我们也希望使用您的方法来获取L(n,n)=u(n,n)/u(n,n)=1
,因此我们将其排除在外
for k=1:n-1
became for k=1:n
we don't do the -1 because we also want to get L(n,n)=u(n,n)/u(n,n)=1
with your method we were leaving this out
L(k+1:n,k) = A(k+1:n,k) / A(k,k);
成为L(k:n,k) = A(k:n,k) / A(k,k);
,因为您遗漏了L(k,k)=A(k,k)/A(k,k)=1
L(k+1:n,k) = A(k+1:n,k) / A(k,k);
became L(k:n,k) = A(k:n,k) / A(k,k);
because you were leaving out L(k,k)=A(k,k)/A(k,k)=1
由于k+1
的变化,我们不需要从L的单位矩阵开始,因为我们现在在对角线上重现1,因此L=eyes(n);
变为L=zeros(n);
because the k+1
change we dont need to start with an identity matrix for L since we are now reproducing the 1's on the diagonals so L=eyes(n);
became L=zeros(n);
和完整的代码
function [L,U,P] = lup(A)
% lup factorization with partial pivoting
% [L,U,P] = lup(A) returns unit lower triangular matrix L, upper
% triangular matrix U, and permutation matrix P so that P*A = L*U.
n = length(A);
L = zeros(n);
U = zeros(n);
P = eye(n);
for k=1:n
% find the entry in the left column with the largest abs value (pivot)
[~,r] = max(abs(A(k:end,k)));
r = n-(n-k+1)+r;
A([k r],:) = A([r k],:);
P([k r],:) = P([r k],:);
L([k r],:) = L([r k],:);
% from the pivot down divide by the pivot
L(k:n,k) = A(k:n,k) / A(k,k);
U(k,1:n) = A(k,1:n);
A(k+1:n,1:n) = A(k+1:n,1:n) - L(k+1:n,k)*A(k,1:n);
end
U(:,end) = A(:,end);
end
这篇关于MATLAB LU分解部分旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!