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

Discussion Overview

The discussion revolves around the behavior of MATLAB when performing arithmetic operations with fractions, particularly focusing on the discrepancies in results due to floating-point representation and the limitations of numerical precision. Participants explore the implications of using MATLAB for simple arithmetic compared to other tools, such as calculators and symbolic math systems.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant notes that MATLAB's output for the expression 1/3 - 1/2 + 1/6 is not zero, which they find surprising, and suggests that even basic calculators perform better in this regard.
  • Another participant explains that MATLAB treats numbers as double-precision floating point by default, leading to small rounding errors in calculations.
  • Some participants discuss the concept of machine epsilon, indicating that results close to zero may not be exactly zero due to numerical precision limitations.
  • One participant points out that while MATLAB can correctly add two fractions, the result of subsequent operations can yield unexpected values due to how numbers are stored and displayed.
  • Another participant emphasizes that the format command in MATLAB only affects display and not the underlying computation.
  • There is mention of the tradeoff between accuracy and speed in numerical computations, with some suggesting that symbolic math systems like Mathematica handle fractions differently, potentially yielding exact results.
  • One participant highlights that Mathematica's symbolic math engine may compute results differently than MATLAB's numerical approach.

Areas of Agreement / Disagreement

Participants express differing views on MATLAB's handling of arithmetic operations, with some agreeing on the limitations of floating-point arithmetic while others highlight the differences in behavior compared to other systems like Mathematica. The discussion remains unresolved regarding the best approach for handling such calculations in MATLAB.

Contextual Notes

Participants acknowledge that the limitations of floating-point representation can lead to unexpected results, and there is a lack of consensus on the best practices for performing arithmetic in MATLAB versus other computational tools.

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
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K