Adding counter to count the number of row swaps for GEA
조회 수: 4 (최근 30일)
이전 댓글 표시
I want to add a counter to a function to count the number of row interchanges for a determinant calculation but it isnt incrementing.
Im getting the right answer for the determinant but only 0 for the count.
here is my code and thanks for your guidance:
function Det = Det_with_RI(A)
% This function calculates determinant of a square matrix A.
%
% Input : A -- a square matrix.
% Output : Det -- determiant of A.
Det = 1; % Initial value of determinant: expecting to be changed.
Count = 0; % inital value for number of row swaps
function ProcessPivotElement
if abs(A(k,k))<10^(-10),
% We use here a variable ii in the following for-cycle,
% unlike the usual i variable, since a child-function
% sees parent-function's variables and changes them.
for ii=k+1:n,
if A(ii,k)~=0, % If a(i,k) is non-zero,
cc=A(ii,:); % swap row i and row k.
A(ii,:)=A(k,:);
A(k,:)=cc;
Count = Count + 1; % Increment the row swap counter
disp(Count);
return
end
Det = Det*(-1);
end
error('The system is indeterminate.');
end
end
[m,n] = size(A);
if m~=n error('The argument must be a square matrix.'); end;
% Forward Phase of GEA:
for k = 1:n-1
ProcessPivotElement;
for i = k+1:n
% Pivoting multiplier:
m = A(i,k)/A(k,k);
% Now we apply Elementary Row Operation of type~I.
% As is known, ERO's of type~I do not change determinant.
A(i,:)=A(i,:)-m*A(k,:);
end
end
% Now, A is an upper triangular matrix.
% In calculation of determinant the Backward Phase is not necessary.
% It is because determinant of an upper triangular matrix
% is the product of its diagonal entries, which we calculate here:
for k=1:n, Det=Det*A(k,k);
end
disp(Count)
det(A)
end
댓글 수: 2
답변 (1개)
Shravan Kumar Vankaramoni
2021년 10월 12일
Hi,
if abs(A(k,k))<10^(-10)
The above line looks suspicious to me. What is the range of values you have in your input matrix (A). Because the above line is checking whether the value at A(k,k) is less than 10^(-10) i.e 0.0000000001. So if you have values less than 10^(-10), count will be incremented otherwise count is 0.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!