Accelerating the pace of engineering and science

• Trials

# vpa

Variable-precision arithmetic

R = vpa(A)
R = vpa(A,d)

## Description

R = vpa(A) uses variable-precision arithmetic (VPA) to compute each element of A to at least d decimal digits of accuracy, where d is the current setting of digits.

R = vpa(A,d) uses at least d significant (nonzero) digits, instead of the current setting of digits.

## Input Arguments

 A Symbolic object, string, or numeric expression. d Integer greater than 1 and smaller than .

## Output Arguments

 R Symbolic object representing a floating-point number

## Examples

Approximate the following expressions with the 25 digits precision:

```old = digits(25);
q = vpa('1/2')
p = vpa(pi)
w = vpa('(1+sqrt(5))/2')
digits(old)```
```q =
0.5

p =
3.141592653589793238462643

w =
1.618033988749894848204587```

Solve the following equation:

`y = solve('x^2 - 2')`
```y =
2^(1/2)
-2^(1/2)```

Approximate the solutions with floating-point numbers:

```vpa(y(1))
vpa(y(2))```
```ans =
1.4142135623730950488016887242097

ans =
-1.4142135623730950488016887242097```

Use the vpa function to approximate elements of the following matrices:

```A = vpa(hilb(2), 25)
B = vpa(hilb(2), 5)```
```A =
[ 1.0,                         0.5]
[ 0.5, 0.3333333333333333333333333]

B =
[ 1.0,     0.5]
[ 0.5, 0.33333]```

The vpa function lets you specify a number of significant (nonzero) digits that is different from the current digits setting. For example, compute the ratio 1/3 and the ratio 1/3000 with 4 significant digits:

```vpa(1/3, 4)
vpa(1/3000, 4)```
```ans =
0.3333

ans =
0.0003333```

The number of digits that you specify by the vpa function or the digits function is the minimal number of digits. Internally, the toolbox can use more digits than you specify. These additional digits are called guard digits. For example, set the number of digits to 4, and then display the floating-point approximation of 1/3 using 4 digits:

```old = digits;
digits(4)
a = vpa(1/3, 4)```
```a =
0.3333```

Now, display a using 20 digits. The result shows that the toolbox internally used more than 4 digits when computing a. The last digits in the following result are incorrect because of the round-off error:

```vpa(a, 20)
digits(old)```
```ans =
0.33333333333303016843```

Hidden round-off errors can cause unexpected results. For example, compute the number 1/10 with the default 32 digits accuracy and with the 10 digits accuracy:

```a = vpa(1/10, 32)
b = vpa(1/10, 10)```
```a =
0.1

b =
0.1```

Now, compute the difference a - b:

`a - b`
```ans =
0.000000000000000000086736173798840354720600815844403```

The difference is not equal to zero because the toolbox approximates the number b=0.1 with 32 digits. This approximation produces round-off errors because the floating point number 0.1 is different from the rational number 1/10. When you compute the difference a - b, the toolbox actually computes the difference as follows:

`a - vpa(b, 32)`
```ans =
0.000000000000000000086736173798840354720600815844403```

Suppose you convert a number to a symbolic object, and then perform VPA operations on that object. The results can depend on the conversion technique that you used to convert a floating-point number to a symbolic object. The sym function lets you choose the conversion technique by specifying the optional second argument, which can be 'r', 'f', 'd' or 'e'. The default is 'r'. For example, convert the constant π=3.141592653589793... to a symbolic object:

```r = sym(pi)
f = sym(pi, 'f')
d = sym(pi, 'd')
e = sym(pi, 'e')```
```r =
pi

f =
884279719003555/281474976710656

d =
3.1415926535897931159979634685442

e =
pi - (198*eps)/359```

Compute these numbers with the 4 digits VPA precision. Three of the four numeric approximations give the same result:

```vpa(r, 4)
vpa(f, 4)
vpa(d, 4)
vpa(e, 4)```
```ans =
3.142

ans =
3.142

ans =
3.142

ans =
3.142 - 0.5515*eps```

Now, increase the VPA precision to 40 digits. The numeric approximation of 1/10 depends on the technique that you used to convert 1/10 to the symbolic object:

```vpa(r, 40)
vpa(f, 40)
vpa(d, 40)
vpa(e, 40)```
```ans =
3.141592653589793238462643383279502884197

ans =
3.141592653589793115997963468544185161591

ans =
3.1415926535897931159979634685442

ans =
3.141592653589793238462643383279502884197 -...
0.5515320334261838440111420612813370473538*eps```

expand all

### Tips

• The toolbox increases the internal precision of calculations by several digits (guard digits).

• When you apply vpa to a numeric expression, such as 1/3, 2^(-5), or sin(pi/4), it is evaluated to a double-precision number. Then, vpa is applied to that double-precision number. For more accurate results, convert numeric expressions to symbolic expressions. For example, to approximate exp(1) use vpa(sym(exp(1)).

• If the value d is not an integer, vpa rounds it to the nearest integer.