# Partial pivoting problem in matrix

조회 수: 2(최근 30일)
Netanel Malihi 2021년 12월 4일
답변: Pranjal Kaura 2021년 12월 30일
I'm getting a mistake in my matrix when trying to get the max number at the column and swiching rows , I'm pretty sure its got to do with an incorrect index.
Im using the partial pivoting method.
clear;
clc;
n=5;
v=repelem(4,n);
A=diag(v);
i=1;
j=1;
A(1,n)=-1
A(n,1)=-1
for i=1:n
for j=1:n
if i==j+1 ||j==i+1
A(i,j)=-1;
end
end
end
b=1:n;
B=b'/n;
A=[A,B]
for k=1:n-1
[max_A1,row_i]=max(abs(A(k:end,k)))
if row_i~=k
A1=A(k,:)
A(k,:)=A(k,:)
A(row_i,:)=A1
else
end
for i=k+1:n
L=A(i,k)/A(k,k)
for j=k:n+1
A(i,j)=A(i,j)-L*A(k,j)
end
end
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

Pranjal Kaura 2021년 12월 30일
Hey,
clear;
clc;
n=5;
v=repelem(4,n);
A=diag(v);
i=1;
j=1;
A(1,n)=-1;
A(n,1)=-1;
for i=1:n
for j=1:n
if i==j+1 ||j==i+1
A(i,j)=-1;
end
end
end
b=1:n;
B=b'/n;
A=[A,B];
% test matrices
% A = [25 5 1 106.8;
% 64 8 1 177.2;
% 144 12 1 279.2];
% A = [1 2 -4 3;
% 2 5 -6 10;
% -2 -7 3 -21;
% 2 8 -5 38];
% n = 4;
%partial pivoting code
for k=1:n-1
[max_A1,row_i]=max(abs(A(k:end,k)));
row_i = row_i - 1 + k;
if row_i~=k
A1=A(k,:);
A(k,:)=A(row_i,:);
A(row_i,:) = A1;
end
for i=k+1:n
L=A(i,k)/A(k,k);
A(i, k:end) = A(i, k:end) - A(k, k:end)*L;
% for j=k:n+1
% A(i,j)=A(i,j)-L*A(k,j)
% end
end
end
Hope this helps!

댓글을 달려면 로그인하십시오.

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by