Main Content

Create symbolic scalar variables, functions, and matrix variables

`syms `

creates symbolic scalar
variables `var1 ... varN`

`var1 ... varN`

of type `sym`

. Separate
different variables by spaces. This syntax clears all previous definitions of
`var1 ... varN`

. Since R2018b, `syms`

also clears all
assumptions from the variables.

`syms `

creates arrays of symbolic scalar variables `var1 ... varN`

`[n1 ... nM]`

`var1 ... varN`

, where each
array has the size
`n1`

-by-`...`

-by-`nM`

and contains
automatically generated symbolic scalar variables as its elements. For brevity, an array
of symbolic scalar variables is also called a symbolic array. For example, ```
syms a
[1 3]
```

creates the symbolic array `a = [a1 a2 a3]`

and the
symbolic scalar variables `a1`

, `a2`

, and
`a3`

in the MATLAB^{®} workspace. For multidimensional arrays, these elements have the prefix
`a`

followed by the element’s index using `_`

as a
delimiter, such as `a1_3_2`

.

`syms `

creates
`var1 ... varN`

n`n`

-by-`n`

matrices of symbolic scalar variables
filled with automatically generated elements. For brevity, a matrix of symbolic scalar
variables is also called a symbolic matrix.

`syms ___ `

sets the assumption
that the created symbolic scalar variables belong to `set`

`set`

, and clears
other assumptions. Here, `set`

can be `real`

,
`positive`

, `integer`

, or `rational`

.
You can also combine multiple assumptions using spaces. For example, ```
syms x
positive rational
```

creates a symbolic scalar variable `x`

with
a positive rational value.

`syms `

creates the symbolic
function `f(var1,...,varN)`

`f`

of type `symfun`

and the symbolic scalar
variables `var1,...,varN`

, which represent the input arguments of
`f`

. This syntax clears all previous definitions of
`var1,...,varN`

including symbolic assumptions. You can create multiple
symbolic functions in one call. For example, `syms f(x) g(t)`

creates two
symbolic functions (`f`

and `g`

) and two symbolic scalar
variables (`x`

and `t`

).

`syms `

creates an `f(var1,...,varN)`

`[n1 ... nM]`

`n1`

-by-`...`

-by-`nM`

symbolic array with automatically generated symbolic functions as its elements. This
syntax also generates the symbolic scalar variables `var1,...,varN`

that
represent the input arguments of `f`

. For example, ```
syms f(x) [1
2]
```

creates the symbolic array `f(x) = [f1(x) f2(x)]`

, the
symbolic functions `f1(x)`

and `f2(x)`

, and the symbolic
scalar variable `x`

in the MATLAB workspace. For multidimensional arrays, these elements have the prefix
`f`

followed by the element’s index using `_`

as a
delimiter, such as `f1_3_2`

.

`syms `

creates an
`f(var1,...,varN)`

n`n`

-by-`n`

matrix of symbolic functions filled with
automatically generated elements.

`syms `

creates symbolic matrix variables `var1 ... varN`

`[nrow ncol]`

matrix`var1 ... varN`

of type
`symmatrix`

, where each symbolic matrix variable has the size
`nrow`

-by-`ncol`

. *(since
R2021a)*

`syms `

creates
`var1 ... varN`

n matrix`n`

-by-`n`

symbolic matrix variables. *(since
R2021a)*

`syms(`

creates the symbolic scalar variables
and functions contained in `symArray`

)`symArray`

, where
`symArray`

is either a vector of symbolic scalar variables or a cell
array of symbolic scalar variables and functions. This syntax clears all previous
definitions of variables specified in `symArray`

including symbolic
assumptions. Use this syntax only when such an array is returned by another function, such
as `solve`

or `symReadSSCVariables`

.

Using Symbolic Math Toolbox™, you can create symbolic functions that depend on symbolic scalar variables as parameters. However, symbolic matrix variables cannot be parameter-dependent. For example, the command

`syms A(x) [3 2] matrix`

currently errors.Differentiation functions, such as

`jacobian`

and`laplacian`

, currently do not accept symbolic matrix variables as input. To evaluate differentiation with respect to vectors and matrices, you can use the`diff`

function instead.To show all the functions in Symbolic Math Toolbox that accept symbolic matrix variables as input, use the command

`methods symmatrix`

.

`syms`

is a shortcut for`sym`

. This shortcut lets you create several symbolic scalar variables in one function call. Alternatively, you can use`sym`

and create each variable separately. However, when you create variables using`sym`

, any existing assumptions on the created variables are retained. You can also use`symfun`

to create symbolic functions.In functions and scripts, do not use

`syms`

to create symbolic scalar variables with the same names as MATLAB functions. For these names, MATLAB does not create symbolic scalar variables, but keeps the names assigned to the MATLAB functions. If you want to create a symbolic scalar variable with the same name as a MATLAB function inside a function or a script, use`sym`

instead. For example, use`alpha = sym('alpha')`

.Avoid using

`syms`

within functions, since it generates variables without direct output assignment. Using`syms`

within functions can create side effects and other unexpected behaviors. Instead, use`sym`

with left-side output assignment within functions, such as`t = sym('t')`

. For more details, see Choose syms or sym Function.The following variable names are invalid with

`syms`

:`integer`

,`real`

,`rational`

,`positive`

, and`clear`

. To create symbolic scalar variables with these names, use`sym`

. For example,`real = sym('real')`

.`clear x`

does not clear the symbolic object of its assumptions, such as real, positive, or any assumptions set by`assume`

,`sym`

, or`syms`

. To remove assumptions, use one of these options:`syms x`

clears all assumptions from`x`

.`assume(x,'clear')`

clears all assumptions from`x`

.`clear all`

clears all objects in the MATLAB workspace and resets the symbolic engine.`assume`

and`assumeAlso`

provide more flexibility for setting assumptions on symbolic scalar variables.

When you replace one or more elements of a numeric vector or matrix with a symbolic number, MATLAB converts that number to a double-precision number.

A = eye(3); A(1,1) = sym(pi)

A = 3.1416 0 0 0 1.0000 0 0 0 1.0000

You cannot replace elements of a numeric vector or matrix with a symbolic scalar variable, expression, or function because these elements cannot be converted to double-precision numbers. For example,

`syms a; A(1,1) = a`

throws an error.

`assume`

| `assumeAlso`

| `assumptions`

| `isvarname`

| `reset`

| `sym`

| `symfun`

| `symvar`

| `symmatrix`

| `symmatrix2sym`