Can MATLAB use higher precision for computations?

In summary, the precision in MATLAB is limited to approximately 2*10^(-16). While it is possible to calculate with higher precision using the vpa() function, the result will be a symbol, not a numerical number. Alternative solutions include creating a user-defined data type or using the symbolic math toolbox, but these options may be difficult to implement in MATLAB.
  • #1
sara_87
763
0

Homework Statement



The precision in MATLAB is approximately 2*10^(-16),
is there a way that my computations can be calculated using a smaller precision?

Homework Equations





The Attempt at a Solution



if i want to calculate the sum of pi^2*j where j=1:20
but i want it to be computed using high precision.if i type:

b=0;
for j=1:20
b=b+pi^2*j;
end

this would use the default double precision.

I know that
vpa(b)

will give me (as a symbol) the answer to 32 decimal places. But, I don't want to display it in 32 deciaml places, i wan the inner computations to be calculated with a better precision (eg 32 decimal places).

Any ideas will be very much appreciated.
 
Physics news on Phys.org
  • #2
sara_87 said:

Homework Statement



The precision in MATLAB is approximately 2*10^(-16),
is there a way that my computations can be calculated using a smaller precision?

Homework Equations





The Attempt at a Solution



if i want to calculate the sum of pi^2*j where j=1:20
but i want it to be computed using high precision.if i type:

b=0;
for j=1:20
b=b+pi^2*j;
end

this would use the default double precision.

I know that
vpa(b)

will give me (as a symbol) the answer to 32 decimal places. But, I don't want to display it in 32 deciaml places, i wan the inner computations to be calculated with a better precision (eg 32 decimal places).

Any ideas will be very much appreciated.

Not directly, no. The limit isn't an artificial constraint but comes from constraints of the machine itself, which can only faithfully represent so many digits. If you store the first half of your variable in a double precision variable and the second half in another double precision variable, you can do it that way, but that's kind of awkward.

Also, note that regardless of digits of precision, the machine memory cannot directly represent pi in binary, so that's an external constraint on the precision of your result.
 
  • #3
oh right i see.
With what precision is pi calculated? double precision?
also, with other functions such a sine, cosine, log, are they also calculated with double precision?

thank you
 
  • #4
Something like this is better suited in a lower-level language. You can create your own floating point class with arbitrary precision (using an ever-growing amount of memory and time per computation) and then code your own multiplication/division algorithms that use your new data type.

The implementation of double-precision is well-documented. Just look at that documentation and extend it to use perhaps 128 bits or 512 bits.

http://stackoverflow.com/questions/2633400/c-c-efficient-bit-array

There is a starting point on how you can represent your bit array.

You would also probably need to load somehow pi into your new data class using this greater precision or else the attempt would be for naught. You could look into methods for computing pi and do so using your new data type.

I have to also note that using 2 doubles makes no sense at all.

As for the precision of pi and computation in MATLAB, the default data type is double. pi() is double precision. cos() and all other functions are too if you feed it a double.
 
  • #5
RoshanBBQ said:
I have to also note that using 2 doubles makes no sense at all.

The way that a double precision number is stored in memory, for 32-bit arithmetic, is to store the number in two neighboring memory locations (or in 64 bit arithmetic, you can represent all 16 decimal places as one number). So, in effect, merging two singles to make a double logically predicts merging two doubles to make a quadruple precision number (32 digits of precision, perhaps), but when you can only have 64 bits per number you have no choice but to split it into two numbers. That is naturally how any quadruple precision (or double-double) memory type would be implemented on a system that does not have 128-bit arithmetic.

The problem here is that MATLAB does not have built-in support for that level of precision at the current time, and creating user-defined data types is difficult in MATLAB (unlike in C++).
 
  • #6
Steely Dan said:
The way that a double precision number is stored in memory, for 32-bit arithmetic, is to store the number in two neighboring memory locations (or in 64 bit arithmetic, you can represent all 16 decimal places as one number). So, in effect, merging two singles to make a double logically predicts merging two doubles to make a quadruple precision number (32 digits of precision, perhaps), but when you can only have 64 bits per number you have no choice but to split it into two numbers. That is naturally how any quadruple precision (or double-double) memory type would be implemented on a system that does not have 128-bit arithmetic.

The problem here is that MATLAB does not have built-in support for that level of precision at the current time, and creating user-defined data types is difficult in MATLAB (unlike in C++).
It doesn't work that way. You have to manage the bits themselves. You can't just use two doubles and say "I got more precision". With respect to MATLAB, it makes zero sense whatsoever.
 
  • #7
RoshanBBQ said:
It doesn't work that way. You have to manage the bits themselves. You can't just use two doubles and say "I got more precision". With respect to MATLAB, it makes zero sense whatsoever.

Yes, I did state that it is quite difficult to do in MATLAB. But it is possible to implement such an extended precision format in MATLAB. Indeed, some toolboxes have been written to this effect, although I cannot vouch for their usefulness, having never needed to use them myself. But if the original poster is not interested in learning a new language, those toolboxes might be worth checking out (or perhaps writing your own).

By the way, the symbolic math toolbox supports arbitrary precision arithmetic, but obviously it can be quite restrictive to do it this way.
 
  • #8
thank you very much for these ideas.

well, I don't want to use another language, I would like to stick with MATLAB.
the symbolic toolbox can use higher precision using vpa for example:
vpa(pi,40) will use 40 significant figures, however, the problem is: the outcome will be a symbol and i want to use this outcome as a numerical number not a symbol.

is there a way i can use the outcome as a number?
 
  • #9
Well, what you can do is basically convert your whole program into a symbolic math calculation (which is why I said it's restrictive). This is because if you just use it for things like constants such as pi, and then you multiply it by some double precision variable stored in memory, you lose all the precision you wanted to keep, since whatever is multiplying pi is not itself accurate to 40 digits. So to maintain that precision, you need to have every term in your calculation be accurate to 40 digits. Then at the end you can just output your results to whatever precision you want.
 
  • #10
Hi Sara, I came across this article on computing pi recently. It might be of use in general for implementing variable precision commands in matlab.

http://www.mathworks.co.uk/company/newsletters/articles/clevescorner-computing-pi.html
 
Last edited by a moderator:

FAQ: Can MATLAB use higher precision for computations?

What is the significance of precision in MATLAB variables?

The precision of a variable in MATLAB refers to the number of digits used to represent the value of the variable. This is important because it determines the accuracy of calculations and the amount of memory needed to store the variable.

How is precision specified in MATLAB?

Precision can be specified in MATLAB using the format command, followed by the number of decimal places desired. For example, format long will display 15 decimal places, while format short will display 4 decimal places.

What are the default precision settings in MATLAB?

The default precision settings in MATLAB are format short for numeric values and format short g for non-numeric values. This means that numeric values will be displayed with 4 decimal places, while non-numeric values will be displayed in the most compact format.

How can precision be adjusted for specific calculations in MATLAB?

Precision can be adjusted for specific calculations in MATLAB by using the vpa (Variable Precision Arithmetic) function. This allows for calculations to be performed with a higher precision than the default settings, which can be useful for sensitive calculations.

Can the precision of variables in MATLAB be changed permanently?

Yes, the precision of variables in MATLAB can be changed permanently by using the default function. This allows for a new default precision to be set for all future calculations.

Similar threads

Back
Top