任何帮助都会受到赞赏,输出没有意义,我不确定是什么问题。我正在尝试执行标题中列出的功能。
function [L,U,P]=lufactor(A);
A= [2 1 1 0;
4 3 3 1;
8 7 9 5;
6 7 9 8];
[Row1,~] = size(A);
L=eye(Row1);
P=eye(Row1);
U=A;
for i=1:Row1
nrow(i)=i;
end
nrow=nrow';
for k=1:Row1-1;
max=0;
for j = k:Row1 %Go through vector one value at a time
if abs(nrow(j))>max; %Determine if value in vector is greater than the current value
max=abs(nrow(j));
if max ~= k %perform row exchange
U([max,j],:) = U([j,max], :); % interchange rows m and j in U
P([max,j],:) = P([j,max], :); % interchange rows m and j in P
if j >= 2
L([max,j],1:j-1) = L([j,max], 1:j-1); % interchange rows m and j in columns 1:j-1 of L
end
end
end
for i = j+1:Row1
L(i, j) = U(i, j) / U(j, j);
U(i, :) = U(i, :) - L(i, j)*U(j, :);
end
end
end
[L,U,P]
当前输出,结果组织为[L,U,P]:
1 0 0 0 2 1 1 0 1 0 0 0
2 1 0 0 0 1 1 1 0 1 0 0
4 0 1 0 0 0 2 2 0 0 1 0
3 0 0 1 0 0 0 2 0 0 0 1
预期产出:
L =
1.0000 0 0 0
0.7500 1.0000 0 0
0.5000 -0.2857 1.0000 0
0.2500 -0.4286 0.3333 1.0000
U =
8.0000 7.0000 9.0000 5.0000
0 1.7500 2.2500 4.2500
0 0 -0.8571 -0.2857
0 0 0 0.6667
P =
0 0 1 0
0 0 0 1
0 1 0 0
1 0 0 0