Code stops after first iteration

So I have this MATLAB code for LU decomposition with partial pivoting and the code stops after doing one iteration even though I have added a loop. What could possibly be the reason
function[Q,P]=lupivot(A)
%%LU factorization with partial pivoting%%
[i,j]=size(A);
n=length(A);
if i~=j
disp('The Matrix must be square to proceed further');
end
P=eye(n); %set permutation matrix to I%
Q=zeros(n);%set Q matrix%
for j=1
Q(:,1)=A(:,1);%initialize the first column to first column of A
[temp,pos] = max(abs(Q(1:n,1)));%Finding the maximum value in the column%
if Q(1,1)==temp %Checking if the diagonal element has maximum value%
for k=2:n
Q(1,k)= 1/Q(1,1)*((A(1,k)));
end
else
Rowswap = pos;
Q([Rowswap, 1],:) = Q([1, Rowswap],:);
A([Rowswap, 1],:) = A([1, Rowswap],:);%Swapping the corresponding rows of Q,P and updating A
P([Rowswap, 1],:) = P([1, Rowswap],:);
for k=2:n
Q(1,k)= 1/Q(1,1)*((A(1,k)));
for k=2:n
Q(k,2)= A(k,2)-((Q(k,1)*Q(1,2)));
end
end
end
end
for j=2:n
[temp1,pos1] = max(abs(Q(j:n,j)));%Finding the maximum value in the column%
if Q(j,j)==temp1 %Checking if the diagonal element has maximum value%
for k=j+1:n
Q(j,k)= 1/Q(j,j)*((A(j,k))-(Q(j,1:j-1)*Q(1:j-1,k)));
for k=j:n
Q(k,j)= A(k,j)-(Q(k,1:j-1)*Q(1:j-1,j));
end
end
else
rowswap = j-1+pos1;
Q([rowswap, j],:) = Q([j, rowswap],:);
A([rowswap, j],:) = A([j, rowswap],:);%Swapping the corresponding rows of Q,P and updating A
P([rowswap, j],:) = P([j, rowswap],:);
for k=j+1:n
Q(j,k)= 1/Q(j,j)*((A(j,k))-(Q(j,1:j-1)*Q(1:j-1,k)));
for k=j:n
Q(k,j)= A(k,j)-(Q(k,1:j-1)*Q(1:j-1,j));
end
end
j=j+1;
end
end
end
OUTPUT
>> [Q,P]=lupivot(A)
Q =
12.0000 -0.6667 0.3333 0.8333
3.0000 -11.0000 -0.1818 -0.0455
6.0000 2.0000 0.3636 -Inf
-6.0000 0 4.0000 0

8 个评论

Is this the loop you have added?
for j=1
Anyway, you should indicate where you have added something and what your expected output is.
for j=2:n This is my loop
You've got double-nested loops on the same index in
...
for k=j+1:n
Q(j,k)= 1/Q(j,j)*((A(j,k))-(Q(j,1:j-1)*Q(1:j-1,k)));
for k=j:n
Q(k,j)= A(k,j)-(Q(k,1:j-1)*Q(1:j-1,j));
end
end
...
I've not tried to read the code see if could decipher what might possibly be intended here, but this can't be correct...
This is the flowchart of my code.
How can I change the index for double nested loop?
Hi,
According to dpb, double nested on same index
for k=1:1:n
for k=1:1:n
end
end
%Try change the inner loop index k to other name (j):
for k=1:1:n
for j=1:1:n
end
end
You mean like this?
for k=j+1:n
Q(j,k)= 1/Q(j,j)*((A(j,k))-(Q(j,1:j-1)*Q(1:j-1,k)));
for m=j:n
Q(m,j)= A(m,j)-(Q(m,1:j-1)*Q(1:j-1,j));
end
It is recommended to avoid i and j as loop indices, as these are used for the imaginary unit:
It is much clearer and less buggy to use ii and jj:
for ii = 1:n
...
for jj = 1:n
...
end
end

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by