Main Content

double

Convert symbolic values to MATLAB double precision

Description

example

d = double(s) 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, see Choose Numeric or Symbolic Arithmetic.

Examples

collapse all

Convert symbolic numbers to double precision by using double. Symbolic numbers are exact, while double-precision numbers have round-off errors.

Convert π and 13 from symbolic form to double precision.

symN = sym([pi 1/3])
symN = 

(π13)

doubleN = double(symN)
doubleN = 1×2

    3.1416    0.3333

For information on round-off errors, see Recognize and Avoid Round-Off Errors.

Variable-precision numbers created by vpa are symbolic values. When a MATLAB function does not accept symbolic values, convert variable precision to double precision by using double.

Convert π and 13 from variable-precision form to double precision.

vpaN = vpa([pi 1/3])
vpaN = (3.14159265358979323846264338327950.33333333333333333333333333333333)
doubleN = double(vpaN)
doubleN = 1×2

    3.1416    0.3333

Convert the symbolic numbers in matrix symM to double-precision numbers by using double.

a = sym(sqrt(2));
b = sym(2/3);
symM = [a b; a*b b/a]
symM = 

(22322323)

doubleM = double(symM)
doubleM = 2×2

    1.4142    0.6667
    0.9428    0.4714

When converting symbolic expressions that suffer from internal cancellation or round-off errors, increase the working precision by using digits before converting the number.

Convert a numerically unstable expression Y with double. Then, increase precision to 100 digits by using digits and convert Y 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 by digits for further calculations.

digits(digitsOld)

Solve the trigonometric equation sin(2x)+cos(x)=0 by using solve. Set the ReturnConditions option to true to return the complete solution, parameters used in the solution, and conditions on those parameters.

syms x
eqn = sin(2*x) + cos(x) == 0;
[solx,params,conds] = solve(eqn,x,ReturnConditions=true)
solx = 

(π2+πk2πk-π67π6+2πk)

params = k
conds = 

(kZkZkZ)

The solver does not create the variable k for the parameters in the MATLAB® workspace. Create this variable. Find the solutions for k=2 by using subs.

syms k
sols_k2 = subs(solx,k,2)
sols_k2 = 

(5π223π631π6)

The solutions are exact symbolic numbers. Convert these numbers to double-precision numbers.

doublesols_k2 = double(sols_k2)
doublesols_k2 = 3×1

    7.8540
   12.0428
   16.2316

Create a symbolic expression S that represents A2-2A+I2, where A is a 2-by-2 symbolic matrix variable.

syms A 2 matrix
S = A*A - 2*A + eye(2)
S = I2-2A+A2

Substitute A with symbolic numbers [cos(π5)sin(π4)-10].

Aval = [cos(sym(pi)/5) sin(pi/4); -1 0];
symS = subs(S,A,Aval)
symS = 

-2Σ1+Σ12+I2where  Σ1=(54+1422-10)

Convert the result to a double-precision matrix.

doubleS = double(symS)
doubleS = 2×2

   -0.6706   -0.8422
    1.1910    0.2929

Input Arguments

collapse all

Symbolic input, specified as a symbolic number, array of symbolic numbers, or symbolic matrix variable of numbers. If the input contains only numeric values (does not contain symbolic numbers), then the MATLAB double function is called.

Data Types: sym | symmatrix

Version History

Introduced before R2006a

expand all