필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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.

채택된 답변

Geoff
Geoff 2012년 4월 13일
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
Samantha Strahan
Samantha Strahan 2020년 3월 16일
what the hexk is " % stuff that goes in here%"
Walter Roberson
Walter Roberson 2020년 3월 16일
% 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.

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

추가 답변 (1개)

Muhammad Hamza  Rafiq
Muhammad Hamza Rafiq 2019년 1월 1일
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
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 1월 1일
You have
while err>tol
but the body of your loop never changes either err or tol so the loop can never be exited.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by