Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - can't even do a simple arithmatic

  1. Nov 30, 2011 #1
    This really surprised me.

    >> format rat
    >> 1/3-1/2+1/6

    ans =

    -1/36028797018963968

    Even school student knows that the answer is 0.

    Even format short does not give a correct answer.
    >> format short
    >> 1/3-1/2+1/6

    ans =

    -2.7756e-017
     
  2. jcsd
  3. Nov 30, 2011 #2

    MATLABdude

    User Avatar
    Science Advisor

    That's the rub isn't it? Computers are great at computation, but are only as good at pattern recognition (like the fact that you have fractions that sum to zero) as the programmer makes it to be. MATLAB (outside of the Symbolic Math Toolbox) doesn't really do fractions.

    By default, MATLAB treats everything as a double-precision floating point number. So when you enter 1/3, it uses 0.333... truncated at something like the 52nd decimal place. When you then turn around and attempt to add or subtract such rounded numbers, you frequently end up with tiny remainders (like the e-17 that you have). You've discovered something quite fundamental to computational mathematics: that you don't usually end up with zeros, but rather something close enough to zero that it's basically zero--something called a machine epsilon.

    Instead of checking for zero when you do an operation like the one you did, you see if the result is below the machine epsilon:
    http://www.mathworks.com/help/techdoc/ref/eps.html

    Additionally, format just changes the way numbers are displayed, not the way they're stored or computed:
    http://www.mathworks.com/help/techdoc/ref/format.html

    The fraction you get when you change it to display fractions is just the rationalized version of that e-17 number.

    EDIT: I should add that Mathematica or Maple or some other CAS (Computer Algebra System) would probably return 0 (assuming you entered it properly to tell these programs that the entry is supposed to be a fraction).
     
  4. Nov 30, 2011 #3
    Thank you for all those information.
    When we were first introduced to matlab, they said matlab can be used as a calculator. Now I think my desktop scientific calculator or Window Accessories calculator can do a much better job for simple arithmetic calculation.

    What puzzled me is that when matlab add two fractions it gives a correct answer.
    >> format rat
    >> 1/3-1/2

    ans =

    -1/6

    But
    >> ans+1/6

    ans =

    -1/36028797018963968.

    Don't tell me that what matlab displayed as -1/6 is not 'really' -1/6.
     
  5. Nov 30, 2011 #4

    Pythagorean

    User Avatar
    Gold Member

    no, realize there's infinite numbers between 0 and 1. The computer can't store infinite numbers, so it can either store some of them, or construct a number set based on the kinds of operations you're doing. It has to avoid numerical rounding errors in the most general form possible. The number returned to you was effectively numerical 0, or... machine epsilon. Since machines can't represent infinity, they can't represent the infinitesimal either. But they can approximate them.

    Basically, you constructed 1/6 two different ways. Do you get the same result if you don't use format rat, though? I mean, did you force it to display 1/6 by its approximation?
     
  6. Nov 30, 2011 #5
    From what I understand the command format won't effect the computation. It is only for display purposes. I show you another session in the default format.

    >> 1/3-1/2

    ans =

    -0.1667

    >> ans + 1/6

    ans =

    -2.7756e-017

    >> -1/6

    ans =

    -0.1667

    >> -1/6+1/6

    ans =

    0
     
  7. Dec 1, 2011 #6
    Every system has its limits. If you want to use it at its limits then you should understand what they are. Expecting 20 significant figures of accuracy from double precision floating point is asking it to go beyond its limits.

    Those errors are inherent in how floating point numbers in computers work. You can make it exact by using symbolic arithmetic (CAS software like Maple), but that's not what Matlab is for, it's for crunching numbers, lots of numbers, fast. Something you can't do when you're representing every number as an algebraic expression with fractions and roots.

    Basically there's a tradeoff between accuracy and speed. A hand calculator doesn't need speed so it can retain fractions as fractions, and roots as roots - to some extent. Complex expressions still get converted to approximate decimal form, and will then suffer from similar problems to what you've seen in Matlab.
     
  8. Dec 2, 2011 #7
    When you type a number into MATLAB the default is double precision (16 significant figures). So technically all your answers are correct to the precision you have implicitly asked for.

    If you don't want the program to perform rounding between operations then perhaps the symbolic math toolbox is what you're looking for.
     
  9. Apr 13, 2012 #8
    But in Mathematica the answer is zero:-
    In[1]:= 1/3 - 1/2 + 1/6

    Out[1]= 0

    OR

    In[2]:= 1/3 - 1/2 + 1/6

    Out[2]= 0
    I guess the difference is because the way mathematica does the computation,maybe it first finds the LCM of the fractions and does the calculation or that sort of thing
     
  10. Apr 13, 2012 #9

    jhae2.718

    User Avatar
    Gold Member

    Mathematica has a symbolic math engine. MATLAB's rat() attempts to find a simple fractional representation of a floating point.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab - can't even do a simple arithmatic
  1. Simple matlab problem (Replies: 1)

Loading...