Note: This page has been translated by MathWorks. Click here to see

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

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

Create symbolic variables, expressions, functions, matrices

**Support of character vectors that are not valid variable names and that do not define a number
has been removed. To create symbolic expressions, first create symbolic variables, and
then use operations on them. For example, use syms x; x + 1 instead
of sym('x + 1'), exp(sym(pi)) instead of
sym('exp(pi)'), and syms f(var1,...varN)
instead of f(var1,...varN) = sym('f(var1,...varN)').**

`x = sym('x')`

`A = sym('a',[n1 ... nM])`

`A = sym('a',n)`

`sym(___,set)`

`sym(___,'clear')`

`sym(num)`

`sym(num,flag)`

`symexpr = sym(h)`

creates an
`A`

= sym('`a`

',`[n1 ... nM]`

)`n1`

-by-`...`

-by-`nM`

symbolic array filled with automatically generated elements. For example,
`A = sym('a',[1 3])`

creates the row vector ```
A =
[a1 a2 a3]
```

. The generated elements `a1`

,
`a2`

, and `a3`

do not appear 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`

.

`sym(___,`

creates a symbolic variable
or array and sets the assumption that the variable or all array elements belong
to a `set`

)`set`

. Here, `set`

can be
`'real'`

, `'positive'`

,
`'integer'`

, or `'rational'`

. You also can
combine multiple assumptions by specifying a string array or cell array of
character vectors. For example, assume a positive rational value by specifying
`set`

as `["positive" "rational"]`

or
`{'positive','rational'}`

.

Create the symbolic variables `x`

and `y`

.

x = sym('x'); y = sym('y');

Create a 1-by-4 symbolic vector `a`

with automatically
generated elements `a1`

, ..., `a4`

.

a = sym('a',[1 4])

a = [ a1, a2, a3, a4]

Format the names of elements of `a`

by using
a format character vector as the first argument. `sym`

replaces `%d`

in
the format character vector with the index of the element to generate
the element names.

a = sym('x_%d',[1 4])

a = [ x_1, x_2, x_3, x_4]

This syntax does not create symbolic variables `x_1`

,
..., `x_4`

in the MATLAB workspace. Access elements
of `a`

using standard indexing methods.

a(1) a(2:3)

ans = x_1 ans = [ x_2, x_3]

Create a 3-by-4 symbolic matrix with automatically generated elements. The
elements are of the form `Ai_j`

, which generates the elements
`A1_1`

, ..., `A3_4`

.

A = sym('A',[3 4])

A = [ A1_1, A1_2, A1_3, A1_4] [ A2_1, A2_2, A2_3, A2_4] [ A3_1, A3_2, A3_3, A3_4]

Create a 4-by-4 matrix with the element names `x_1_1`

,
..., `x_4_4`

by using a format character vector as
the first argument. `sym`

replaces `%d`

in
the format character vector with the index of the element to generate
the element names.

B = sym('x_%d_%d',4)

B = [ x_1_1, x_1_2, x_1_3, x_1_4] [ x_2_1, x_2_2, x_2_3, x_2_4] [ x_3_1, x_3_2, x_3_3, x_3_4] [ x_4_1, x_4_2, x_4_3, x_4_4]

This syntax does not create symbolic variables `A1_1`

,
..., `A3_4`

, `x_1_1`

, ..., `x_4_4`

in
the MATLAB workspace. To access an element of a matrix, use parentheses.

A(2,3) B(4,2)

ans = A2_3 ans = x_4_2

Create a 2-by-2-by-2 symbolic array with automatically
generated elements `A1_1_1`

, ..., `A2_2_2`

.

A = sym('a',[2 2 2])

A(:,:,1) = [ a1_1_1, a1_2_1] [ a2_1_1, a2_2_1] A(:,:,2) = [ a1_1_2, a1_2_2] [ a2_1_2, a2_2_2]

Convert numeric values to symbolic numbers
or expressions. Use `sym`

on subexpressions instead
of the entire expression for better accuracy. Using `sym`

on
entire expressions is inaccurate because MATLAB first converts
the expression to a floating-point number, which loses accuracy. `sym`

cannot
always recover this lost accuracy.

inaccurate1 = sym(1/1234567) accurate1 = 1/sym(1234567) inaccurate2 = sym(sqrt(1234567)) accurate2 = sqrt(sym(1234567)) inaccurate3 = sym(exp(pi)) accurate3 = exp(sym(pi))

inaccurate1 = 7650239286923505/9444732965739290427392 accurate1 = 1/1234567 inaccurate2 = 4886716562018589/4398046511104 accurate2 = 1234567^(1/2) inaccurate3 = 6513525919879993/281474976710656 accurate3 = exp(pi)

When creating symbolic numbers with 15 or more digits, use quotation marks to accurately represent the numbers.

inaccurateNum = sym(11111111111111111111) accurateNum = sym('11111111111111111111')

inaccurateNum = 11111111111111110656 accurateNum = 11111111111111111111

When you use quotation marks to create symbolic complex numbers,
specify the imaginary part of a number as `1i`

, `2i`

,
and so on.

sym('1234567 + 1i')

ans = 1234567 + 1i

Create a symbolic expression and a symbolic matrix from anonymous functions associated with MATLAB handles.

h_expr = @(x)(sin(x) + cos(x)); sym_expr = sym(h_expr)

sym_expr = cos(x) + sin(x)

h_matrix = @(x)(x*pascal(3)); sym_matrix = sym(h_matrix)

sym_matrix = [ x, x, x] [ x, 2*x, 3*x] [ x, 3*x, 6*x]

Create the symbolic variables `x`

, `y`

,
`z`

, and `t`

while simultaneously assuming
that `x`

is real, `y`

is positive,
`z`

rational, and `t`

is positive
integer.

x = sym('x','real'); y = sym('y','positive'); z = sym('z','rational'); t = sym('t',{'positive','integer'});

Check the assumptions on `x`

, `y`

, `z`

, and
`t`

using `assumptions`

.

assumptions

ans = [ in(x, 'real'), in(z, 'rational'), 1 <= t, 0 < y, in(t, 'integer')]

For further computations, clear the assumptions using `assume`

.

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

ans = Empty sym: 1-by-0

Create a symbolic matrix and set assumptions on each element of that matrix.

A = sym('A%d%d',[2 2],'positive')

A = [ A11, A12] [ A21, A22]

Solve an equation involving the first element of `A`

. MATLAB assumes
that this element is positive.

solve(A(1, 1)^2 - 1, A(1, 1))

ans = 1

Check the assumptions set on the elements of `A`

by
using `assumptions`

.

assumptions(A)

ans = [ 0 < A11, 0 < A12, 0 < A21, 0 < A22]

Clear all previously set assumptions on elements of a symbolic
matrix by using `assume`

.

assume(A,'clear'); assumptions(A)

ans = Empty sym: 1-by-0

Solve the same equation again.

solve(A(1, 1)^2 - 1, A(1, 1))

ans = -1 1

Convert `pi`

to a symbolic
value.

Choose the conversion technique by specifying the optional second
argument, which can be `'r'`

, `'f'`

, `'d'`

,
or `'e'`

. The default is `'r'`

.
See the Input Arguments section for the details about conversion techniques.

r = sym(pi) f = sym(pi,'f') d = sym(pi,'d') e = sym(pi,'e')

r = pi f = 884279719003555/281474976710656 d = 3.1415926535897931159979634685442 e = pi - (198*eps)/359

Statements like

`pi = sym('pi')`

and`delta = sym('1/10')`

create symbolic numbers that avoid the floating-point approximations inherent in the values of`pi`

and`1/10`

. The`pi`

created in this way temporarily replaces the built-in numeric function with the same name.`sym`

always treats`i`

in character vector input as an identifier. To input the imaginary number`i`

, use`1i`

instead.`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 variable.

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,

`A(1,1) = sym('a')`

throws an error.When you use the syntax

`A = sym('a',[n1 ... nM])`

, the`sym`

function assigns only the symbolic array`A`

to the MATLAB workspace. To also assign the automatically generated elements of`A`

, use the`syms`

function instead For example,`syms a [1 3]`

creates the row vector`a = [a1 a2 a3]`

and the symbolic variables`a1`

,`a2`

, and`a3`

in the MATLAB workspace.

To create several symbolic variables in one function call, use `syms`

.
Using `syms`

also clears assumptions from the named
variables.