## Introduction to Matlab vpa

The following article provides an outline for Matlab vpa. Matlab variable precision arithmetic is used in calculations where large numbers are involved (as input or output), and the primary focus is on precision and not the speed of computation. The high precision for these long numbers is achieved by algorithms that group these numbers into smaller parts for calculation.

For example, the mathematical ‘pi’ has more than 31 Trillion digits, but we usually use 3.14 as its value. However, some of our calculations might require more digits to be involved, and this is where vpa (variable precision arithmetic) comes in handy.

**Syntax:**

`vpa (a)`

`vpa (a, n)`

**Explanation:**

- The vpa (a) is used to compute the elements of the input ‘a’ till ‘n’ significant digits.
- By default, the value of significant digits is 32 for the vpa function.

### Examples of Matlab vpa

Given below examples shows how to use vpa in Matlab:

#### Example #1

In this example, we will use vpa to perform a non-terminating division. First, we will compute the result using the normal division in Matlab, and then we will use vpa to understand the utility of vpa clearly.

For this example, we will divide 2 by 3, which will give us a non-terminating output.

**Code without using vpa:**

A1 = double (2) / double (3)

[Using ‘double’ as the data type to ensure that the decimal part is displayed in the output]

A1

[Displaying the output (without vpa)]

**Input:**

4.5 (6,382 ratings)

View Course

**Output:**

**Code using vpa:**

syms x

[Initializing symbolic input]

InputNumber = sym (2 / 3);

[Declaring the input]

A2 = vpa (InputNumber)

[Displaying the output using vpa]

**Input:**

**Output:**

If we compare the 2 outputs above, we will notice that the first one has 4 significant digits, whereas the second one has 32, and thus the second one, which uses vpa, provides more precision.

#### Example #2

Next, we will use vpa to compute the value of a mathematical expression.

For this example, we will add two non-terminating numbers and will see the difference in precision while using vpa. Here the output will also be a non-terminating number.

**Code without using vpa:**

Input1 = double (2 / 3)

[Declaring the first input, using ‘double’ as the data type to ensure that the decimal part is displayed in the output]

Input2 = double (pi)

[Declaring the second input]

Input1 + Input2

[Adding the 2 inputs (without using vpa)]

**Input:**

**Output:**

**Code using vpa:**

syms x

[Initializing symbolic input]

Input1 = sym (2 / 3)

[Declaring the first input]

Input2 = sym (pi)

[Declaring the second input]

vpa(Input1) + vpa(Input2)

[Adding the 2 inputs using vpa]

**Input:**

**Output:**

As we can see in the two output above, we have obtained 5 significant digits without using the vpa, whereas 32 significant digits while using the vpa, resulting in more precision.

#### Example #3

Next, we will use vpa to compute the value of a mathematical expression involving square root.

For this example, we will add 2 numbers with non-terminating and non-repeating square roots and will see the difference in precision while using vpa. Here also, the output will be a non-terminating number.

**Code without using vpa:**

Input1 = sqrt (2)

[Declaring the first input variable and using the sqrt function to compute the square root]

Input2 = sqrt (5)

[Declaring the second input variable and using the sqrt function to compute the square root]

Input1 + Input2

[Adding the 2 inputs (without using vpa)]

**Input:**

**Output:**

**Code using vpa:**

syms x

[Initializing symbolic input]

Input1 = sqrt (2)

[Declaring the first input variable]

Input2 = sqrt (5)

[Declaring the second input variable]

vpa(Input1) + vpa(Input2)

[Adding the 2 inputs using vpa]

**Input:**

**Output:**

As we can see in the two outputs obtained above, we have obtained 5 significant digits without using the vpa, whereas 32 significant digits while using the vpa, resulting in more precision.

In the above 3 examples, we got 32 significant digits as the output; however, we can get more or less than 32 digits if required. For this, we will pass the required number of digits as the second argument to the vpa function.

#### Example #4

In this example, we will learn how to get more than 32 digits, say 50, in the output.

**Code:**

syms x

[Initializing symbolic input]

A = vpa(sqrt(3), 50)

[Declaring the first input, and using vpa to get 50 significant digits]

B = vpa(3*pi, 50)

[Declaring the second input]

vpa(A + B, 50)

[Adding the 2 inputs using vpa and again passing 50 as the second argument to get 50 significant digits]

**Input:**

**Output:**

As we can see in the output, we now have 50 significant digits as expected by us.

### Conclusion

The vpa is used in Matlab to increase precision in the output. We can control the number of significant digits in the output using the input argument; by default, this number is 32. Using vpa can affect the performance of a program, as the focus is on precision.

### Recommended Articles

This is a guide to Matlab vpa. Here we discuss the introduction and the examples of Matlab vpa for a better understanding. You may also have a look at the following articles to learn more –