Partial pivoting row swapping

31 views (last 30 days)
Rabia Sonmez
Rabia Sonmez on 11 Sep 2021
Commented: Rabia Sonmez on 13 Sep 2021
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.

Accepted Answer

Chunru
Chunru on 12 Sep 2021
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 Comments
Chunru
Chunru on 12 Sep 2021
[~, imax] = max() is to find which element has the maximum. doc max for details.
Rabia Sonmez
Rabia Sonmez on 13 Sep 2021
Okay thank you 🙏

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!