Symbolic Math Toolbox | ![]() ![]() |
Example: Using the Different Kinds of Arithmetic
Rational Arithmetic
By default, the Symbolic Math Toolbox uses rational arithmetic operations, i.e., Maple's exact symbolic arithmetic. Rational arithmetic is invoked when you create symbolic variables using the sym
function.
sym
function converts a double matrix to its symbolic form. For example, if the double matrix is
A = 1.1000 1.2000 1.3000 2.1000 2.2000 2.3000 3.1000 3.2000 3.3000Its symbolic form,
S = sym(A)
, is
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(2); log(3) rand]returns a matrix
E = 2.71828182845905 2.41421356237309 1.09861228866811 0.23113851357429whose elements are not the ratios of small integers, so
sym(E)
reproduces the floating-point representation in a symbolic form.
[ 6121026514868074*2^(-51), 5436325649948134*2^(-51)] [ 4947709893870346*2^(-52), 8327642588833064*2^(-55)]
Variable-Precision Numbers
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.
vpa
function generates the representation that is used with variable-precision arithmetic. Continuing on with our example, and using digits(4)
, applying vpa
to the matrix S
vpa(S)generates the output
S = [1.100, 1.200, 1.300] [2.100, 2.200, 2.300] [3.100, 3.200, 3.300]and with
digits(25)
F = vpa(E)generates
F = [2.718281828459045534884808, 1.414213562373094923430017] [1.098612288668110004152823, .2189591863280899719512718]
Converting to Floating-Point
To convert a rational or variable-precision number to its MATLAB floating-point representation, use the double
function.
double(sym(E))
and double(vpa(E))
return E
.
![]() | Variable-Precision Arithmetic | Another Example | ![]() |