Main Content

`syms`

or `sym`

FunctionIn Symbolic Math Toolbox™, you can declare symbolic objects 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 `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$$`

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`

.

`f1 = sym('x')`

`f1 = $$x$$`

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'

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

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');

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

To declare a symbolic variable within a nested 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

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

.