# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# sym

Create symbolic variables, expressions, functions, matrices

Support of character vectors that are not valid variable names and do not define a number will be removed in a future release. 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)')`.

## Syntax

``x = sym('x')``
``````A = sym('a', [n1 ... nM])``````
``````A = sym('a', n)``````
``sym(___, set)``
``sym(___, 'clear')``
``sym(num)``
``sym(num, flag)``
``symexpr = sym(h)``

## Description

example

````x = sym('x')` creates symbolic variable `x`.```

example

``````A = sym('a', [n1 ... nM])``` creates an `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 auto-generated elements do not appear in the MATLAB® workspace. For arrays, these elements have the prefix `a` followed by the element’s index using `_` as a delimiter, such as `a1_3_2`.```

example

``````A = sym('a', n)``` creates an `n`-by-`n` symbolic matrix filled with automatically generated elements.```

example

````sym(___, set)` creates a symbolic variable or array and sets the assumption that the variable or all array elements belong to a `set`. Here, `set` can be `'real'`, `'positive'`, `'integer'`, or `'rational'`.```

example

````sym(___, 'clear')` clears assumptions set on a symbolic variable or array. You can specify `'clear'` after the input arguments in any of the previous syntaxes, except combining `'clear'` and `set`. You cannot set and clear an assumption in the same function call to `sym`.```

example

````sym(num)` converts a number or numeric matrix to a symbolic number or symbolic matrix.```

example

````sym(num, flag)` uses the technique specified by `flag` for converting floating-point numbers to symbolic numbers.```

example

````symexpr = sym(h)` creates a symbolic expression or matrix `symexpr` from an anonymous MATLAB function associated with the function handle `h`.```

## Examples

### Create Symbolic Variables

Create the symbolic variables `x` and `y`.

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

### Create Symbolic Vector

Create a 1-by-4 symbolic vector `a` with auto-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 Symbolic Matrices

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 Symbolic Multidimensional Arrays

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

### Create Symbolic Numbers

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

### Create Large Symbolic Numbers

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 Symbolic Expressions from Function Handles

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

### Set Assumptions While Creating Variables

Create the symbolic variables `x`, `y`, `z`, and `t` simultaneously assuming that `x` is real, `y` is positive, `z` integer, and `t` is rational.

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

Check the assumptions on `x`, `y`, and `z` using `assumptions`.

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

For further computations, clear the assumptions using `assume`.

```assume([x y z t],'clear') assumptions```
```ans = Empty sym: 1-by-0```

### Set Assumptions on Matrix Elements

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

### Choose Conversion Technique for Floating-Point Values

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

## Input Arguments

collapse all

Variable name, specified as a character vector. Argument `x` must a valid variable name. That is, `x` must begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use `isvarname`.

Example: `x`, `y123`, `z_1`

Anonymous function, specified as a MATLAB function handle

Example: `h = @(x)sin(x); symexpr = sym(h)`

Prefix for automatically generated matrix elements, specified as a character vector. Argument `a` must a valid variable name. That is, `a` must begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use `isvarname`.

Example: `a`, `b`, `a_bc`

Vector, matrix, or array dimensions, specified as a vector of integers. As a shortcut, you also can use one integer to create a square matrix. For example, `A = sym('A',3)` creates a square `3`-by-`3` matrix.

Example: `[2 3]`, `[2,3]`, `[2;3]`

Assumptions on a symbolic variable or matrix, specified as one of these character vectors: `'real'`, `'positive'`, `'integer'`, or `'rational'`.

Numeric value to be converted to symbolic number or matrix, specified as a number or a matrix of numbers.

Example: `10`, `pi`,`hilb(3)`

Conversion technique, specified as one of the characters listed in this table.

 `'r'` When `sym` uses the rational mode, it converts floating-point numbers obtained by evaluating expressions of the form `p/q`, `p*pi/q`, `sqrt(p)`, `2^q`, and `10^q` for modest sized integers `p` and `q` to the corresponding symbolic form. This effectively compensates for the round-off error involved in the original evaluation, but might not represent the floating-point value precisely. If `sym` cannot find simple rational approximation, then it uses the same technique as it would use with the flag `'f'`. `'d'` When `sym` uses the decimal mode, it takes the number of digits from the current setting of `digits`. Conversions with fewer than 16 digits lose some accuracy, while more than 16 digits might not be warranted. For example, `sym(4/3,'d')` with the 10-digit accuracy returns `1.333333333`, while with the 20-digit accuracy it returns `1.3333333333333332593`. The latter does not end in `3`s, but it is an accurate decimal representation of the floating-point number nearest to `4/3`. `'e'` When `sym` uses the estimate error mode, it supplements a result obtained in the rational mode by a term involving the variable `eps`. This term estimates the difference between the theoretical rational expression and its actual floating-point value. For example, `sym(3*pi/4,'e')` returns ```(3*pi)/4 - (103*eps)/249```. `'f'` When `sym` uses the floating-point mode, it represents all values in the form `N*2^e` or `-N*2^e`, where `N >= 0` and `e` are integers. For example, `sym(1/10,'f')` returns `3602879701896397/36028797018963968` . The returned rational value is the exact value of the floating-point number that you convert to a symbolic number.

## Output Arguments

collapse all

Variable, returned as a symbolic variable.

Expression or matrix generated from an anonymous MATLAB function, returned as a symbolic expression or matrix.

Vector or matrix with automatically generated elements, returned as a symbolic vector or matrix. The elements of this vector or matrix do not appear in the MATLAB workspace.

## Tips

• 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.

## Alternative Functionality

### Alternative Approaches for Creating Symbolic Variables

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