Convert symbolic values toMATLABdouble precision
double(
converts the symbolic values
)s
to double precision. Converting symbolic values to double precision is useful when a MATLAB®function does not accept symbolic values. For differences between symbolic and double-precision numbers, seeChoose Numeric or Symbolic Arithmetic.
Convert symbolic numbers to double precision by usingdouble
. Symbolic numbers are exact while double-precision numbers have round-off errors.
Convertpi
and1/3
from symbolic form to double precision.
symN = sym([pi 1/3])
symN = [ pi, 1/3]
doubleN = double(symN)
doubleN = 3.1416 0.3333
For information on round-off errors, seeRecognize and Avoid Round-Off Errors.
Variable-precision numbers created byvpa
are symbolic values. When a MATLAB function does not accept symbolic values, convert variable precision to double precision by usingdouble
.
Convertpi
and1/3
from variable-precision form to double precision.
vpaN = vpa([pi 1/3])
vpaN = [ 3.1415926535897932384626433832795, 0.33333333333333333333333333333333]
doubleN = double(vpaN)
doubleN = 3.1416 0.3333
Convert the symbolic numbers in matrixsymM
to double-precision numbers by usingdouble
.
a = sym(sqrt(2)); b = sym(2/3); symM = [a b; a*b b/a]
symM = [ 2^(1/2), 2/3] [ (2*2^(1/2))/3, 2^(1/2)/3]
doubleM = double(symM)
doubleM = 1.4142 0.6667 0.9428 0.4714
When converting symbolic expressions that suffer from internal cancelation or round-off errors, increase the working precision by usingdigits
before converting the number.
Convert a numerically unstable expressionY
withdouble
. Then, increase precision to100
digits by usingdigits
and convertY
again. This high-precision conversion is accurate while the low-precision conversion is not.
Y = ((exp(sym(200)) + 1)/(exp(sym(200)) - 1)) - 1; lowPrecisionY = double(Y)
lowPrecisionY = 0
digitsOld = digits(100); highPrecisionY = double(Y)
highPrecisionY = 2.7678e-87
Restore the old precision used bydigits
for further calculations.
digits(digitsOld)