syms

Create symbolic variables and functions

Description

example

syms var1 ... varN creates symbolic variables var1 ... varN. Separate different variables by spaces. syms clears all assumptions from the variables.

example

syms var1 ... varN [n1 ... nM] creates symbolic arrays var1 ... varN, where each array has the size n1-by-...-by-nM and contains automatically generated symbolic variables as its elements. For example, syms a [1 3] creates the symbolic array a = [a1 a2 a3] and the symbolic variables a1, a2, and a3 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

syms var1 ... varN n creates n-by-n symbolic matrices filled with automatically generated elements.

example

syms ___ set sets the assumption that the created symbolic variables belong to set, and clears other assumptions. Here, set can be real, positive, integer, or rational. You also can combine multiple assumptions using spaces. For example, syms x positive rational creates a variable x with a positive rational value.

example

syms f(var1,...,varN) creates the symbolic function f and the symbolic variables var1,...,varN, which represent 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).

example

syms f(var1,...,varN) [n1 ... nM] creates an n1-by-...-by-nM symbolic array with automatically generated symbolic functions as its elements. This syntax also generates the symbolic variables var1,...,varN that represent the input arguments of f. For example, syms f(x) [1 2] creates the symbolic array f(x) = [f1(x) f2(x)], the symbolic functions f1(x) and f2(x), and the symbolic variable x in the MATLAB workspace. For multidimensional arrays, these elements have the prefix f followed by the element’s index using _ as a delimiter, such as f1_3_2.

example

syms f(var1,...,varN) n creates an n-by-n symbolic matrix filled with automatically generated elements.

example

syms(symArray) creates the symbolic variables and functions contained in 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.

example

syms lists the names of all symbolic variables, functions, and arrays in the MATLAB workspace.

example

S = syms returns a cell array of the names of all symbolic variables, functions, and arrays.

Examples

collapse all

Create symbolic variables x and y.

syms x y
x
x = xx
y
y = yy

Create a 1-by-4 symbolic vector a with the automatically generated elements a1,,a4. This command also creates the symbolic variables a1, ..., a4 in the MATLAB workspace.

syms a [1 4]
a
a = (a1a2a3a4)[a1, a2, a3, a4]
whos
  Name      Size            Bytes  Class    Attributes

  a         1x4                 8  sym                
  a1        1x1                 8  sym                
  a2        1x1                 8  sym                
  a3        1x1                 8  sym                
  a4        1x1                 8  sym                

You can change the naming format of the generated elements by using a format character vector. Declare the symbolic variables by enclosing each variable name in single quotes. syms replaces %d in the format character vector with the index of the element to generate the element names.

syms 'p_a%d' 'p_b%d' [1 4]
p_a
p_a = (pa1pa2pa3pa4)[p_a1, p_a2, p_a3, p_a4]
p_b
p_b = (pb1pb2pb3pb4)[p_b1, p_b2, p_b3, p_b4]

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.

syms A [3 4]
A
A = 

(A1,1A1,2A1,3A1,4A2,1A2,2A2,3A2,4A3,1A3,2A3,3A3,4)[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 symbolic variables x and y, and assume that they are integers.

syms x y integer

Create another variable z, and assume that it has a positive rational value.

syms z positive rational

Check assumptions.

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

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

assumptions(x)
ans = xZin(x, 'integer')

Clear assumptions on x, y, and z.

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

Create a 1-by-3 symbolic array a and assume that the array elements have real values.

syms a [1 3] real
assumptions
ans = (a1Ra2Ra3R)[in(a1, 'real'), in(a2, 'real'), in(a3, 'real')]

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+2yx + 2*y

Compute the function value at the point x = 1 and y = 2.

f(1,2)
ans = 5sym(5)

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

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

(xx3x2x4)[x, x^3; x^2, x^4]

Compute the function value at the point x = 2:

f(2)
ans = 

(28416)[sym(2), sym(8); sym(4), sym(16)]

Compute the value of this function for x = [1 2 3; 4 5 6]. The result is a cell array of symbolic matrices.

xVal = [1 2 3; 4 5 6];
y = f(xVal)
y=2×2 cell array
    {2x3 sym}    {2x3 sym}
    {2x3 sym}    {2x3 sym}

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

y{1}
ans = 

(123456)[sym(1), sym(2), sym(3); sym(4), sym(5), sym(6)]

Create a 2-by-2 symbolic matrix with automatically generated symbolic functions as its elements.

syms f(x,y) 2
f
f(x, y) = 

(f1,1(x,y)f1,2(x,y)f2,1(x,y)f2,2(x,y))[f1_1(x, y), f1_2(x, y); f2_1(x, y), f2_2(x, y)]

Assign symbolic expressions to the symbolic functions f1_1(x,y) and f2_2(x,y). These functions are displayed as f1,1(x,y) and f2,2(x,y) in the Live Editor. When you assign these expressions, the symbolic matrix f still contains the initial symbolic functions in its elements.

f1_1(x,y) = 2*x;
f2_2(x,y) = x - y;
f
f(x, y) = 

(f1,1(x,y)f1,2(x,y)f2,1(x,y)f2,2(x,y))[f1_1(x, y), f1_2(x, y); f2_1(x, y), f2_2(x, y)]

Substitute the expressions assigned to f1_1(x,y) and f2_2(x,y) by using the subs function.

A = subs(f)
A(x, y) = 

(2xf1,2(x,y)f2,1(x,y)x-y)[2*x, f1_2(x, y); f2_1(x, y), x - y]

Evaluate the value of the symbolic matrix A, which contains the substituted expressions at x = 2 and y = 3.

A(2,3)
ans = 

(4f1,2(2,3)f2,1(2,3)-1)[sym(4), f1_2(2, 3); f2_1(2, 3), -sym(1)]

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
eqn = sin(x) == 1;
[sol, parameter, condition] = solve(eqn, x, 'ReturnConditions', true);
parameter
parameter = kk

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)
syms A [2 2]

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

syms
Your symbolic variables are:

A     A1_1  A1_2  A2_1  A2_2  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 = 8x1 cell
    {'A'   }
    {'A1_1'}
    {'A1_2'}
    {'A2_1'}
    {'A2_2'}
    {'a'   }
    {'f'   }
    {'x'   }

Create several symbolic objects.

syms a b c f(x)

Return all symbolic objects as a cell array by using the syms function. Use the cellfun function to delete all symbolic objects in the cell array symObj.

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

Input Arguments

collapse all

Symbolic variables, matrices, or arrays, specified as valid variable names separated by spaces. Each variable name 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

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, syms x 3 creates a square 3-by-3 matrix.

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

Assumptions on a symbolic variable or matrix, specified as real, positive, integer, or rational.

You can combine multiple assumptions using spaces. For example, syms x positive rational creates a variable x with a positive rational value.

Example: rational

Symbolic function with its input arguments, specified as an expression with parentheses. The function name f and the variable names var1...varN must be valid variable names. That is, they 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: s(t), f(x,y)

Symbolic variables or functions, specified as a vector of symbolic variables or a cell array of symbolic variables and functions. Such a vector or array is typically the output of another function, such as solve or symReadSSCVariables.

Output Arguments

collapse all

Names of all symbolic variables, functions, and arrays in the MATLAB workspace, returned as a cell array of character vectors.

Tips

  • 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. However, when you create variables using sym, any existing assumptions on the created variables are retained. You can also 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 instead. 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:

    • syms x clears all assumptions from x.

    • assume(x,'clear') clears all assumptions from 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.

Compatibility Considerations

expand all

Behavior changed in R2018b

Warns starting in R2018b

Introduced before R2006a