Weird results using LU
显示 更早的评论
Hi
Today I was testing my own implementation of the LU Descomposition
function [L,U]=LUDescomp(Coefs)
[N,~]=size(Coefs);
L=eye(N,N);%Genera una matriz identidad
%Eliminacion de variables
for i=1:N-1
%Proceso de Pivoteo Parcial
Ai=Coefs(i:N,i);
[~ ,Ind]=max(abs(Ai));
Ind=Ind+i-1;
if(Ind~=i)
Temp=Coefs(i,:);
Coefs(i,:)=Coefs(Ind,:);
Coefs(Ind,:)=Temp;
end
Piv=Coefs(i,i);
for j=i+1:N
ElementoPiv=Coefs(j,i)/Piv;
L(j,i)=ElementoPiv;
ModifiedRow=Coefs(i,:)*ElementoPiv;
Coefs(j,:)=Coefs(j,:)-ModifiedRow;
end
end
U=Coefs;
end
So i try to compare the results with the lu function in Matlab, and something weird happen
x =
1 2 6
4 8 -1
-2 3 -5
>>[L,U,P]=lu(x)
L =
1.0000 0 0
-0.5000 1.0000 0
0.2500 0 1.0000
U =
4.0000 8.0000 -1.0000
0 7.0000 -5.5000
0 0 6.2500
P =
0 1 0
0 0 1
1 0 0
>> [L,U]=LUDescomp(x)
L =
1.0000 0 0
0.2500 1.0000 0
-0.5000 0 1.0000
U =
4.0000 8.0000 -1.0000
0 7.0000 -5.5000
0 0 6.2500
As you can see, the answers are almost same, except in the L matrix , the values 0.25 and -0.5 are interchanged in my matrix and i have checked my algorithm many times and i can´t find what's wrong.
I would appreciate all the help i could get.
Thank you
回答(1 个)
John BG
2016-4-17
Fabio
your observation of a single case and then saying that the 1st column elements other than (1,1) are inverted or swapped is not correct.
Check for larger dimension with:
x=randi(13,6)
x =
13 4 4 8 8 2
8 11 9 12 1 8
2 4 7 4 1 7
2 13 5 10 7 1
4 5 11 10 11 5
11 3 8 5 13 3
>> [L,U,P]=lu(x)
L =
1.0000 0 0 0 0 0
0.1538 1.0000 0 0 0 0
0.3077 0.3043 1.0000 0 0 0
0.8462 -0.0311 0.5633 1.0000 0 0
0.6154 0.6894 0.4168 0.2355 1.0000 0
0.1538 0.2733 0.6149 0.6183 0.6685 1.0000
U =
13.0000 4.0000 4.0000 8.0000 8.0000 2.0000
0 12.3846 4.3846 8.7692 5.7692 0.6923
0 0 8.4348 4.8696 6.7826 4.1739
0 0 0 -4.2401 2.5891 -1.0221
0 0 0 0 -11.3373 4.7930
0 0 0 0 0 1.3646
P =
1 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 1 0 0 0 0
0 0 1 0 0 0
>> [L2,U2]=LUDescomp(x)
L2 =
1.0000 0 0 0 0 0
0.6154 1.0000 0 0 0 0
0.1538 0.2733 1.0000 0 0 0
0.1538 0.6894 0.4168 1.0000 0 0
0.3077 0.3043 0.6149 0.6183 1.0000 0
0.8462 -0.0311 0.5633 0.2355 0.6685 1.0000
U2 =
13.0000 4.0000 4.0000 8.0000 8.0000 2.0000
0 12.3846 4.3846 8.7692 5.7692 0.6923
0 0.0000 8.4348 4.8696 6.7826 4.1739
0 -0.0000 0 -4.2401 2.5891 -1.0221
0 -0.0000 0 0 -11.3373 4.7930
0 0.0000 0 0 0 1.3646
It's not L 1st column elements below 1 are swapped.
It's all elements below diagonal, of all columns are somehow shuffled.
Find out where in your function the shuffling takes place and fix it.
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!