Register to reply

Matlab - can't even do a simple arithmatic

by matematikawan
Tags: arithmatic, matlab, simple
Share this thread:
matematikawan
#1
Nov30-11, 04:22 AM
P: 333
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
Phys.Org News Partner Science news on Phys.org
'Office life' of bacteria may be their weak spot
Lunar explorers will walk at higher speeds than thought
Philips introduces BlueTouch, PulseRelief control for pain relief
MATLABdude
#2
Nov30-11, 05:21 AM
Sci Advisor
P: 1,724
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).
matematikawan
#3
Nov30-11, 09:47 PM
P: 333
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.

Pythagorean
#4
Nov30-11, 09:57 PM
PF Gold
Pythagorean's Avatar
P: 4,292
Matlab - can't even do a simple arithmatic

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?
matematikawan
#5
Nov30-11, 11:22 PM
P: 333
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
Unrest
#6
Dec1-11, 09:32 PM
P: 360
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.
mikeph
#7
Dec2-11, 09:09 AM
P: 1,212
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.
kritboy
#8
Apr13-12, 08:44 AM
P: 1
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
jhae2.718
#9
Apr13-12, 11:22 PM
PF Gold
jhae2.718's Avatar
P: 1,160
Mathematica has a symbolic math engine. MATLAB's rat() attempts to find a simple fractional representation of a floating point.


Register to reply

Related Discussions
Modular Arithmatic Linear & Abstract Algebra 6
Mod Arithmatic General Math 12
General Arithmatic... Calculus & Beyond Homework 4
Arithmatic series help Precalculus Mathematics Homework 10