Main Content

vpa

可变精确算术(任意精确算术)

Support for character vectors that do not define a number has been removed. Instead, first create symbolic numbers and variables using符号一个nd符号,然后对它们使用操作。例如,使用VPA(((1+sqrt(sym(5)))/2)代替VPA('(1 + SQRT(5))/2')

Description

例子

VPA(Xuses variable-precision floating-point arithmetic (VPA) to evaluate each element of the symbolic inputXto at leastdsignificant digits, wheredis the value of the数字功能。的默认值数字是32。

例子

VPA(X,,,,duses at leastdsignificant digits, instead of the value of数字

Examples

Evaluate Symbolic Inputs with Variable-Precision Arithmetic

评估具有可变精确浮点算术的符号输入。默认,vpacalculates values to 32 significant digits.

信谊x p =符号(π);piVpa = vpa (p)
PIVPA = 3.1415926535897932384626433832795
a = sym(1/3);f = a*sin(2*p*x);FVPA = VPA(F)
FVPA = 0.3333333333333333333333333333333333333*SIN(6.28318530717958647692528676666559*X)

评估具有可变精确算术的向量或矩阵的元素。

V = [x/p a^3]; M = [sin(p) cos(p/5); exp(p*x) x/log(p)]; vpa(V) vpa(M)
一个ns = [ 0.31830988618379067153776752674503*x, 0.037037037037037037037037037037037] ans = [ 0, 0.80901699437494742410229341718282] [ exp(3.1415926535897932384626433832795*x), 0.87356852683023186835397746476334*x]

笔记

You must wrap all inner inputs withvpa, 如exp(vpa(200))。否则,输入将自动通过MATLAB转换为double®

Change Precision Used byvpa

默认,vpaevaluates inputs to 32 significant digits. You can change the number of significant digits by using the数字功能。

Approximate the expression100001/10001with seven significant digits using数字。Save the old value of数字returned by数字(7)。Thevpa函数仅返回五个重要数字,这可能意味着剩余数字是零。

数字Old = digits(7); y = sym(100001)/10001; vpa(y)
ANS = 9.9991

Check if the remaining digits are zeros by using a higher precision value of25。结果表明剩余数字实际上是重复的十进制。

数字((25) vpa(y)
ANS = 9.999100089991000899910009

Alternatively, to override数字单一vpa电话,改变e precision by specifying the second argument.

通过指定第二个参数,找到π到100个有意义的数字。

VPA(pi,100)
一个ns = 3.141592653589793238462643383279502884197169... 39937510582097494459230781640628620899862803... 4825342117068

恢复原始的精度值数字Oldfor further calculations.

数字((digitsOld)

数值近似符号结果

虽然符号结果是准确的,但它们可能不采取方便的形式。您可以使用vpa在数值上近似精确的符号结果。

Solve a high-degree polynomial for its roots using解决。The解决function cannot symbolically solve the high-degree polynomial and represents the roots using

符号Xy = solve(x^4 - x + 1, x)
y = root(z^4 -z + 1,z,1)root(z^4 -z + 1,z,2)root(z^4 -z + 1,z,3)root(z^4--z + 1,z,4)

Usevpato numerically approximate the roots.

yVpa = vpa(y)
yVpa = 0.72713608449119683997667565867496 - 0.43001428832971577641651985839602i 0.72713608449119683997667565867496 + 0.43001428832971577641651985839602i - 0.72713608449119683997667565867496 - 0.93409928946052943963903028710582i - 0.72713608449119683997667565867496 + 0.93409928946052943963903028710582i

vpa使用后卫数字来保持精度

The value of the数字函数指定使用的最小数字数量。在内部vpa可以使用更多的数字数字specifies. These additional digits are called guard digits because they guard against round-off errors in subsequent calculations.

Numerically approximate1/3using four significant digits.

一个= vpa(1/3, 4)
一个= 0.3333

近似结果一个using 20 digits. The result shows that the toolbox internally used more than four digits when computing一个。The last digits in the result are incorrect because of the round-off error.

VPA(一个,,,,20)
ANS = 0.333333333333033016843

Avoid Hidden Round-off Errors

隐藏的圆形错误可能会导致意外结果。

Evaluate1/10使用默认的32位数精度,然后使用10位数字精度。

A = VPA(1/10,32)B = VPA(1/10,10)
a = 0.1 b = 0.1

Superficially,一个一个ndb看起来相等。查找他们的平等一个- b

一个- b
一个ns = 0.000000000000000000086736173798840354720600815844403

差异不等于零,因为bwas calculated with only10精度的数字和包含比一个。When you find一个- b,,,,vpa一个pproximatesb有32位数字。演示这种行为。

一个- vpa(b, 32)
一个ns = 0.000000000000000000086736173798840354720600815844403

vpa恢复普通双重精确输入的精度

Unlike exact symbolic values, double-precision values inherently contain round-off errors. When you callvpaon a double-precision input,vpa即使返回的数字比双精度值更多,也无法恢复丢失的精度。然而,vpa可以识别并恢复表格的表达精度p/,,,,pπ/,,,,((p/1/2,,,,2,,,,一个nd10,,,,wherep一个nd一个re modest-sized integers.

First, demonstrate thatvpacannot restore precision for a double-precision input. Callvpaon a double-precision result and the same symbolic result.

dp= log(3); s = log(sym(3)); dpVpa = vpa(dp) sVpa = vpa(s) d = sVpa - dpVpa
dpvpa = 1.09861228868681095600636126619065 svpa = 1.09861222886686868196913952452452452369225

正如预期的那样,双精度结果与16的确切结果不同Thdecimal place.

Demonstrate thatvpa恢复表格表达式的精度p/,,,,pπ/,,,,((p/1/2,,,,2,,,,一个nd10,,,,wherep一个nd是适度大小的整数,通过找到vpacall on the double-precision result and on the exact symbolic result. The differences are0.0显示vpa恢复双重精确输入中丢失的精度。

VPA(1/3) - vpa(1/sym(3)) vpa(pi) - vpa(sym(pi)) vpa(1/sqrt(2)) - vpa(1/sqrt(sym(2))) vpa(2^66) - vpa(2^sym(66)) vpa(10^25) - vpa(10^sym(25))
一个ns = 0.0 ans = 0.0 ans = 0.0 ans = 0.0 ans = 0.0

Input Arguments

collapse all

输入要评估,指定为数字,向量,矩阵,多维阵列或符号数,矢量,矩阵,多维阵列,表达式,函数或字符向量。

大数的数量,,,,specified as an integer.d必须大于1,并且比 2 29 + 1

Tips

  • vpa不会将指数中的分数转换为浮点。例如,VPA(a^Sym(2/5))返回A^(2/5)

  • vpa使用比指定的数字数量更多的数字数字。这些额外的数字防守防止后续计算中的圆形错误,称为后卫数字。

  • 你打电话时vpa在数字输入上,例如1/3,,,,2^(-5), 或者罪(pi/4),,,,The numeric expression is evaluated to a double-precision number that contains round-off errors. Then,vpais called on that double-precision number. For accurate results, convert numeric expressions to symbolic expressions with符号。例如,to approximateexp(1),,,,useVPA(exp(sym(1)))

  • If the second argumentd不是整数,vpa将其四舍五入到最近的整数round

  • vparestores precision for numeric inputs that match the formsp/,,,,pπ/,,,,((p/1/2,,,,2,,,,一个nd10,,,,wherep一个nd一个re modest-sized integers.

  • 原子操作使用可变精确的算术弹到最近。

  • 可变精确算术和IEEE浮点标准754之间的差异为

    • Inside computations, division by zero throws an error.

    • The exponent range is larger than in any predefined IEEE mode.vpaunderflows below approximately10^(-323228496)

    • 未实施非规范数字。

    • Zeroes are not signed.

    • 的数量binary数字in the mantissa of a result may differ between variable-precision arithmetic and IEEE predefined types.

    • There is only one表示。安静和信号之间没有区别

    • No floating-point number exceptions are available.

版本历史记录

Introduced before R2006a