简体   繁体   English

如何增加MATLAB中的位数?

[英]How do I 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 n = 0.783325849821429 , is there a way to display even more decimal places? 如果我有一个像n = 0.783325849821429 ,有没有办法显示更多的小数位? This was produced with format long. 这是用格式很长的。 The problem is that I'm doing error estimate calculations with sine and there comes a point where the estimation is so close to the actual value of sine that MATLAB calculates the error as 0, even though it's not possible that the estimation is completely accurate. 问题是我正在用正弦进行误差估计计算,并且有一个点估计非常接近正弦的实际值,MATLAB将误差计算为0,即使估算不可能完全准确。 There's always error, it's just too small for MATLAB to recognize. 总是有错误,它对于MATLAB来说太小了。 Is there a way to make MATLAB consider more decimal places? 有没有办法让MATLAB考虑更多小数位?

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 numbers in floating point. 但是 ,浮点数存储数字的精度存在限制。
You can test the machine precision using eps 您可以使用eps测试机器精度

eps( n )

If your estimate error is smaller than eps than your estimate is within machine precision and cannot be measured. 如果您的估计误差小于eps则估计误差在机器精度范围内且无法测量。

use digits with vpa (Variable-precision accuracy). 使用带有vpa的 数字 (可变精度准确度)。 More about Variable-Precision Arithmetic here ... Note that you'll need the symbolic math toolbox. 更多关于可变精度算术这里 ......请注意,您所需要的符号数学工具箱。 If you don't have it, then you can use John D'Errico's Variable Precision Integer Arithmetic from the file exchange.. 如果你没有它,那么你可以从文件交换中使用John D'Errico的可变精度整数算术

if you use vpa function to increase precision then it will have your variable as syms and after that if u want to use this variable for other matlab internal functions then it will not work hence find some other way. 如果你使用vpa函数来提高精度,那么它将你的变量作为syms,之后如果你想将这个变量用于其他matlab内部函数,那么它将无法工作因此找到其他方法。

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(:)))

For example, to get the answer with 1000 digits for the equation of 22/7 例如,为了得到22/7等式的1000位数的答案

digits(1000) 数字(1000)

vpa(22/7) VPA(22/7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM