Unexpected result of the "find" command in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
7 replies · 2K views
hokhani
Messages
610
Reaction score
22
The program below gives "error=4 7" unexpectedly while all the arrays of "real" are 1! I would be grateful for any help.

Code:
kky=pi/(2*10000):pi/10000:1/658;
    ky=[-fliplr(kky) kky];
        kx=sqrt((1/658)^2-ky.^2);
expiphi_1=(658/1)*(kx+i*ky);
real=expiphi_1.*conj(expiphi_1)
error=find(real~=1)
 
Physics news on Phys.org
I've never seen this error. You could either find when real==1 then compute the complement or write a simple search loop yourself and see what you get. What is the output of the "real" vector?
 
HuskyNamedNala said:
I've never seen this error. You could either find when real==1 then compute the complement or write a simple search loop yourself and see what you get. What is the output of the "real" vector?

Thank you. What should I do to get rid of this problem?

The output of real and error are:
Code:
real =

    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000    1.0000error =

     4     7
 
HuskyNamedNala said:
I've never seen this error.
I tested it in another computer and got again to those unexpected values ,[4 7], for "error".
 
Try writing your own find loop. Also, check the data type for "real". If I remember correctly real is a command in MATLAB too, though I've since switched to Python.
 
The problem here is due to the nature of floating point arithmetic, where there can be roundoff errors in trying to represent numbers. You are comparing some floating point numbers for exact equality to a number. They are not exactly equal in a bit-for-bit sense, because the numbers came about as the result of arithmetic operations. So this type of comparison is discouraged.

Here is some proof, since if you subtract 1 from the array you don't get all zeros:

Code:
real-1

ans =

   1.0e-15 *

  Columns 1 through 4

                   0                   0                   0  -0.111022302462516

  Columns 5 through 8

                   0                   0  -0.111022302462516                   0

  Columns 9 through 10
                   0                   0

Inspecting the 3rd and 4th elements to 20 digits:

Code:
sprintf('%.20f',real(3))

ans =

1.00000000000000000000

sprintf('%.20f',real(4))

ans =

0.99999999999999989000

So what you need to do to avoid this problem is use a comparison tolerance. Something like the following:

Code:
tol = eps;
error = find(abs(real-1)>tol)

error =

   Empty matrix: 1-by-0
Future reading:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
 
  • Like
Likes   Reactions: hokhani and HuskyNamedNala
Ahh, good call. I remember running into a similar error when I was writing an optimization code and the search vector kept returning 0. It turns out it was ~10^-10
 
HuskyNamedNala said:
... Also, check the data type for "real". If I remember correctly real is a command in MATLAB too, though I've since switched to Python.

This is a good point, because "real" is a MATLAB function that returns the real part of a complex number. In general you shouldn't use names that override MATLAB functions, so just call this array something else, like "real1", "real_matrix" or "result".

If you were to execute the original code and then try to use the MATLAB real function, you might be disappointed that it no longer works:

Code:
a = 1- 5i

a =

  1.000000000000000 - 5.000000000000000i

real(a)
Subscript indices must either be real positive integers or logicals.

If you clear the variable, the MATLAB function works again:
Code:
real(a)

ans =

     1