double
Convert symbolic values to MATLAB double precision
Syntax
Description
converts the symbolic values d
= double(s
)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
Convert Symbolic Number to Double Precision
Convert symbolic numbers to double precision by using double
. Symbolic numbers are exact, while double-precision numbers have round-off errors.
Convert and from symbolic form to double precision.
symN = sym([pi 1/3])
symN =
doubleN = double(symN)
doubleN = 1×2
3.1416 0.3333
For information on round-off errors, see Recognize and Avoid Round-Off Errors.
Convert Variable Precision to Double Precision
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 from variable-precision form to double precision.
vpaN = vpa([pi 1/3])
vpaN =
doubleN = double(vpaN)
doubleN = 1×2
3.1416 0.3333
Convert Symbolic Matrix to Double-Precision Matrix
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 =
doubleM = double(symM)
doubleM = 2×2
1.4142 0.6667
0.9428 0.4714
High-Precision Conversion
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)
Convert Symbolic Results to Double Precision
Solve the trigonometric equation 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 =
params =
conds =
The solver does not create the variable for the parameters in the MATLAB® workspace. Create this variable. Find the solutions for by using subs
.
syms k
sols_k2 = subs(solx,k,2)
sols_k2 =
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
Convert Symbolic Matrix Variable of Numbers to Double Precision
Create a symbolic expression S
that represents , where is a 2-by-2 symbolic matrix variable.
syms A 2 matrix S = A*A - 2*A + eye(2)
S =
Substitute with symbolic numbers .
Aval = [cos(sym(pi)/5) sin(pi/4); -1 0]; symS = subs(S,A,Aval)
symS =
Convert the result to a double-precision matrix.
doubleS = double(symS)
doubleS = 2×2
-0.6706 -0.8422
1.1910 0.2929
Input Arguments
s
— Symbolic input
symbolic number | array of symbolic numbers | symbolic matrix variable of numbers
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 R2006aR2022b: Convert symbolic matrix variables of numbers to double precision
You can convert a symbolic matrix variable of numbers of type
symmatrix
to a double-precision matrix of type
double
. For an example, see Convert Symbolic Matrix Variable of Numbers to Double Precision.
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)