# Choose `syms`

or `sym`

Function

In Symbolic Math Toolbox™, you can create symbolic objects by using either `syms`

or `sym`

. These two functions are conceptually different.

The

`syms`

function*creates*a symbolic object that is automatically assigned to a MATLAB® variable with the same name.The

`sym`

function*refers*to a symbolic object that can be assigned to a MATLAB variable with the same name or a different name.

The following examples discuss the differences between the `syms`

and `sym`

functions. For more examples on the use cases of each function, see `syms`

or `sym`

.

### Assign Symbolic Variables to MATLAB Variables

The `syms`

function *creates* a variable dynamically. For example, the command `syms x`

creates the symbolic variable `x`

and automatically assigns it to a MATLAB variable with the same name.

```
syms x
x
```

`x = $$x$$`

You can then use the variable `x`

in the MATLAB workspace for symbolic workflow, such as finding the roots of a polynomial.

f = x^2 + x - 6

`f = $${x}^{2}+x-6$$`

x0 = solve(f)

x0 =$$\left(\begin{array}{c}-3\\ 2\end{array}\right)$$

The `sym`

function *refers* to a symbolic variable, which you can then assign to a MATLAB variable with a different name. For example, the command `f1 = sym('x')`

refers to the symbolic variable `x`

and assigns it to the MATLAB variable `f1`

.

```
clear
f1 = sym('x')
```

`f1 = $$x$$`

You can then use the variable `f1`

in the MATLAB workspace for symbolic workflow, such as finding the zeros of a sine function.

f2 = sin(f1)

`f2 = $$\mathrm{sin}\left(x\right)$$`

`[solx,parameters,conditions] = solve(f2,f1,'ReturnConditions',true)`

`solx = $$\pi \hspace{0.17em}k$$`

`parameters = $$k$$`

`conditions = $$k\in \mathbb{Z}$$`

### Create Symbolic Number

Use the `syms`

function to *create* a symbolic variable `x`

and automatically assign it to a MATLAB variable `x`

. When you assign a number to the MATLAB variable `x`

, the number is represented in double-precision and this assignment overwrites the previous assignment to a symbolic variable. The class of `x`

becomes `double`

.

```
syms x
x = 1/33
```

x = 0.0303

class(x)

ans = 'double'

Use the `sym`

function to *refer* to an exact symbolic number without floating-point approximation. You can then assign this number to the MATLAB variable `x`

. The class of `x`

is `sym`

.

`x = sym('1/33')`

x =$$\frac{1}{33}$$

class(x)

ans = 'sym'

### Create Symbolic Variable with Assumptions

When you create a symbolic variable with an assumption, MATLAB stores the symbolic variable and its assumption separately.

Use `syms`

to *create* a symbolic variable that is assigned to a MATLAB variable with the same name. You get a fresh symbolic variable with no assumptions. If you declare a variable using `syms`

, existing assumptions are cleared.

syms x positive syms x assumptions

ans = Empty sym: 1-by-0

Use `sym`

to *refer* to an existing symbolic variable. If this symbolic variable was used in your MATLAB session before, then `sym`

refers to it and its current assumption. If it was not used before, then `sym`

creates it with no assumptions.

syms x positive x = sym('x'); assumptions

`ans = $$0<x$$`

### Create Many Symbolic Variables

To *create* many symbolic variables simultaneously, using the `syms`

function is more convenient. You can create multiple variables in one line of code.

syms a b c

When you use `sym`

, you have to declare MATLAB variables one by one and *refer* them to the corresponding symbolic variables.

a = sym('a'); b = sym('b'); c = sym('c');

### Create Array of Symbolic Variables

To declare a symbolic array that contains symbolic variables as its elements, you can use either `syms`

or `sym`

.

The command `syms a [1 3]`

*creates* a 1-by-3 symbolic array `a`

and the symbolic variables `a1`

, `a2`

, and `a3`

in the workspace. The symbolic variables `a1`

, `a2`

, and `a3`

are automatically assigned to the symbolic array `a`

.

clear syms a [1 3] a

`a = $$\left(\begin{array}{ccc}{a}_{1}& {a}_{2}& {a}_{3}\end{array}\right)$$`

whos

Name Size Bytes Class Attributes a 1x3 8 sym a1 1x1 8 sym a2 1x1 8 sym a3 1x1 8 sym

The command `a = sym('a',[1 3])`

*refers* to the symbolic variables `a1`

, `a2`

, and `a3`

, which are assigned to the symbolic array `a`

in the workspace. The elements `a1`

, `a2`

, and `a3`

are not created in the workspace.

```
clear
a = sym('a',[1 3])
```

`a = $$\left(\begin{array}{ccc}{a}_{1}& {a}_{2}& {a}_{3}\end{array}\right)$$`

whos

Name Size Bytes Class Attributes a 1x3 8 sym

### Create Symbolic Variable in Parallel For Loop

To create a symbolic variable in each worker for parallel computation, use `sym`

. For example, you can create a symbolic variable `k`

in a `parfor`

loop that performs sums of series using `symsum`

.

S = zeros(1,10); parfor i = 1:10 k = sym('k'); S(i) = symsum(1/k^i,k,1,Inf); end

Starting parallel pool (parpool) using the 'Processes' profile ... Connected to the parallel pool (number of workers: 6).

You cannot use `syms`

to create a symbolic variable in a `parfor`

loop because it modifies the global state of the workspace.

### Create Symbolic Variable in Function

To declare a symbolic variable within a function, use `sym`

. For example, you can explicitly define a MATLAB variable `x`

in the parent function workspace and refer `x`

to a symbolic variable with the same name.

function primaryFx x = sym('x') function nestedFx ... end end

Functions make the workspace static, so you cannot dynamically add variables using `syms`

.