Partial pivoting row swapping

28 ビュー (過去 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 Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by