Main Content

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 = 

110

sym determines that the double-precision value 0.1 approximates the exact symbolic value 110. 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 pq, pπq, (pq)12, 2q, and 10q, where p and q are modest-sized integers.

For these forms, demonstrate that sym converts floating-point inputs to the exact symbolic form. First, numerically approximate 17, π, and 12.

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 = 

(17π22)

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 = 0.142857142857143

When you convert large numbers, use quotes to exactly represent them. Demonstrate this behavior by comparing sym(133333333333333333333) with sym('133333333333333333333').

sym(1333333333333333333)
ans = 1333333333333333248
sym('1333333333333333333')
ans = 1333333333333333333

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 = 

110

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 N2e, where N and e are the exponent and mantissa respectively.

Convert t by using a floating-point expansion.

sym(t,'f')
ans = 

360287970189639736028797018963968

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 = 

eps40+110

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 = 0.10000000000000000555111512312578

Change the number of significant digits by using digits.

digitsOld = digits(7);
sym(t,'d')
ans = 0.1

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 = 3.1415926535897932384626433832795

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 = 0.69314718055994528622676398299518
vpaOnSym_17 = vpa(log(sym(2)),17)
vpaOnSym_17 = 0.69314718055994531
vpaOnSym_20 = vpa(log(sym(2)),20)
vpaOnSym_20 = 0.69314718055994530942

When you convert large numbers, use quotes to exactly represent them.

inaccurateNum = vpa(123456789012345678)
inaccurateNum = 123456789012345680.0
accurateNum = vpa('123456789012345678')
accurateNum = 123456789012345678.0

Related Topics