# double

Convert symbolic values to MATLAB double precision

## Syntax

``d = double(s)``

## 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 $\pi$ and $\frac{1}{3}$ from symbolic form to double precision.

`symN = sym([pi 1/3])`
```symN =  $\left(\begin{array}{cc}\pi & \frac{1}{3}\end{array}\right)$```
`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 $\pi$ and $\frac{1}{3}$ from variable-precision form to double precision.

`vpaN = vpa([pi 1/3])`
`vpaN = $\left(\begin{array}{cc}3.1415926535897932384626433832795& 0.33333333333333333333333333333333\end{array}\right)$`
`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 =  $\left(\begin{array}{cc}\sqrt{2}& \frac{2}{3}\\ \frac{2 \sqrt{2}}{3}& \frac{\sqrt{2}}{3}\end{array}\right)$```
`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 $\mathrm{sin}\left(2\mathit{x}\right)+\mathrm{cos}\left(\mathit{x}\right)=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 =  $\left(\begin{array}{c}\frac{\pi }{2}+\pi k\\ 2 \pi k-\frac{\pi }{6}\\ \frac{7 \pi }{6}+2 \pi k\end{array}\right)$```
`params = $k$`
```conds =  $\left(\begin{array}{c}k\in \mathbb{Z}\\ k\in \mathbb{Z}\\ k\in \mathbb{Z}\end{array}\right)$```

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

```syms k sols_k2 = subs(solx,k,2)```
```sols_k2 =  $\left(\begin{array}{c}\frac{5 \pi }{2}\\ \frac{23 \pi }{6}\\ \frac{31 \pi }{6}\end{array}\right)$```

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 ${\mathbit{A}}^{2}-2\mathbit{A}+{\mathbit{I}}_{2}$, where $\mathbit{A}$ is a 2-by-2 symbolic matrix variable.

```syms A 2 matrix S = A*A - 2*A + eye(2)```
`S = ${\mathrm{I}}_{2}-2 A+{A}^{2}$`

Substitute $\mathbit{A}$ with symbolic numbers $\left[\begin{array}{cc}\mathrm{cos}\left(\frac{\text{\hspace{0.17em}}\pi }{5}\right)& \mathrm{sin}\left(\frac{\pi }{4}\right)\\ -1& 0\end{array}\right]$.

```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

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