Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Create symbolic variables and functions

`syms var1 ... varN`

`syms var1 ... varN set`

`syms var1 ... varN clear`

`syms f(var1,...,varN)`

`syms(symArray)`

`syms`

`S = syms`

`syms `

creates
symbolic variables `var1 ... varN`

`var1 ... varN`

. Separate variables
by spaces.

`syms `

sets
an assumption that the created symbolic variables belong to a `var1 ... varN`

`set`

`set`

.

`syms `

clears
assumptions set on a symbolic variables `var1 ... varN`

clear`var1 ... varN`

.

`syms `

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

`f`

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

representing
the input arguments of `f`

. 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 variables (`x`

and `t`

).

`syms(`

creates
the symbolic variables and functions contained in `symArray`

)`symArray`

,
where `symArray`

is either a vector of symbolic
variables or a cell array of symbolic variables and functions. Use
this syntax only when such an array is returned by another function,
such as `solve`

or `symReadSSCVariables`

.

Create symbolic variables `x`

and `y`

.

syms x y

Create symbolic variables `x`

and `y`

,
and assume that they are integers.

syms x y integer

Check assumptions.

assumptions

ans = [ in(x, 'integer'), in(y, 'integer')]

Alternatively, check assumptions on each variable. For example,
check assumptions set on the variable `x`

.

assumptions(x)

ans = in(x, 'integer')

Clear assumptions on `x`

and `y`

.

assume([x y],'clear') assumptions

ans = Empty sym: 1-by-0

Create symbolic functions with one and two arguments.

syms s(t) f(x,y)

Both `s`

and `f`

are abstract
symbolic functions. They do not have symbolic expressions assigned
to them, so the bodies of these functions are `s(t)`

and `f(x,y)`

,
respectively.

Specify the following formula for `f`

.

f(x,y) = x + 2*y

f(x, y) = x + 2*y

Compute the function value at the point `x = 1`

and ```
y
= 2
```

.

f(1,2)

ans = 5

Create a symbolic function and specify its formula by using a symbolic matrix.

syms x f(x) = [x x^3; x^2 x^4]

f(x) = [ x, x^3] [ x^2, x^4]

Compute the function value at the point `x = 2`

:

f(2)

ans = [ 2, 8] [ 4, 16]

Compute the value of this function for ```
x = [1 2 3;
4 5 6]
```

. The result is a cell array of symbolic matrices.

y = f([1 2 3; 4 5 6])

y = 2×2 cell array [2×3 sym] [2×3 sym] [2×3 sym] [2×3 sym]

Access the contents of a cell in the cell array by using braces.

y{1}

ans = [ 1, 2, 3] [ 4, 5, 6]

Certain functions, such as `solve`

and `symReadSSCVariables`

,
can return a vector of symbolic variables or a cell array of symbolic
variables and functions. These variables or functions do not automatically
appear in the MATLAB workspace. Create these variables or functions
from the vector or cell array by using `syms`

.

Solve the equation `sin(x) == 1`

by using `solve`

.
The parameter `k`

in the solution does not appear
in the MATLAB workspace.

syms x [sol, parameter, condition] = solve(sin(x) == 1, x, 'ReturnConditions', true); parameter

parameter = k

Create the parameter `k`

by using `syms`

.
The parameter `k`

now appears in the MATLAB workspace.

syms(parameter)

Similarly, use `syms`

to create the symbolic
objects contained in a vector or cell array. Examples of functions
that return a cell array of symbolic objects are `symReadSSCVariables`

and `symReadSSCParameters`

.

Create some symbolic variables, functions, and arrays.

syms a f(x) A = sym('A',[2 3]);

Display a list of all symbolic objects that currently exist
in the MATLAB workspace by using `syms`

.

syms

Your symbolic variables are: A a f x

Instead of displaying a list, return a cell array of all symbolic
objects by providing an output to `syms`

.

S = syms

S = 4×1 cell array 'A' 'a' 'f' 'x'

Create some symbolic variables, functions, and arrays.

syms a f(x) A = sym('A',[2 3]);

Check if `x`

exists in the output of `syms`

by
using `ismember`

and `any`

.
The `any`

function returns logical `1`

(`true`

),
meaning `x`

does exist in the output of `syms`

.

checkVar = sym('x'); S = syms; any(ismember(S,checkVar))

ans = logical 1

Create several symbolic objects.

syms a b c f(x)

Delete all symbolic objects by clearing the output of `syms`

.

symObj = syms; cellfun(@clear,symObj)

Check that you deleted all symbolic objects by calling `syms`

.
The output is empty meaning no symbolic objects exist in the MATLAB workspace.

syms

`syms`

is a shortcut for`sym`

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

and create each variable separately. You also can use`symfun`

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

`syms`

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

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

.The following variable names are invalid with

`syms`

:`integer`

,`real`

,`rational`

,`positive`

, and`clear`

. To create 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:`assume(x,'clear')`

removes all assumptions affecting`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 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 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`

| `clear all`

| `reset`

| `sym`

| `symfun`

| `symvar`

Was this topic helpful?