Not sure best way to code orthogonal diagonalization

6 visualizaciones (últimos 30 días)
Thomas Stowell
Thomas Stowell el 7 de Abr. de 2020
Comentada: David Goodmanson el 9 de Abr. de 2020
Just having an issue with highlighted part, will attach rest of code below.
function []=symmetric(A)
if size(A,1)~=size(A,2)
disp('Matrix is not symmetric')
return
else
if all(abs(A-A')<.000001)
[P,D]=eig(A);
if all(abs(inv(P)-P')<.00001)
disp('P:')
disp(P)
disp('D:')
disp(D)
disp('P is orthogonal matrix')
else
disp('P:')
disp(P)
disp('D:')
disp(D)
disp('P is orthogonal matrix')
end
else
disp('Matrix is not symmetric')
end
end
end
  5 comentarios
Thomas Stowell
Thomas Stowell el 8 de Abr. de 2020
Is there any suggested code or is this thread primarily to point out how bad other code is?
David Goodmanson
David Goodmanson el 9 de Abr. de 2020
Hi Thomas,
That's a good point. Because of your other question I had a feeling that the stuff here was not really helping.
Since the entries of A in this case are integers you can do a check for exact equality, A - A' == 0. The other tests are going to take tolerance checks, such as all(A*P-P*D < tolerance). As far as the acceptable tolerance, it looks like the instructor has provided that with closetozeroroundoff (although I have no idea how it works). For orthogonality, you can have all(inv(P) -P' < tolerance) as you are doing. Probably better, especially for large matrices, is not doing the inverse. For an orthogonal matrix P*P' = eye(size(P)) so you can check all(P*P'-eye(size(P))< tolerance).

Iniciar sesión para comentar.

Respuestas (1)

David Hill
David Hill el 9 de Abr. de 2020
For homework, we like to help you figure it out yourself. The code below might help and you could likely find a better way.
function a=symmetric(A)
if issymmetric(A)
[P,D]=eig(A);
if closetozeroroundoff(P*D,A*P)&&closetozeroroundoff(inv(P),P')
a='orthogonal diagonal';
else
a='symmetric';
end
else
a='nonsymmetric';
end
end
function yn=closetozeroroundoff(a,b)
yn=0;
if norm(a-b)<1e-14
yn=1;
end
end

Categorías

Más información sobre Linear Algebra 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!

Translated by