Numeric to Symbolic Conversion
This topic shows how Symbolic Math Toolbox™ converts numbers into symbolic form. For an overview of symbolic and numeric arithmetic, see Choose Numeric or Symbolic Arithmetic.
To convert numeric input to symbolic form, use the sym command. By default, sym returns a rational approximation of a numeric expression.
t = 0.1; sym(t)
ans =
sym determines that the double-precision value 0.1 approximates the exact symbolic value . In general, sym tries to correct the round-off error in floating-point inputs to return the exact symbolic form. Specifically, sym corrects round-off error in numeric inputs that match the forms , , , , and , where and are modest-sized integers.
For these forms, demonstrate that sym converts floating-point inputs to the exact symbolic form. First, numerically approximate , , and .
N = [1/7 pi 1/sqrt(2)]
N = 1×3
0.1429 3.1416 0.7071
Convert the numeric approximations to exact symbolic form. sym corrects the round-off error.
S = sym(N)
S =
You can force sym to accept the input as is by placing the input in quotes. Demonstrate this behavior on the previous input 0.142857142857143. The sym function does not convert the input to 1/7.
sym('0.142857142857143')ans =
When you convert large numbers, use quotes to exactly represent them. Demonstrate this behavior by comparing sym(133333333333333333333) with sym('133333333333333333333').
sym(1333333333333333333)
ans =
sym('1333333333333333333')ans =
You can specify the technique used by sym to convert floating-point numbers using the optional second argument, which can be 'f', 'r', 'e', or 'd'. The default flag is 'r', for rational form.
Conversion to Rational Symbolic Form
Convert input to exact rational form by calling sym with the 'r' flag. This is the default behavior when you call sym without flags.
t = 0.1;
sym(t,'r')ans =
Conversion by Using Floating-Point Expansion
If you call sym with the flag 'f', sym converts double-precision, floating-point numbers to their numeric value by using , where and are the exponent and mantissa respectively.
Convert t by using a floating-point expansion.
sym(t,'f')ans =
Conversion to Rational Symbolic Form with Error Term
If you call sym with the flag 'e', sym returns the rational form of t plus the error between the estimated, exact value for t and its floating-point representation. This error is expressed in terms of eps (the floating-point relative precision).
Convert t to symbolic form. Return the error between its estimated symbolic form and its floating-point value.
sym(t,'e')ans =
The error term eps/40 is the difference between sym('0.1') and sym(0.1).
Conversion to Decimal Form
If you call sym with the flag 'd', sym returns the decimal expansion of the input. The digits function specifies the number of significant digits used. The default value of digits is 32.
sym(t,'d')ans =
Change the number of significant digits by using digits.
digitsOld = digits(7);
sym(t,'d')ans =
For further calculations, restore the old value of digits.
digits(digitsOld)
Conversion to Variable Precision
You can create symbolic numbers with variable-precision floating-point arithmetic by using vpa. By default, vpa calculates values to 32 significant digits.
piVpa = vpa(pi)
piVpa =
When you use vpa on a numeric input, such as log(2), the numeric expression is first evaluated to the MATLAB® default double-precision number that has less than 32 significant digits. Then, vpa is applied on that double-precision number, which can be less accurate. For more accurate results, convert numeric expressions to symbolic expressions with sym and then use vpa to evaluate the results with variable precision. For example, find log(2) with 17 and 20 digits precision.
vpaOnDouble = vpa(log(2))
vpaOnDouble =
vpaOnSym_17 = vpa(log(sym(2)),17)
vpaOnSym_17 =
vpaOnSym_20 = vpa(log(sym(2)),20)
vpaOnSym_20 =
When you convert large numbers, use quotes to exactly represent them.
inaccurateNum = vpa(123456789012345678)
inaccurateNum =
accurateNum = vpa('123456789012345678')accurateNum =