MATLAB Unexpected inequality in Matlab

  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Inequality Matlab
AI Thread Summary
The discussion centers around a MATLAB program where the expected result of a variable "difr" is not zero, despite the mathematical definition suggesting it should be. The issue arises from the calculation of "qx," which involves a square root that can yield negative values, leading to confusion about the equality of floating-point numbers. It is emphasized that the operator "^" behaves differently from ".^" when dealing with arrays, but both work the same for scalars. The conversation highlights the importance of understanding floating-point arithmetic and rounding errors in numerical computations, suggesting that comparisons of floating-point numbers should be approached with caution. The final query addresses how to handle fractions that may approach infinite values, prompting a discussion on whether to adjust the equations before programming or simply use a small epsilon value in the denominator. The consensus is that learning to manage numerical errors is crucial for accurate programming.
hokhani
Messages
556
Reaction score
17
In the program below, the result of "difr" is not zero but according to the definition of qx in the second line, I expect it to be zero (because qx^2+ky^2=(\frac{ef-u}{hbarv_f})^2). What is the problem?
Code:
ef=1;hbarv_f=658;ky=0.0011;u=2.5;
qx=sqrt(((ef-u)/hbarv_f)^2-ky.^2);
difr=(ef-u)/hbarv_f-sqrt(qx^2+ky^2)
 
Physics news on Phys.org
You are not taking into account that ##x = \sqrt{x^2}## is only true when ##x \ge 0##. (ef-u)/hbarv_f is negative.
 
I don't know MATLAB well, but isn't ^ a different operator than .^
 
DaleSpam said:
I don't know MATLAB well, but isn't ^ a different operator than .^
The two operators act the same way on scalars. It is only for arrays that the behavior is different.
 
DrClaude said:
You are not taking into account that ##x = \sqrt{x^2}## is only true when ##x \ge 0##. (ef-u)/hbarv_f is negative.
But this problem exists even if you put, for instance, ef=10!
 
hokhani said:
But this problem exists even if you put, for instance, ef=10!
No. Did you recalculate qx after having changed ef?
 
DrClaude said:
No. Did you recalculate qx after having changed ef?
Yes, the result is:
Code:
ef=10;hbarv_f=658;ky=0.0011;u=2.5;
qx=sqrt(((ef-u)/hbarv_f)^2-ky.^2);
difr=(ef-u)/hbarv_f-sqrt(qx^2+ky^2)

difr =

  1.7347e-018
 
That's what I call zero. You are doing numerical computations: you have to allow for rounding errors. Look up floating-point arithmetic.
 
  • Like
Likes hokhani
DrClaude said:
That's what I call zero. You are doing numerical computations: you have to allow for rounding errors. Look up floating-point arithmetic.
My program is sensitive even to this tiny value. What should I do to get rid of such errors?
 
  • #10
hokhani said:
My program is sensitive even to this tiny value. What should I do to get rid of such errors?
You can't. You have to learn to deal with those errors. If the rest of your program is affected, it means that it is not properly written. For example, you should never compare the equality of two floating point numbers (or the equality of a float with an integer).
 
  • #11
DrClaude said:
You can't. You have to learn to deal with those errors. If the rest of your program is affected, it means that it is not properly written. For example, you should never compare the equality of two floating point numbers (or the equality of a float with an integer).
If I had in my program the fractions which take infinite values at some points, is it reasonable only putting eps (epsilon) in denominator or I must first factorize the "zero factor" out by trying (hardly in some cases) to change my equations (writing on paper) and then use them in the program?
 
Last edited:
Back
Top