Partial pivoting row swapping

조회 수: 29 (최근 30일)
Rabia Sonmez
Rabia Sonmez 2021년 9월 11일
댓글: Rabia Sonmez 2021년 9월 13일
a=[1 4 3 5;2 0 2 6;1 1 0 5;1 2 3 4];
b=[5;6;7;10]
ab=[a b]
for i=1,3
if ab(i,i)< abs(max(ab(:,i)))
piv=ab(i,:)
ab(i,:)=ab(i+1,:)
ab(i+1,:)=piv
for j=2:4
ab(j,:)=ab(j,:)-ab(j,1)/ab(1,1)*ab(1,:)
end
end
end
Guys when I run the code I get
ab =
2 0 2 6 6
0 4 2 2 2
0 1 -1 2 4
0 2 2 1 7
I should make zero below the diagonal matrix. I did firs column but the others I could not. Can you help me? Thank you in advance.

채택된 답변

Chunru
Chunru 2021년 9월 12일
a=[1 4 3 5;2 0 2 6;1 1 0 5;1 2 3 4];
b=[5;6;7;10]
b = 4×1
5 6 7 10
ab=[a b]
ab = 4×5
1 4 3 5 5 2 0 2 6 6 1 1 0 5 7 1 2 3 4 10
for i=1:size(ab, 1)-1
% pivoting
[~, imax] = max(abs(ab(i:end, i)));
%ab(i:end, i)
%imax
% exchange rows
piv=ab(imax+(i-1),:);
ab(imax+(i-1), :)=ab(i, :);
ab(i, :)=piv;
ab
% elimination
for j=i+1:size(ab, 1)
ab(j,:)=ab(j,:)-ab(j,i)/ab(i,i)*ab(i,:)
end
end
ab = 4×5
2 0 2 6 6 1 4 3 5 5 1 1 0 5 7 1 2 3 4 10
ab = 4×5
2 0 2 6 6 0 4 2 2 2 1 1 0 5 7 1 2 3 4 10
ab = 4×5
2 0 2 6 6 0 4 2 2 2 0 1 -1 2 4 1 2 3 4 10
ab = 4×5
2 0 2 6 6 0 4 2 2 2 0 1 -1 2 4 0 2 2 1 7
ab = 4×5
2 0 2 6 6 0 4 2 2 2 0 1 -1 2 4 0 2 2 1 7
ab = 4×5
2.0000 0 2.0000 6.0000 6.0000 0 4.0000 2.0000 2.0000 2.0000 0 0 -1.5000 1.5000 3.5000 0 2.0000 2.0000 1.0000 7.0000
ab = 4×5
2.0000 0 2.0000 6.0000 6.0000 0 4.0000 2.0000 2.0000 2.0000 0 0 -1.5000 1.5000 3.5000 0 0 1.0000 0 6.0000
ab = 4×5
2.0000 0 2.0000 6.0000 6.0000 0 4.0000 2.0000 2.0000 2.0000 0 0 -1.5000 1.5000 3.5000 0 0 1.0000 0 6.0000
ab = 4×5
2.0000 0 2.0000 6.0000 6.0000 0 4.0000 2.0000 2.0000 2.0000 0 0 -1.5000 1.5000 3.5000 0 0 0 1.0000 8.3333
  댓글 수: 4
Chunru
Chunru 2021년 9월 12일
[~, imax] = max() is to find which element has the maximum. doc max for details.
Rabia Sonmez
Rabia Sonmez 2021년 9월 13일
Okay thank you 🙏

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by