Writing a matlab program that is diagonally dominant? Very confused help please.
18 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
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
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
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 Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!