Documentation

This is machine translation

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

syms

Create symbolic variables and functions

Syntax

Description

example

syms var1 ... varN creates symbolic variables var1 ... varN. Separate variables by spaces.

example

syms var1 ... varN set sets an assumption that the created symbolic variables belong to a set.

example

syms var1 ... varN clear clears assumptions set on a symbolic variables var1 ... varN.

example

syms f(var1,...,varN) creates the symbolic function f and symbolic variables var1,...,varN representing 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(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 without input arguments lists all symbolic variables, functions, vectors, and matrices currently existing in the MATLAB® workspace.

Examples

Create Symbolic Variables

Create symbolic variables x and y.

syms x y

Set Assumptions While Creating Variables

Create symbolic variables x and y, and assume that they are integers.

syms x y integer

Check assumptions.

assumptions
ans =
[ in(x, 'integer'), in(y, 'integer')]

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

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

Clear assumptions on x and y.

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

Create Symbolic Functions

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

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

f(1,2)
ans =
5

Create Symbolic Functions with Matrices as Formulas

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

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

Compute the function value at the point x = 2:

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

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

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

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

y{1}
ans =
[ 1, 2, 3]
[ 4, 5, 6]

Create Objects from Array of Symbolic Variables and Functions

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

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.

List All Symbolic Variables, Functions, and Matrices

Create several symbolic variables, functions, and matrices.

syms a b c f(x,y) g(s,t)
A = sym('A',[2,3]);
B = sym('B',[1 10]);

Use syms without input arguments to print a list of all symbolic objects that currently exist in the MATLAB workspace.

syms
    'A'    'B'    'a'    'b'    'c'    'f'    'g'    's'    't'    'x'    'y'

Related Examples

Input Arguments

collapse all

Symbolic variables, 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

Assumptions on a symbolic variable or matrix, specified as real, positive, integer, or 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.

More About

collapse all

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. You also can 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. 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:

    • 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, syms a; A(1,1) = a throws an error.

Introduced before R2006a

Was this topic helpful?