EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab vpa
 

Matlab vpa

Updated March 6, 2023

Matlab vpa

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Matlab vpa 1

Output:

Matlab vpa 2

Code using vpa:

syms x
[Initializing symbolic input]

InputNumber = sym (2 / 3);
[Declaring the input]

A2 = vpa (InputNumber)
[Displaying the output using vpa]

Input:

Matlab vpa 3

Output:

Matlab vpa 4

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:

Matlab vpa 5

Output:

to compute the value of a mathematical expression

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:

Matlab vpa 7

Output:

Matlab vpa 8

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:

Matlab vpa 9

Output:

mathematical expression involving square root

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:

Matlab vpa 11

Output:

Matlab vpa 12

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:

syms

Output:

how to get more than 32 digits,

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 –

  1. What is Matlab?
  2. Matlab limit
  3. Plot graph Matlab
  4. Absolute Value Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW