Documentation Center

  • Trial Software
  • Product Updates

sym

Create symbolic objects

Syntax

var = sym('var')
var = sym('var',set)
sym('var','clear')
Num = sym(Num)
Num = sym(Num,flag)
A = sym('A',dim)
A = sym(A,set)
sym(A,'clear')
f(arg1,...,argN) = sym('f(arg1,...,argN)')
fsym = sym(fh)

Description

var = sym('var') creates the symbolic variable var.

var = sym('var',set) creates the symbolic variable var and states that var belongs to set.

sym('var','clear') clears assumptions previously set on the symbolic variable var.

Num = sym(Num) converts a number or a numeric matrix Num to symbolic form.

Num = sym(Num,flag) converts a number or a numeric matrix Num to symbolic form. The second argument specifies the technique for converting floating-point numbers.

A = sym('A',dim) creates a vector or a matrix of symbolic variables.

A = sym(A,set), where A is an existing symbolic vector or matrix, sets an assumption that each element of A belongs to set. To check assumptions set on A, use the assumptions function. This syntax does not create A. To create a symbolic vector or a symbolic matrix A, use A = sym('A',[m n]) or A = sym('A',n).

sym(A,'clear'), where A is an existing symbolic vector or matrix, clears assumptions previously set on elements of A. This syntax does not create A. To create a symbolic vector or a symbolic matrix A, use A = sym('A',[m n]) or A = sym('A',n).

f(arg1,...,argN) = sym('f(arg1,...,argN)') creates the symbolic function f and specifies that arg1,...,argN are the input arguments of f. This syntax does not create symbolic variables arg1,...,argN. The arguments arg1,...,argN must be existing symbolic variables.

fsym = sym(fh) uses the symbolic form of the body of the function handle fh to create the symbolic expression, vector, or matrix fsym.

Input Arguments

var

String that represents the variable name. It must begin with a letter and can contain only alphanumeric characters.

set

Either real or positive.

Num

Number, vector, or matrix of numbers.

flag

One of these strings: r, d, e, or f.

  • r stands for "rational." 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 are converted 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 no simple rational approximation can be found, an expression of the form p*2^q with large integers p and q reproduces the floating-point value exactly. For example, sym(4/3,'r') is '4/3', but sym(1+sqrt(5),'r') is 7286977268806824*2^(-51).

  • d stands for "decimal." The number of digits is taken from the current setting of digits used by vpa. Fewer than 16 digits loses some accuracy, while more than 16 digits might not be warranted. For example, with digits(10), sym(4/3,'d') is 1.333333333, while with digits digits(20), sym(4/3,'d') is 1.3333333333333332593, which does not end in a string of 3s, but is an accurate decimal representation of the floating-point number nearest to 4/3.

  • e stands for "estimate error." The 'r' form is supplemented by a term involving the variable 'eps', which estimates the difference between the theoretical rational expression and its actual floating-point value. For example, sym(3*pi/4,'e') is 3*pi/4*(1+3143276*eps/65).

  • f stands for "floating-point." All values are represented in the form N*2^e or -N*2^e, where N and e are integers, N >= 0. For example, sym(1/10,'f') is 3602879701896397/36028797018963968 .

Default: r

A

String that represents the base for generated names of vector or matrix elements. It must be a valid variable name. (To verify if the name is a valid variable name, use isvarname.)

Default: The generated names of elements of a vector use the form Ak, and the generated names of elements of a matrix use the form Ai_j. The values of k, i, and j range from 1 to m or 1 to n. To specify another form for generated names of matrix elements, use '%d' in the first input. For example, A = sym('A%d%d', [3 3]) generates the 3-by-3 symbolic matrix A with the elements A11, A12, ..., A33.

dim

Integer or vector of two integers specifying dimensions of A. For example, if dim is a vector [m n], then the syntax A = sym('A',[m n]) creates an m-by-n matrix of symbolic variables. If dim is an integer n, then the syntax A = sym('A',n) creates a square n-by-n matrix of symbolic variables.

f

Name of a symbolic function. It must begin with a letter and contain only alphanumeric characters.

arg1,...,argN

Arguments of a symbolic function. Each argument must be an existing symbolic variable.

fh

Function handle.

Output Arguments

var

Symbolic variable.

Num

Symbolic number or vector or matrix of symbolic numbers.

A

Vector or matrix of automatically generated symbolic variables.

f

Symbolic function.

fsym

Symbolic number, variable, expression; vector or matrix of symbolic numbers, variables, expressions.

Examples

Create the symbolic variables x and y:

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

Create the symbolic variables x and y assuming that x is real and y is positive:

x = sym('x','real');
y = sym('y','positive');

Check the assumptions on x and y using assumptions:

assumptions
ans =
[ x in R_, 0 < y]

For further computations, clear the assumptions:

sym('x','clear');
sym('y','clear');
assumptions
ans =
Empty sym: 1-by-0
 

The sym function lets you choose the conversion technique by specifying the optional second argument, which can be 'r', 'f', 'd', or 'e'. The default is 'r'. For example, convert the number 1/3 to a symbolic object:

r = sym(1/3)
f = sym(1/3, 'f')
d = sym(1/3, 'd')
e = sym(1/3, 'e')
r =
1/3
 
f =
6004799503160661/18014398509481984
 
d =
0.3333333333333333148296162562473909929395
 
e =
1/3 - eps/12
 

Create the 3-by-4 symbolic matrix A with the auto-generated 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]

Now create the 4-by-4 matrix B with the elements x_1_1, ..., x_4_4:

B = sym('x_%d_%d', [4 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 define elements of a symbolic matrix as separate symbolic objects. To access an element of a matrix, use parentheses:

A(2, 3)
B (4, 2)
ans =
A2_3
 
ans =
x_4_2
 

You can use symbolic matrices and vectors generated by the sym function to define other matrices:

A = diag(sym('A',[1 4]))
A =
[ A1,  0,  0,  0]
[  0, A2,  0,  0]
[  0,  0, A3,  0]
[  0,  0,  0, A4]

Perform operations on symbolic matrices by using the operators that you use for numeric matrices. For example, find the determinant and the trace of the matrix A:

det(A)
ans =
A1*A2*A3*A4
trace(A)
ans =
A1 + A2 + A3 + A4
 

Use the sym function to set assumptions on each element of a symbolic matrix. You cannot create a symbolic matrix and set an assumption on all its elements in one sym function call. Use two separate sym function calls. The first call creates a matrix, and the second call specifies an assumption:

A = sym('A%d%d', [2 2]);
A = sym(A, 'positive')
A =
[ A11, A12]
[ A21, A22]

Now, MATLAB® assumes that all elements of A are positive:

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

To check the assumptions set on the elements of A, use assumptions:

assumptions(A)
ans =
[ 0 < A21, 0 < A11, 0 < A22, 0 < A12]

To clear all previously set assumptions on elements of a symbolic matrix, also use the sym function:

A = sym(A, 'clear');
assumptions(A)
ans =
Empty sym: 1-by-0
 

Create the symbolic function f whose input arguments are symbolic variables x and y:

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

Alternatively, you can use the assignment operation to create the symbolic function f:

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

Alternatives

  • To create several symbolic variables in one function call, use syms. When using syms, do not enclose variables in quotes and do not use commas between variable names:

    syms var1 var2 var3

    syms also lets you create real variables or positive variables. It also lets you clear assumptions set on a variable.

  • assume and assumeAlso provide more flexibility for setting assumptions on variable.

  • When creating a symbolic function, use sym to create arg1,...,argN as symbolic variables. Then use the assignment operation to create the symbolic function f, for example:

    x = sym('x');
    y = sym('y');
    f(x, y) = x + y
  • syms f(x, y) is equivalent to these commands:

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

More About

expand all

Tips

  • For compatibility with previous versions, sym('var','unreal') is equivalent to sym('var','clear').

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

  • clear x does not clear the symbolic object of its assumptions, such as real, positive, or any assumptions set by assume. To remove assumptions, use one of these options:

    • sym('x','clear') removes assumptions from x without affecting any other symbolic variables.

    • reset(symengine) resets the symbolic engine and therefore removes assumptions on all variables. The variables themselves remain in the MATLAB workspace.

    • clear all clears all objects in the MATLAB workspace and resets the symbolic engine.

See Also

| | | | | | | | | | |

Related Examples

Was this topic helpful?