| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Symbolic Math Toolbox |
| Contents | Index |
| Learn more about Symbolic Math Toolbox |
| On this page… |
|---|
There are three different kinds of arithmetic operations in this toolbox:
Numeric | MATLAB floating-point arithmetic |
Rational | MuPAD® software exact symbolic arithmetic |
VPA | MuPAD software variable-precision arithmetic |
For example, the MATLAB statements
format long 1/2 + 1/3
use numeric computation to produce
ans = 0.833333333333333
With Symbolic Math Toolbox software, the statement
sym(1/2) + 1/3
uses symbolic computation to yield
ans = 5/6
And, also with the toolbox, the statements
digits(25)
vpa('1/2 + 1/3')use variable-precision arithmetic to return
ans = 0.8333333333333333333333333
The floating-point operations used by numeric arithmetic are the fastest of the three, and require the least computer memory, but the results are not exact. The number of digits in the printed output of MATLAB double quantities is controlled by the format statement, but the internal representation is always the eight-byte floating-point representation provided by the particular computer hardware.
In the computation of the numeric result above, there are actually three roundoff errors, one in the division of 1 by 3, one in the addition of 1/2 to the result of the division, and one in the binary to decimal conversion for the printed output. On computers that use IEEE® floating-point standard arithmetic, the resulting internal value is the binary expansion of 5/6, truncated to 53 bits. This is approximately 16 decimal digits. But, in this particular case, the printed output shows only 15 digits.
The symbolic operations used by rational arithmetic are potentially the most expensive of the three, in terms of both computer time and memory. The results are exact, as long as enough time and memory are available to complete the computations.
Variable-precision arithmetic falls in between the other two in terms of both cost and accuracy. A global parameter, set by the function digits, controls the number of significant decimal digits. Increasing the number of digits increases the accuracy, but also increases both the time and memory requirements. The default value of digits is 32, corresponding roughly to floating-point accuracy.
By default, Symbolic Math Toolbox software uses rational arithmetic operations, i.e., MuPAD software's exact symbolic arithmetic. Rational arithmetic is invoked when you create symbolic variables using the sym function.
The sym function converts a double matrix to its symbolic form. For example, if the double matrix is
format short; A = [1.1,1.2,1.3;2.1,2.2,2.3;3.1,3.2,3.3]
A =
1.1000 1.2000 1.3000
2.1000 2.2000 2.3000
3.1000 3.2000 3.3000its symbolic form is:
S = sym(A)
S = [ 11/10, 6/5, 13/10] [ 21/10, 11/5, 23/10] [ 31/10, 16/5, 33/10]
For this matrix A, it is possible to discover that the elements are the ratios of small integers, so the symbolic representation is formed from those integers. On the other hand, the statement
E = [exp(1) (1 + sqrt(5))/2; log(3) rand]
returns a matrix
E =
2.7183 1.6180
1.0986 0.6324whose elements are not the ratios of small integers, so
sym(E)
reproduces the floating-point representation in a symbolic form:
ans = [ 3060513257434037/1125899906842624, 910872158600853/562949953421312] [ 2473854946935173/2251799813685248, 1423946432832521/2251799813685248]
Variable-precision numbers are distinguished from the exact rational representation by the presence of a decimal point. A power of 10 scale factor, denoted by 'e', is allowed. To use variable-precision instead of rational arithmetic, create your variables using the vpa function.
For matrices with purely double entries, the vpa function generates the representation that is used with variable-precision arithmetic. For example, if you apply vpa to the matrix S defined in the preceding section, with digits(4), by entering
vpa(S)
MATLAB returns the output
ans = [ 1.1, 1.2, 1.3] [ 2.1, 2.2, 2.3] [ 3.1, 3.2, 3.3]
Applying vpa to the matrix E defined in the preceding section, with digits(25), by entering
digits(25) F = vpa(E)
returns
F = [ 2.718281828459045534884808, 1.618033988749894902525739] [ 1.098612288668109560063613, 0.6323592462254095103446616]
To convert a rational or variable-precision number to its MATLAB floating-point representation, use the double function.
In the example, both double(sym(E)) and double(vpa(E)) return E.
The next example is perhaps more interesting. Start with the symbolic expression
f = sym('exp(pi*sqrt(163))')The statement
format long; double(f)
produces the printed floating-point value
ans =
2.625374126407687e+017Using the second argument of vpa to specify the number of digits,
vpa(f,18)
returns
ans = 262537412640768744.0
and, too,
vpa(f,25)
returns
ans = 262537412640768744.0
You might suspect that f actually has an integer value. This suspicion is reinforced by the 30 digit value:
vpa(f,30)
ans = 262537412640768743.999999999999
Finally, the 40–digit value:
vpa(f,40)
ans = 262537412640768743.9999999999992500725972
shows that f is very close to, but not exactly equal to, an integer.
![]() | Simplifications and Substitutions | Linear Algebra | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |