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
Examples
Evaluate Symbolic Inputs with Variable-Precision Arithmetic
评估具有可变精确浮点算术的符号输入。默认,vpa
calculates 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
默认,vpa
evaluates inputs to 32 significant digits. You can change the number of significant digits by using the数字
功能。
Approximate the expression100001/10001
with 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
恢复原始的精度值数字Old
for 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)
Usevpa
to 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/3
using 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
差异不等于零,因为b
was 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 callvpa
on a double-precision input,vpa
即使返回的数字比双精度值更多,也无法恢复丢失的精度。然而,vpa
可以识别并恢复表格的表达精度p/问,,,,pπ/问,,,,((p/问)1/2,,,,2问,,,,一个nd10问,,,,wherep一个nd问一个re modest-sized integers.
First, demonstrate thatvpa
cannot restore precision for a double-precision input. Callvpa
on 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问是适度大小的整数,通过找到vpa
call 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
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,vpa
is 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 argument
d
不是整数,vpa
将其四舍五入到最近的整数round
。vpa
restores 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.
vpa
underflows 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.