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

조회 수: 10 (최근 30일)
지우 . 2023년 9월 22일
댓글: Dyuman Joshi . 2023년 9월 23일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

Dyuman Joshi 2023년 9월 22일
"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없음 표시없음 숨기기
지우 2023년 9월 23일
thank you for your help! after use this code is runnig well
Dyuman Joshi 2023년 9월 23일

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Sulaymon Eshkabilov 2023년 9월 22일
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
지우 2023년 9월 23일
thank you but the language in the footnote can't understand but i'll try. thanks!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!