Main Content

sym

Create symbolic variables, expressions, functions, matrices

Description

example

x = sym('x') creates symbolic scalar 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 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.

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 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'}.

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 specified by num to a symbolic number or symbolic matrix.

example

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

example

sym(strnum) converts the character vector or string specified by strnum to an accurate symbolic number without approximation.

example

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

example

symexpr = sym(M) converts a symbolic matrix variable M of type symmatrix to an array of symbolic scalar variables symexpr of type sym.

Examples

collapse all

Create the symbolic variables x and y.

x = sym('x')
x = x
y = sym('y')
y = 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]
 

You can specify the format for the element names 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',[1 4])
 
b =
 
[x_1, x_2, x_3, x_4]
 

These syntaxes do not create symbolic variables a1, ..., a4, x_1, ..., x_4 in the MATLAB® workspace. Access elements of a and b using standard indexing methods.

a(1)
b(2:3)
 
ans =
 
a1
 
 
ans =
 
[x_2, x_3]
 

Create a 3-by-4 symbolic matrix with automatically generated elements. The sym function generates matrix elements of the form Ai_j. Here, sym 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]
 

These syntaxes do 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,1a1,2,1a2,1,1a2,2,1)

A(:,:,2) = 

(a1,1,2a1,2,2a2,1,2a2,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)
inaccurate1 = 

76502392869235059444732965739290427392

accurate1 = 1/sym(1234567)
accurate1 = 

11234567

inaccurate2 = sym(sqrt(1234567))
inaccurate2 = 

48867165620185894398046511104

accurate2 = sqrt(sym(1234567))
accurate2 = 1234567
inaccurate3 = sym(exp(pi))
inaccurate3 = 

6513525919879993281474976710656

accurate3 = exp(sym(pi))
accurate3 = eπ

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

inaccurateNum = sym(11111111111111111111)
inaccurateNum = 11111111111111110656
accurateNum = sym('11111111111111111111')
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+i

Convert anonymous functions associated with MATLAB® handles to a symbolic expression and a symbolic matrix.

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 = 

(xxxx2x3xx3x6x)

Create the symbolic variables x, y, z, and t while simultaneously setting assumptions that x is real, y is positive, z is rational, and t is a 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 = (tZxRzQ1t0<y)

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 = 

(A11A12A21A22)

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 on the elements of A by using assumptions.

assumptions(A)
ans = (0<A110<A120<A210<A22)

Clear all previously set assumptions on elements of the 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 = 

(-11)

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 details about the conversion techniques.

r = sym(pi)
r = π
f = sym(pi,'f')
f = 

884279719003555281474976710656

d = sym(pi,'d')
d = 3.1415926535897931159979634685442
e = sym(pi,'e')
e = 

π-198eps359

Create 3-by-3 and 3-by-1 symbolic matrix variables.

syms A [3 3] matrix
syms X [3 1] matrix

Find the Hessian matrix of XTAX.

f = X.'*A*X;
M = diff(f,X,X.')
M = AT+A

Convert the result from a symbolic matrix variable to a matrix of symbolic scalar variables.

S = sym(M)
S = 

(2A1,1A1,2+A2,1A1,3+A3,1A1,2+A2,12A2,2A2,3+A3,2A1,3+A3,1A2,3+A3,22A3,3)

Alternatively, you can use symmatrix2sym to convert a symbolic matrix variable to an array of symbolic scalar variables.

S = symmatrix2sym(M)
S = 

(2A1,1A1,2+A2,1A1,3+A3,1A1,2+A2,12A2,2A2,3+A3,2A1,3+A3,1A2,3+A3,22A3,3)

Input Arguments

collapse all

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

Example: 'x', "y123", 'z_1'

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

If you specify the argument a and its vector, matrix, or array dimensions in the argument [n1 ... nM], then a can include a format character vector such as 'a_%d_%d'. For examples, see Create Symbolic Vectors and Create Symbolic Matrices.

Example: 'a', "b", 'a_bc'

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

Example: [2 3]

Assumptions on the symbolic variable or matrix, specified as a character vector, string array, or cell array of character vectors. The available assumptions are 'integer', 'rational', 'real', or 'positive'.

You 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'}.

Example: 'integer'

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

Example: pi

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. For example, sym(1/10,'r') returns 1/10. This mode 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 a 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 10-digit accuracy returns 1.333333333, while with 20-digit accuracy it returns 1.3333333333333332593. The latter does not end in 3s, 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 to rational mode, it returns the symbolic form for all values in the form N*2^e or -N*2^e, where N >= 0 is a nonnegative integer and e is an integer. The returned symbolic number is a precise rational number that is equal to the floating-point value. For example, sym(1/10,'f') returns 3602879701896397/36028797018963968.

Characters representing a symbolic number, specified as a character vector or string.

Example: '1/10'

Anonymous function, specified as a MATLAB function handle. For more information, see Anonymous Functions.

Example: h = @(x)sin(x)

Symbolic matrix variable to convert, specified as a symbolic matrix variable.

Alternatively, you can use symmatrix2sym to convert a symbolic matrix variable to an array of symbolic scalar variables.

Example: syms A 2 matrix; M = A^2 + eye(2)

Data Types: symmatrix

Output Arguments

collapse all

Variable, returned as a symbolic scalar variable.

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

Expression or matrix converted from an anonymous MATLAB function or a symbolic matrix variable, returned as a symbolic expression or matrix of symbolic scalar variables.

Data Types: sym

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 stores the symbolic number in a workspace variable named pi, which temporarily replaces the built-in numeric function with the same name. Use clear pi to restore the floating-point representation of pi.

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

Alternative Functionality

Alternative Approaches for Creating Symbolic Variables

To create several symbolic variables in one function call, use syms. Using syms also clears assumptions from the named variables.

Version History

Introduced before R2006a

expand all