how to make upper triangular matrix easlily by pivoting(Gauss elimination)
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
Sulaymon Eshkabilov
il 22 Set 2023
0 voti
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 Commento
지우
il 23 Set 2023
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!