Writing a matlab program that is diagonally dominant? Very confused help please.
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Reelz
el 6 de Abr. de 2012
Comentada: Walter Roberson
el 16 de Mzo. de 2020
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 comentarios
Respuesta aceptada
Geoff
el 13 de Abr. de 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 comentarios
Walter Roberson
el 16 de Mzo. de 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.
Más respuestas (1)
Muhammad Hamza Rafiq
el 1 de En. de 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
1 comentario
Walter Roberson
el 1 de En. de 2019
You have
while err>tol
but the body of your loop never changes either err or tol so the loop can never be exited.
Ver también
Categorías
Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!