Unexpected inequality in Matlab

In summary: If you have fractions which take infinite values at some points, you should first factorize the "zero factor" out by trying (hardly in some cases) to change your equations (writing on paper) and then use them in the program.
  • #1
hokhani
483
8
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 [itex]qx^2+ky^2=(\frac{ef-u}{hbarv_f})^2[/itex]). 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
  • #2
You are not taking into account that ##x = \sqrt{x^2}## is only true when ##x \ge 0##. (ef-u)/hbarv_f is negative.
 
  • #3
I don't know MATLAB well, but isn't ^ a different operator than .^
 
  • #4
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.
 
  • #5
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!
 
  • #6
hokhani said:
But this problem exists even if you put, for instance, ef=10!
No. Did you recalculate qx after having changed ef?
 
  • #7
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
 
  • #8
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
  • #9
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:

1. What is "Unexpected inequality" in Matlab?

"Unexpected inequality" in Matlab refers to situations where the output of a calculation or comparison does not match the expected result. This can happen due to a variety of reasons, such as incorrect use of operators or functions, data type mismatches, or rounding errors.

2. How can I troubleshoot unexpected inequality in my Matlab code?

To troubleshoot unexpected inequality in your Matlab code, first check for any obvious errors in your code, such as typos or missing parentheses. Next, use the debugger tool to step through your code and track the values of variables and parameters. This can help identify where the unexpected inequality is occurring. Additionally, you can try using the "isequal" function to compare the values of variables instead of using direct equality comparisons.

3. Can unexpected inequality cause errors in my Matlab program?

Yes, unexpected inequality can cause errors in your Matlab program. If your code relies on certain values being equal or not equal, unexpected inequality can lead to incorrect results or unexpected behavior. It is important to troubleshoot and fix any instances of unexpected inequality in your code to avoid errors.

4. How can I prevent unexpected inequality in my Matlab code?

To prevent unexpected inequality in your Matlab code, it is important to use appropriate operators and functions for your data types, avoid mixing data types in calculations, and be aware of potential rounding errors. Additionally, regularly testing and debugging your code can help catch any unexpected inequality before it becomes a problem.

5. Are there any built-in functions in Matlab that can help with unexpected inequality?

Yes, there are a few built-in functions in Matlab that can help with unexpected inequality. The "isequal" function can be used to compare the values of variables, and the "format" function can control the display format of numbers, potentially reducing rounding errors. Additionally, the "debugger" tool can be used to step through your code and track variable values to identify instances of unexpected inequality.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
875
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Classical Physics
Replies
13
Views
987
Back
Top