Matlab - can't even do a simple arithmatic

  • Context: MATLAB 
  • Thread starter Thread starter matematikawan
  • Start date Start date
  • Tags Tags
    even Matlab
Click For Summary
SUMMARY

The discussion centers on MATLAB's handling of arithmetic operations involving fractions, specifically the limitations of double-precision floating-point representation. Users observed that operations like 1/3 - 1/2 + 1/6 yield unexpected results due to numerical rounding errors, leading to outputs like -2.7756e-017 instead of zero. The conversation highlights that MATLAB's default behavior is to treat numbers as doubles, which can introduce inaccuracies in calculations. For precise arithmetic with fractions, users are advised to utilize the Symbolic Math Toolbox or consider alternative software like Mathematica or Maple, which handle symbolic computation more effectively.

PREREQUISITES
  • Understanding of MATLAB's data types, specifically double-precision floating-point numbers.
  • Familiarity with numerical precision concepts, including machine epsilon.
  • Knowledge of MATLAB's formatting commands, particularly 'format short' and 'format rat'.
  • Basic understanding of symbolic computation and its differences from numerical computation.
NEXT STEPS
  • Explore MATLAB's Symbolic Math Toolbox for exact arithmetic operations.
  • Learn about machine epsilon and its implications in numerical computing.
  • Investigate the differences between MATLAB and Mathematica in handling fractions and symbolic math.
  • Study the limitations of double-precision floating-point arithmetic in computational mathematics.
USEFUL FOR

Mathematics students, engineers, and data scientists who use MATLAB for numerical computations and require a deeper understanding of its arithmetic limitations and alternatives for precise calculations.

matematikawan
Messages
336
Reaction score
0
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
 
Physics news on Phys.org
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).
 
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.
 
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?
 
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
 
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.
 
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.
 
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
 
Mathematica has a symbolic math engine. MATLAB's rat() attempts to find a simple fractional representation of a floating point.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K