Partial pivoting row swapping

25 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(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