Gaussian Elimination with Partial Pivoting, how do I swap rows
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
WAGDI AL-BAADANI
am 26 Mär. 2021
function output= pivGauss(A)
↑
Error: Function definition not supported in this context. Create functions in code file.
Antworten (1)
Pratyush Roy
am 12 Nov. 2020
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
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!