How to avoid effect of number of extra zeros after decimal point on the equality between two arrays?

조회 수: 1 (최근 30일)
I have calculated two one diensonal matrices (i.e. two arrays) using two different equations. I am trying to check if corresponding values of both the arrays are equal or not. Following are the two methods that I followed to generate two arrays, namely y1 and y2,
Method: 1
R = 2.5;
x = (0:0.005:1);
y1 = R*x.*(1-x);
Method: 2
x = (0:0.005:1);
y2 = (-1*((x-0.5).^2) + 0.25)/0.4;
I am using following command to check equality between the values of two arrays,
y1 == y2
It is expected that both the arrays should be equal but, the output of above command is a logical array consisting of both zero's and one's. I tried manually cheking the values and they were equal. For example, (I have kept format of command window as long)
>> format long
>> y1(3)
ans =
0.024750000000000
>> y(3)
ans =
0.024750000000000
Later on, I tried this,
>> y1(3)==y(3)
ans =
logical
0
If I explicitely set these values to other variables,
>> format long
>> a=0.024750000000000;
>> b=0.024750000000000;
>> a==b
ans =
logical
1
In the question I am talking about number of extra zeros after decimal point because, if I explicitely remove those zeros from the values of both the arrays (by opening the arryas in variables window), I get,
>> format long
>> y1(3)
ans =
0.024750000000000
>> y(3)
ans =
0.024750000000000
>> y1(3)==y(3)
ans =
logical
1
What is happening here? What am I missing?
How do I check the equality between the two arrays generated in such a manner?
  댓글 수: 2
Stephen23
Stephen23 2020년 8월 15일
편집: Stephen23 2020년 8월 15일
"What is happening here?"
Your calculations accumulate floating point error, which is expected and documented.
"What am I missing?"
Your forgot to take into account the accumulated floating point error.
"How do I check the equality between the two arrays generated in such a manner?"
Don't. All operations on floating point numbers should be assumed to accumulate error, this is inherent in their nature. It is up to the programmer to understand this and take this into account: the usual approach is to compare the absolute difference against a tolerance, or something like Bruno Luong showed.

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

채택된 답변

Bruno Luong
Bruno Luong 2020년 8월 15일
"How do I check the equality between the two arrays generated in such a manner? "
tol = 2*eps(norm(y1,Inf));
norm(y1-y2,Inf)<=tol

추가 답변 (1개)

Sara Boznik
Sara Boznik 2020년 8월 15일
Hi,
I also tried like you and also got wrong result. Later I checked for y1(201) where the ans=0 and I got correct result.
It is possible that is Matlab's bug.
Than I try also:
tf = isequal(y1,y2)
And also get logical 0.
But then I tried that:
y1 = struct('field1',0.005,'field2',2500);
y2 = struct('field2',2500,'field1',0.005);
tf = isequal(y1,y2)
And works, I got logical 1.
Best of luck.
  댓글 수: 2
Stephen23
Stephen23 2020년 8월 15일
편집: Stephen23 2020년 8월 15일
"It is possible that is Matlab's bug."
Not at all.
The behaviors of binary floating point numbers are well documented, are not specific to MATLAB, and have been discussed thousands of times on this forum (and on other forums, blogs, documentation, etc. on the internet. Also academic research over the last 60+ years, so really there are no surprises here). Start by reading these:
This is worth reading as well:
Arithmetic operations on binary floating point numbers accumulate (in the general case) floating point error. This is not a bug, just a reason for beginners to learn something new.

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

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by