Diagonally Dominant Check & Change for Coefficient Matrix and Result Vector

105 views (last 30 days)
A is the coefficient matrix of the linear equation system. B is the right hand side vector of the linear equation system (which are results). A and b will be used in Gauss-Seidel method to solve the system. This code checks if A is diagonally dominant or not. If it is not diagonally dominant, it changes row orders of A (of course if it is possible to make it diagonally dominant). However, the problem is, the vector b has also to change in the same order that A was changed. How can I do that?
A=[0.3 0.7 1.3;1.2 0.3 -0.5;0.1 0.5 0.3];
b=[7.6;0.1;2.8];
[maxrow,maxind] = max(abs(A),[],2);
if all(maxrow > (sum(abs(A),2) - maxrow)) && isequal(sort(maxind),(1:numel(maxind))')
A(maxind,:) = A;
else
disp('This matrix can never be made to be diagonally dominant')
A = [];
end

Answers (1)

Stephan Ciobanu
Stephan Ciobanu on 13 Jan 2021
Hi! This code should work for your requests:
By the way, I'd rather calculate
Where is the Gauss-Seidel matrix iteration, are the eigenvalues of and (where n is the size of A)
or check if A is symmetric positive-definite.
[row,col] = size(A);
if row~=col % Checking the size of A
error('A must be square matrix!')
end
Diag = diag(A); % Extracting the main diagonal of A
A_mod = A - Diag.*eye(row); % A_mod is A without the main diagonal
for i=1:row
A_sum_row(i) = sum(abs(A_mod(i,:))); % Calculating the sum of each row and
A_sum_col(i) = sum(abs(A_mod(:,i))); % column
end
% Checking if A is already diagonal dominant
if (all(A_sum_row(:)<abs(Diag(:)))==true && all((A_sum_col(:))<abs(Diag(:)))==true)
fprintf('A is diagonal dominant by rows and column \n')
elseif A_sum_row(:)<abs(Diag(:))
fprintf('A is diagonal dominant by rows \n')
elseif A_sum_col(:)<abs(Diag(:))
fprintf('A is diagonal dominant by column \n')
else
result = false;
end
% Try to convert A in a diagonal dominant matrix
if exist('result','var')
[max_val_row,max_pos_row]=max(abs(A));
if length(unique(max_pos_row))~=length(max_pos_row)
fprintf('A isn''t diagonal dominant by rows or column \n')
else
for i = 1:row
A_diag_dom(i,:) = A(max_pos_row(i),:);
B(i) = b(max_pos_row(i));
end
end
end

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!