how to make upper triangular matrix easlily by pivoting(Gauss elimination)

4 vues (au cours des 30 derniers jours)
When i do Gauss elimination in 'Matlab' i made a code like this
in following case(3x3matrix)
First step is making a augmented matrix and after that i visually chek the 1 column element and which elements absolute value is bigger than 1x1 element and pivot 1st row and 2nd row(for reduce error)
but this type of pivoting method is not efficient.
So i want to make code continuity so i use "if" and "elseif" to pivoting easily (the last footnote of the code)
But at the 3x3 matrix only use "elseif" just one time but if the given matrix is nxn matrix i have to use n-1 time
so i want to make the code like this
"if the abs(a(1,1))is smaller than other elments of 1st column(absolute value), the row that have biggest element pivot with 1st row "
How can i make this code?
a=[0.2 0.4 0.8; 0.6 0.6 0.2; 0.4 0.8 1];
x=['x1'; 'x2'; 'x3'];
y=[18;27;34];
%Augmented matrix
a(:,4)=y
%pivoting1 : 1st_row<>2nd_row
temp=a(2,:);
a(2,:)=a(1,:);
a(1,:)=temp
%upper triangular1
a(2,:)=a(2,:)-a(1,:)*a(2,1)/a(1,1);
a(3,:)=a(3,:)-a(1,:)*a(3,1)/a(1,1)
%pivoting2 : 2nd_row<>3rd_row
temp2=a(2,:);
a(2,:)=a(3,:);
a(3,:)=temp2
%upper triangular2
a(3,:)=a(3,:)-a(2,:)*a(3,2)/a(2,2)
%final
x3=a(3,4)/a(3,3)
x2=(a(2,4)-a(2,3)*x3)/a(2,2)
x1=(a(1,4)-x2*a(1,2)-x3*a(1,3))/a(1,1)
% if & elseif
a=[0.2 0.4 0.8; 0.6 0.6 0.2; 0.4 0.8 1];
x=['x1'; 'x2'; 'x3'];
y=[18;27;34];
if abs(a(1,1)) < abs(a(2,1));
temp1=a(2,:);
a(2,:)=a(1,:);
a(1,:)=temp1
elseif abs(a(1,1)) < abs(a(3,1));
temp2=a(3,:);
a(3,:)=a(1,:);
a(1,:)=temp2
end

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 22 Sep 2023
"so i want to make the code like this
"if the abs(a(1,1))is smaller than other elments of 1st column(absolute value), the row that have biggest element pivot with 1st row "
How can i make this code? "
%Find the index of the row whose 1st element has the highest absolute value
[~,idx] = max(abs(a(:,1)))
%pivot
a([1 idx],:) = a([idx 1 ],:);
  2 commentaires
지우
지우 le 23 Sep 2023
thank you for your help! after use this code is runnig well
Dyuman Joshi
Dyuman Joshi le 23 Sep 2023
Glad to have helped!

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 22 Sep 2023
Have you seen these shared codes in MATLAB file exchange:
https://www.mathworks.com/matlabcentral/fileexchange/12752-method-of-elimination-of-gauss-with-pivoting-partial?s_tid=srchtitle_support_results_1_Gauss%20elimination%20method%20by%20pivoting%20
  1 commentaire
지우
지우 le 23 Sep 2023
thank you but the language in the footnote can't understand but i'll try. thanks!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by