Writing a matlab program that is diagonally dominant? Very confused help please.

23 views (last 30 days)
Here's what I am trying to do:
Write a matlab program which determines whether a given _n_ by _n_ matrix A is strictly diagonally dominant, if in every row the diagonal entry exceeds the remaining row sum : abs(aii) > Summation of abs(aij) with j=1 and _n_, where j can't = i for each i = 1, 2, ...., _n_. Or equivalently 2*abs(aii) > Summation of abs(aij) with j =1 and _n_ for every i = 1,2 ..., _n_. The output should be a truth value, which can be produced in one vector instruction if done right.
My teacher moves over things very quickly kind of assuming the student knows his assumptions. Help greatly needed, some detailed explanation would be greatly appreciated, I appreciate any time anyone will put into answering this.

Accepted Answer

Geoff
Geoff on 13 Apr 2012
Did you solve this in the end?
Think about it this way... For each row r in your matrix A, you want to test that abs(A(r,r)) is greater than sum(abs(A(r,:))) - abs(A(r,r)).
The : as an index just means 'all values', and since it's indexing the 2nd dimension of A, it means 'all columns'.
And yes, they gave away that this is more easily expressed as 2*abs(A(r,r)) is greater than sum(abs(A(r,:))).
I assume you have learned functions?
function [isdom] = IsDiagDom( A )
% Stuff goes here, return value is 'isdom'.
end
And loops?
isdom = true;
for r = 1:size(A,1)
rowdom = 2 * abs(A(r,r)) > sum(abs(A(r,:)));
isdom = isdom && rowdom;
end
Here, isdom starts as being true, but because we are combining it with && with the dominance value for each row, then if even one row is not dominant, it will become false.
So you can do it that way in your function...
But MatLab is more helpful than this. You can take the sum of each row of A by asking sum to operate on the second dimension (and abs can operate on a whole matrix):
rowsums = sum(abs(A), 2);
Likewise, you can pull out the diagonal of A using diag:
diagvals = abs(diag(A));
Your various operators can also function on matrices (vectors in this case). The following returns a vector of logical (true/false) values, one for each element of your vectors (which have to be the same size):
2 * diagvals > rowsums
And if you want to know whether they are all true, you can use the command all:
isdom = all( 2 * diagvals > rowsums );
Putting it all together, you can actually express this entire thing with a single-line anonymous function:
IsDiagDom = @(A) all( 2 * abs(diag(A)) > sum(abs(A),2) );
This literally says: "given A, tell me if for all rows in A, twice the absolute diagonal of A is larger than the sum of absolute values of A".
And I guess that's why some of us love MatLab =)
  4 Comments
Walter Roberson
Walter Roberson on 16 Mar 2020
% stuff that goes in here%
should be replaced by the code that implements the work required. Geoff discussed in detail what kind of code you would need there.

Sign in to comment.

More Answers (1)

Muhammad Hamza  Rafiq
Muhammad Hamza Rafiq on 1 Jan 2019
please help me there are some errors this progame is for jacobi method and there we also show that matrix is diagnoly dominent. please help me

Categories

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

Community Treasure Hunt

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

Start Hunting!