Gaussian Elimination with Partial Pivoting, how do I swap rows

조회 수: 9 (최근 30일)
Susanna Westersund
Susanna Westersund 2020년 10월 26일
댓글: WAGDI AL-BAADANI 2021년 3월 26일
function output= pivGauss(A)
%% Problem Setup
% Get the size of the problem (number of equations / unknowns)
i=1;
[n,~]=size(A);
% Get the coefficient matrix and the RHS vector
C=A(1:n,1:n);
rhs=(1:n,1:n+1);
%% Forward Elimination w/ Partial Piviting
% for each FE iteration:
% 1) find the rows to switch (hint: find the right commands to use from ML3 Tutorial slides)
% 2) switch the rows (hint: you can create a temp variable to hold one set of the values as an intermediate step)
% 3) perform elimination of unknown variables (reminder: use nested loops and MATLAB vector/matrix operations to replace unnecessary loops)
P=(1:n); %pivoting vector
[m,I]=max(abs(A(k:n,k))
for i=1:n-1
piv=i; %selecting the pivot
for j=(i+1):n
if abs(A(j,i))>abs(A(i,i))
U=A(i,:);
A(i,:)=A(j,:);
A(j,:)=U;
end
end
for j=i+1:n
x=A(j,i)/A(i,i);
for k=i+1:n+1
A(j,k)=A(j,k)-x*A(i,k);
end
end
fprintf('\n\nIteration #%2d:\n', i)
fprintf(' The row that is to be switched is %.4e\n', U)
fprintf(' The coefficient matrix is ', A)
fprintf(' The right-hand side vector is ', x)
end
%% Back Substitution
% Use Question 4 in C17_InClass as a reference
% This code has unnecessary loops that can be replaced by MATLAB vector/matrix operations though.
x(n)=Z(n)/U(n,n);
for i=n-1:-1:1
sum=Z(i);
for j=i+1:1:n
sum=sum-U(i,j)*x(j);
end
x(i)=sum/U(i,i);
end
%% Final Display and Output
output=[U, x]
disp(output)
end
  댓글 수: 1
WAGDI AL-BAADANI
WAGDI AL-BAADANI 2021년 3월 26일
function output= pivGauss(A)
Error: Function definition not supported in this context. Create functions in code file.

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

답변 (1개)

Pratyush Roy
Pratyush Roy 2020년 11월 12일
Hi Susanna,
The variable 'k' seems to be not defined for the line given below:
[m,I]=max(abs(A(k:n,k))
Can you furnish some more information regarding the variable k?
Regards,
Pratyush

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by