How to increase the number of digits in MATLAB?

I realize this is a somewhat vague question, so I'll do my best. If I have a number like this n = 0.783325849821429

, is there a way to show even more decimal places? This was done with a long format. The problem is that I am doing calculations with an error estimate with sine, and there comes a point where the estimate is so close to the actual sine value that MATLAB calculates the error as 0, although it is impossible for the estimate to be completely accurate. There is always an error, it is too small for MATLAB to recognize. Is there a way to make MATLAB more decimal places?

+3


source to share


5 answers


You can always ask for more digits using formatted strings

 fprintf(1, 'n = %.20f\n', n ); % print with 20 digits after the decimal point

      

However, there is a limit to the precision of stored floating point numbers.
You can check the accuracy of the machine usingeps



eps( n )

      

If your estimate error is less eps

than your estimate is within the machine's precision and cannot be measured.

+3


source


use digits with vpa (Variable Precision). Read more about variable precision arithmetic here ... Note that you will need a symbolic math toolbar. If you don't have it, you can use John D'Errico Variable Precision Integer Arithmetic from File Exchange.



+1


source


if you use vpa function to improve precision then it will have your variable as syms and after that if you want to use this variable for other internal Matlab functions then it won't work so find another way.

eg.

a=vpa(a,5) 
a =

[ 1.25, 2.0276, 3.2108, 3.3695, 2.0589, 1.0]

polyval(a,3)

??? Undefined function or method 'isfinite' for input arguments of type
'sym'.

Error in ==> polyval at 54
if isscalar(x) && (nargin < 3) && nc>0 && isfinite(x) &&
all(isfinite(p(:)))

      

0


source


For example, to get a 1000-digit answer for equation 22/7

numbers (1000)

VPA (22/7)

0


source


Create an arbitrary class like dlnode and add a member variable. MATLAB does not impose any limit on the number of digits a member function has, so you can store values ​​with arbitrary precision.

0


source







All Articles