Variable-precision arithmetic (arbitrary-precision arithmetic)
Support for character vectors that do not define a number has been removed. Instead, first
create symbolic numbers and variables using
syms, and then use operations on them. For example, use
+ sqrt(sym(5)))/2) instead of
vpa('(1 + sqrt(5))/2').
variable-precision arithmetic (arbitrary-precision floating-point numbers)
to evaluate each element of the symbolic input
xVpa = vpa(
x to at
d significant digits, where
the value of the
digits function. The default value of
digits is 32.
Evaluate Symbolic Inputs with Variable-Precision Arithmetic
Evaluate symbolic inputs with variable-precision floating-point arithmetic. By default,
vpa calculates values to 32 significant digits.
p = sym(pi); pVpa = vpa(p)
syms x a = sym(1/3); f = a*sin(2*p*x); fVpa = vpa(f)
Evaluate elements of vectors or matrices with variable-precision arithmetic.
V = [x/p a^3]; VVpa = vpa(V)
M = [sin(p) cos(p/5); exp(p*x) x/log(p)]; MVpa = vpa(M)
Change Precision Used by
vpa evaluates inputs to 32 significant digits. You can change the number of significant digits by using the
Approximate the expression
100001/10001 with seven significant digits using
digits. Save the old value of
digits returned by
vpa function returns only five significant digits, which can mean the remaining digits are zeros.
digitsOld = digits(7); y = sym(100001)/10001; yVpa = vpa(y)
Check if the remaining digits are zeros by using a higher precision value of
25. The result shows that the remaining digits are in fact zeros that are part of a repeating decimal.
digits(25) yVpa = vpa(y)
Alternatively, to override
digits for a single
vpa call, change the precision by specifying the second argument.
Find π to 100 significant digits by specifying the second argument.
pVpa = vpa(pi,100)
Restore the original precision value in
digitsOld for further calculations.
Numerically Approximate Symbolic Results
While symbolic results are exact, they might not be in a convenient form. You can use
vpa to numerically approximate exact symbolic results.
Solve a high-degree polynomial for its roots using
solve function cannot symbolically solve the high-degree polynomial and represents the roots using
syms x y = solve(x^4 - x + 1, x)
vpa to numerically approximate the roots.
yVpa = vpa(y)
vpa Uses Guard Digits to Maintain Precision
The value of the
digits function specifies the minimum number of significant digits used. Internally,
vpa can use more digits than
digits specifies. These additional digits are called guard digits because they guard against round-off errors in subsequent calculations.
1/3 using four significant digits.
a = vpa(1/3,4)
Approximate the result
a using 20 digits. The result shows that the toolbox internally used more than four digits when computing
a. The last digits in the result are incorrect because of the round-off error.
aVpa = vpa(a,20)
Avoid Hidden Round-Off Errors
Hidden round-off errors can cause unexpected results.
1/10 with the default 32-digit precision and then with the 10-digit precision.
a = vpa(1/10,32)
b = vpa(1/10,10)
b look equal. Check their equality by finding
a - b.
roundoff = a - b
The difference is not equal to zero because
b was calculated with only
10 digits of precision and contains a larger round-off error than
a. When you find
a - b,
b with 32 digits. Demonstrate this behavior.
roundoff = a - vpa(b,32)
vpa Restores Precision of Common Double-Precision Inputs
Unlike exact symbolic values, double-precision values inherently contain round-off errors. When you call
vpa on a double-precision input,
vpa cannot restore the lost precision, even though it returns more digits than the double-precision value. However,
vpa can recognize and restore the precision of expressions of the form , , , , and , where and are modest-sized integers.
First, demonstrate that
vpa cannot restore precision for a double-precision input. Call
vpa 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
As expected, the double-precision result differs from the exact result at the 16th decimal place.
vpa restores precision for expressions of the form , , , , and , where and are modest-sized integers, by finding the difference between the
vpa call on the double-precision result and on the exact symbolic result. The differences are showing that
vpa restores lost precision for the double-precision input.
d = vpa(1/3) - vpa(1/sym(3))
d = vpa(pi) - vpa(sym(pi))
d = vpa(1/sqrt(2)) - vpa(1/sqrt(sym(2)))
d = vpa(2^66) - vpa(2^sym(66))
d = vpa(10^25) - vpa(10^sym(25))
Evaluate Symbolic Matrix Variable with Variable-Precision Arithmetic
Create a symbolic expression
S that represents , where is a 2-by-1 symbolic matrix variable.
syms X [2 1] matrix S = sin(hilb(2)*pi*X)
Evaluate the expression with variable-precision arithmetic.
SVpa = vpa(S)
x — Input to evaluate
number | vector | matrix | multidimensional array | symbolic number | symbolic vector | symbolic matrix | symbolic multidimensional array | symbolic expression | symbolic function | symbolic character vector | symbolic matrix variable
Input to evaluate, specified as a number, vector, matrix, multidimensional array, or a symbolic number, vector, matrix, multidimensional array, expression, function, character vector, or matrix variable.
d — Number of significant digits
positive integer scalar
Number of significant digits, specified as a positive integer scalar.
must be greater than 1 and less than .
vpadoes not convert fractions in the exponent to floating point. For example,
vpauses more digits than the number of digits specified by
digits. These extra digits guard against round-off errors in subsequent calculations and are called guard digits.
When you call
vpaon a numeric input, such as
sin(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
sym. For example, to approximate
If the second argument
dis not an integer,
vparounds it to the nearest integer with
vparestores precision for numeric inputs that match the forms p/q, pπ/q, (p/q)1/2, 2q, and 10q, where p and q are modest-sized integers.
Variable-precision arithmetic is different from IEEE® Floating-Point Standard 754 in these ways:
Inside computations, division by zero throws an error.
The exponent range is larger than in any predefined IEEE mode.
vpaunderflows below approximately
Denormalized numbers are not implemented.
Zeros are not signed.
The number of binary digits in the mantissa of a result may differ between variable-precision arithmetic and IEEE predefined types.
There is only one
NaNrepresentation. No distinction is made between quiet and signaling
No floating-point number exceptions are available.
Version HistoryIntroduced before R2006a
R2022b: Evaluate symbolic matrix variables with variable-precision arithmetic
You can evaluate a symbolic matrix variable of type
symmatrix with variable-precision arithmetic. The
result is a symbolic expression with variable-precision numbers and scalar
variables of type
sym. For an example, see Evaluate Symbolic Matrix Variable with Variable-Precision Arithmetic.