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

5 次查看(过去 30 天)
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.

采纳的回答

Bruno Luong
Bruno Luong 2020-8-27
编辑:Bruno Luong 2020-8-27
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 个评论
James Tursa
James Tursa 2020-8-27
编辑:James Tursa 2020-8-27
"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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by