필터 지우기
필터 지우기

Why does the code output something that's clearly false?

조회 수: 2 (최근 30일)
B
B 2017년 11월 24일
댓글: Stephen23 2017년 11월 25일
It's supposed to check if A is orthogonal. Here's the code:
A= [2/3 2/3 1/3; -2/3 1/3 2/3; 1/3 -2/3 2/3];
Id= [1 0 0; 0 1 0; 0 0 1];
AT= transpose(A);
Ai= inv(A);
B=AT*A;
if Ai==AT
disp("A is orthogonal");
elseif B==Id
disp("A is orthogonal B");
else
disp('A is not orthogonal');
end
I checked myself and both of the first two tests should output True. But they don't. I wasn't even supposed to use both of the first two at once since they both are true but the even when I use only one, it still says that A is not orthogonal when it is?
Why is this happening?
  댓글 수: 1
Stephen23
Stephen23 2017년 11월 25일
"Why does the code output something that's clearly false?"
It is not "clearly false" to me. My assumptions are that numeric mathematics contains numeric error which accumulates through numeric calculations, and that this error must be taken into account by anyone writing code that uses numeric calculations. What are your assumptions? Did you assume that numeric calculations are the same as symbolic mathematics? Or that your computer has infinite memory and stores all numeric data with infinite precision?
In any case, you will find plenty of information on why this happens: because you compare floating-point values, which inherently have floating point error. Small differences in the floating-point values means that you should not expect an output equivalent to some mathematical operation/s. Floating-point numbers have been explained a thousand times on this forum:
etc, etc, etc
If you want a more detailed explanation, then read this:

댓글을 달려면 로그인하십시오.

채택된 답변

Christoph F.
Christoph F. 2017년 11월 24일
> Why is this happening?
Look at the value of
Ai-AT
MatLAB uses numeric methods to invert matrices with inv(), and numeric methods are subject to numeric errors. This starts with finite machine precision (MatLAB has no way to represent a value like 2/3 accurately with a double precision floating point number).

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 11월 24일

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by