Why is det(A) sometimes =/= det(A') for random square matrices

3 visualizaciones (últimos 30 días)
Hello,
I have been sitting on this for a few hours now.
A few months ago, during a practice session, we were asked to show proof that
det(A) == det(A')
with A being a random 3x3 matrix, or in general nxn matrix. The task asks for 3x3, but it should be the same as long as it is a square matrix.
Back then I didn't loop this for several repetitions, and just got the right answer when checking first time. Later on I decided to just repeat it several times, expecting to get the same answer every time. For reasons I don't understand, this is not the case, and I don't understand why that is the case.
I used the following code to test this "quickly":
% Preallocating and Setting Reps
NReps=1000;
Sol=zeros(1,NReps);
Result_Zero=zeros(1,NReps);
Result_One=zeros(1,NReps);
% Calcing
for k=1:1:NReps
for j=1:1:NReps
NR6.A=randi([-100,100],[3,3]);
NR6.detA=det(NR6.A);
NR6.detAtransp=det(NR6.A');
Sol(j)=isequal(NR6.detA,NR6.detAtransp);
end
Result_Zero(k)=nnz(Sol(:)==0);
Result_One(k)=nnz(Sol(:)==1); % this is what should happen, but it does ony do so in ~50%
end % of all cases. I have no clue why, or why it is not a 100% either way
x=1:1:NReps; % at least, instead of this.
clc; close all
% Evaluation
hold on
plot(x,Result_Zero,'-r')
plot(x,Result_One,'-b')
legend('Result: 1 (red)','Result: 0 (blue)','Location','east') % for some reason, my matlab instance doesn't actually display the
% color next to the text in the legend unless I export the figure into
% f.e. word. No clue why that happens.
As I understand it, I should not get any "red" cases, i.e. where
Result_One(k)=nnz(Sol(:)==1);
is all I get.
Now, pretty obviously this is not the case, and I don't know if this is just me not knowing something about matrix calculations, or doing something wrong in-code. Doing a bit of digging, I couldn't find any reason why this behaviour happens.
I can't find any problems with this code myself, and in my opinion this should work perfectly.
What am I missing?
Thank you & stay healthy,
C.A.

Respuesta aceptada

Bruno Luong
Bruno Luong el 27 de Ag. de 2020
Editada: Bruno Luong el 27 de Ag. de 2020
Nobody told you ever compare floating point using "==", "isequal", "ismember()"?
% Preallocating and Setting Reps
NReps=1000;
Sol=zeros(1,NReps);
Result_Zero=zeros(1,NReps);
Result_One=zeros(1,NReps);
% Calcing
for k=1:1:NReps
for j=1:1:NReps
NR6.A=randi([-100,100],[3,3]);
NR6.detA=det(NR6.A);
NR6.detAtransp=det(NR6.A');
% compare up to 10 significant digits
Sol(j)=abs(NR6.detA-NR6.detAtransp)<abs(NR6.detA)*1e-10; % HERE IS HOW FLOATINT POINTS SHOULD BE COMPARED
end
Result_Zero(k)=nnz(Sol(:)==0);
Result_One(k)=nnz(Sol(:)==1); % this is what should happen, but it does ony do so in ~50%
end % of all cases. I have no clue why, or why it is not a 100% either way
x=1:1:NReps; % at least, instead of this.
clc; close all
% Evaluation
hold on
plot(x,Result_Zero,'-r')
plot(x,Result_One,'-b')
legend('Result: 1 (red)','Result: 0 (blue)','Location','east') % for some reason, my matlab instance doesn't actually display the
% color next to the text in the legend unless I export the figure into
% f.e. word. No clue why that happens.
  3 comentarios
Bruno Luong
Bruno Luong el 27 de Ag. de 2020
Try to undesrtand the result of this:
>> isequal(1/3 + 1/6 - 1/2, 1/3 - 1/2 + 1/6)
ans =
logical
0
Some reading for you
James Tursa
James Tursa el 27 de Ag. de 2020
Editada: James Tursa el 27 de Ag. de 2020
"But why shouldn't/can't I compare with isequal?"
Bottom line is that the calculations to figure out det(A) are done in a different order than they are with det(A'). That is, if you were to list out all of the calculations done in the background to get det(A) and compare them to a list of all of the calculations done in the background to get det(A'), you could verify that yes they were mathematically equal. But because they were done in a different order you can't expect the floating point results to be exactly the same.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by