Documentation Center

  • Trial Software
  • Product Updates

syms

Shortcut for creating symbolic variables and functions

Syntax

syms var1 ... varN
syms var1 ... varN set
syms var1 ... varN clear
syms f(arg1,...,argN)

Description

syms var1 ... varN creates symbolic variables var1 ... varN.

syms var1 ... varN set creates symbolic variables var1 ... varN and states that these variables belong to set.

syms var1 ... varN clear removes assumptions previously set on symbolic variables var1 ... varN.

syms f(arg1,...,argN) creates the symbolic function f and symbolic variables arg1,...,argN representing the input arguments of f.

Input Arguments

var1 ... varN

Names of symbolic variables. Each name must begin with a letter and contain only alphanumeric characters.

set

Either real or positive.

f

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

arg1,...,argN

Arguments of a symbolic function. For each argument, syms creates a symbolic variable. Argument names must begin with a letter and contain only alphanumeric characters.

Examples

Create symbolic variables x and y using syms:

syms x y
 

Create symbolic variables x and y, and assume that they are real:

syms x y real

To see assumptions set on x and y, use assumptions:

assumptions(x)
assumptions(y)
ans =
x in R_
 
ans =
y in R_

Clear the assumptions that x and y are real:

syms x y clear
assumptions
ans =
Empty sym: 1-by-0
 

Create a symbolic function f that accepts two arguments, x and y:

syms f(x, y)

Specify the formula for this function:

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

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

f(1, 2)
ans =
5
 

Create symbolic function f and specify its formula by this symbolic matrix:

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

Compute the function value at the point x = 2:

f(2)
ans =
[ 2,  4]
[ 8, 16]

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

y = f([1 2; 3 4])
y = 
    [2x2 sym]    [2x2 sym]
    [2x2 sym]    [2x2 sym]

To access the contents of each cell in a cell array, use braces:

y{1}
ans =
[ 1, 2]
[ 3, 4]
y{2}
ans =
[  1,  8]
[ 27, 64]
y{3}
ans =
[ 1,  4]
[ 9, 16]
y{4}
ans =
[  1,  16]
[ 81, 256]

Alternatives

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

    var1 = sym('var1');
    ...
    varN = sym('varN');

    sym 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 syms to create arg1,...,argN as symbolic variables. Then use the assignment operation to create the symbolic function f, for example:

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

More About

expand all

Tips

  • For compatibility with previous versions, syms var1 ... varN unreal is equivalent to syms var1 ... varN clear.

  • 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 some MATLAB function inside a function or a script, use sym. For example:

    alpha = sym('alpha')
  • 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:

    • syms 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 removes all objects in the MATLAB workspace and resets the symbolic engine.

See Also

| | | | | | |

Was this topic helpful?