Matlab - can't even do a simple arithmatic

  • MATLAB
  • Thread starter matematikawan
  • Start date
  • Tags
    even Matlab
In summary: So in summary, what you've seen is that when you enter a fraction in Matlab, by default it truncates the fractional part to the nearest integer, and then performs the operation. If the result is below the machine epsilon (0.1667 in this case), then the result is displayed as a fraction. However, if you change the format to "rat" (a symbolic fraction), then the computer will return an approximation of the fractional number, which is essentially numerical 0.
  • #1
matematikawan
338
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
  • #2
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).
 
  • #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.
 
  • #4
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?
 
  • #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
 
  • #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.
 
  • #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.
 
  • #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
 
  • #9
Mathematica has a symbolic math engine. MATLAB's rat() attempts to find a simple fractional representation of a floating point.
 

1. Why am I getting an error in my simple arithmetic calculation in Matlab?

There could be several reasons for this error. It could be due to a typo in the code, incorrect syntax, or using the wrong function. Double-check your code and make sure you are using the correct syntax and function for your desired calculation.

2. How can I troubleshoot my Matlab code if I am unable to perform a simple arithmetic calculation?

First, check for any errors in your code and make sure you are using the correct syntax. You can also use the debugger tool to step through your code and identify any errors. Additionally, consult Matlab's documentation or online forums for tips on troubleshooting common issues.

3. Why is my Matlab code giving me incorrect results for a simple arithmetic calculation?

There could be a few reasons for this. Make sure you are using the correct data types for your variables, as mixing data types can lead to unexpected results. Also, check for any logical errors in your code that may be causing incorrect calculations.

4. How can I improve the performance of simple arithmetic calculations in Matlab?

One way to improve performance is to vectorize your code, which involves performing calculations on arrays instead of individual elements. This can significantly speed up your code. You can also try using built-in Matlab functions instead of writing your own, as they are usually optimized for performance.

5. Are there any resources available for learning how to perform simple arithmetic calculations in Matlab?

Yes, there are many online tutorials and resources available for learning how to use Matlab's arithmetic functions. You can also refer to Matlab's documentation for specific function usage and examples. Additionally, there are numerous forums and communities where you can ask for help and advice from other Matlab users.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
940
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top