vpa
Variable-precision evaluation of numbers and symbolic expressions.
Category: Symbolic Computations in MATLAB > Conversion Between Symbolic and Numeric
Syntax
r = vpa(x)
Evaluates x with digits significant decimal digits (32 by default).
r = vpa(x, d)
Evaluates with d digits for this call only.
Description
vpa turns exact quantities like sqrt(sym(2)) or sym(1)/3 into decimal numbers with far more digits than a double offers, while symbols stay symbolic: vpa(sin(x) + sym(pi), 5) gives sin(x) + 3.1416. The global precision is set by digits.
The result is of class sym; convert it to a double with double when ordinary arithmetic is needed.
Input arguments
x- Number, symbolic expression or array.
d- Number of significant digits.
Output arguments
r- Symbolic result in which numeric parts are decimals with the requested precision.
Example
Different precisions
vpa(sqrt(sym(2)), 20)
vpa(sym(1)/3, 5)
vpa(sym(pi))
syms x
vpa(sin(x) + sym(pi), 5)
ans = 1.4142135623730950488
ans = 0.33333
ans = 3.1415926535897932384626433832795
ans = sin(x) + 3.1416
Notes
In MatLivre an explicit d larger than the current digits setting is not honored; raise digits first. A double argument such as vpa(pi) or vpa(1/3) is treated as the exact constant or fraction, whereas MATLAB shows the digits of the stored double (for example 0.33333333333333331483); use vpa(sym(...)) for portable results.