Create symbolic scalar variables and functions, and matrix variables and functions
collapse all in page
Syntax
syms var1 ... varN
syms var1 ... varN [n1 ... nM]
syms var1 ... varN n
syms ___ set
syms f(var1,...,varN)
syms f(var1,...,varN) [n1 ... nM]
syms f(var1,...,varN) n
syms var1 ... varN [nrow ncol] matrix
syms var1 ... varN n matrix
syms f(var1,...,varN) [nrow ncol] matrix
syms f(var1,...,varN) [nrow ncol] matrix keepargs
syms f(var1,...,varN) n matrix
syms f(var1,...,varN) n matrix keepargs
syms(symArray)
syms
S = syms
Description
Symbolic Scalar Variables
example
syms var1 ... varN
creates symbolic scalar variables var1 ... varN
of type sym
. Separate the different variables with spaces. This syntax clears all previous definitions of var1 ... varN
.
example
syms var1 ... varN [n1 ... nM]
creates arrays of symbolic scalar variables var1 ... varN
, where each array has the size n1
-by-...
-by-nM
and contains automatically generated symbolic scalar variables as its elements. For example, syms a [1 3]
creates the symbolic array a = [a1 a2 a3]
and the symbolic scalar 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
.
syms var1 ... varN n
creates n
-by-n
matrices of symbolic scalar variables filled with automatically generated elements.
example
syms ___ set
sets the assumption that the created symbolic scalar variables belong to set
, and clears other assumptions. Here, set
can be real
, positive
, integer
, or rational
. You can also combine multiple assumptions using spaces. For example, syms x positive rational
creates a symbolic scalar variable x
with a positive rational value. Use this option in addition to any of the input argument combinations in previous syntaxes.
Symbolic Scalar Functions
example
syms f(var1,...,varN)
creates the symbolic function f
of type symfun
and the symbolic scalar variables var1,...,varN
of type sym
, which represent the input arguments of f
. This syntax clears all previous definitions of var1,...,varN
including symbolic assumptions. The evaluated symbolic function f(var1,...,varN)
is of type sym
.
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 scalar 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
and f2
, and the symbolic scalar 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
matrix of symbolic functions filled with automatically generated elements.
Symbolic Matrix Variables
example
syms var1 ... varN [nrow ncol] matrix
creates symbolic matrix variables var1 ... varN
of type symmatrix
, where each symbolic matrix variable has the size nrow
-by-ncol
. (since R2021a)
example
syms var1 ... varN n matrix
creates n
-by-n
symbolic matrix variables. (since R2021a)
Symbolic Matrix Functions
syms f(var1,...,varN) [nrow ncol] matrix
creates the symbolic matrix function f
of type symfunmatrix
and the symbolic scalar variables var1,...,varN
of type sym
. The evaluated symbolic matrix function f(var1,...,varN)
is of type symmatrix
and has the size nrow
-by-ncol
. This syntax clears all previous definitions of var1,...,varN
including symbolic assumptions. (since R2022a)
example
syms f(var1,...,varN) [nrow ncol] matrix keepargs
keeps existing definitions of var1,...,varN
in the workspace. If any of the variables var1,...,varN
does not exist in the workspace, then this syntax creates them as symbolic scalar variables of type sym
. The size of the evaluated symbolic matrix function f(var1,...,varN)
is nrow
-by-ncol
. (since R2022a)
syms f(var1,...,varN) n matrix
creates a square symbolic matrix function, where the evaluated symbolic matrix function f(var1,...,varN)
has the size n
-by-n
. This syntax clears all previous definitions of var1,...,varN
including symbolic assumptions. (since R2022a)
syms f(var1,...,varN) n matrix keepargs
keeps existing definitions of var1,...,varN
in the workspace. If any of the variables var1,...,varN
does not exist in the workspace, then this syntax creates them as symbolic scalar variables of type sym
. (since R2022a)
Array of Symbolic Objects
example
syms(symArray)
creates the symbolic scalar variables and functions contained in symArray
, where symArray
is either a vector of symbolic scalar variables or a cell array of symbolic scalar variables and functions. This syntax clears all previous definitions of variables specified in symArray
including symbolic assumptions. Use this syntax only when such an array is returned by another function, such as solve
or symReadSSCVariables
.
Names of Symbolic Objects
example
syms
lists the names of all symbolic scalar variables, functions, matrix variables, matrix functions, and arrays in the MATLAB workspace.
example
S = syms
returns a cell array of the names of all symbolic scalar variables, functions, matrix variables, matrix functions, and arrays.
Examples
collapse all
Create Symbolic Scalar Variables
Open Live Script
Create symbolic scalar variables x
and y
.
syms x yx
x =
y
y =
Create Vector of Symbolic Scalar Variables
Open Live Script
Create a 1-by-4 vector of symbolic scalar variables a
with the automatically generated elements . This command also creates the symbolic scalar variables a1
, ..., a4
in the MATLAB workspace.
syms a [1 4]a
a =
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 scalar 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 =
p_b
p_b =
Create Matrix of Symbolic Scalar Variables
Open Live Script
Create a 3-by-4 matrix of symbolic scalar variables with automatically generated elements. The elements are of the form , which generates the symbolic matrix variables .
syms A [3 4]A
A =
Manage Assumptions for Symbolic Scalar Variables
Open Live Script
Create symbolic scalar variables x
and y
, and assume that they are integers.
syms x y integer
Create another scalar variable z
, and assume that it has a positive rational value.
Check assumptions.
assumptions
ans =
Alternatively, check assumptions on each variable. For example, check assumptions set on the variable x
.
assumptions(x)
ans =
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] realassumptions
ans =
Create and Evaluate Symbolic Functions
Open Live Script
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 a formula for f
.
f(x,y) = x + 2*y
f(x, y) =
Compute the function value at the point x = 1
and y = 2
.
f(1,2)
ans =
Create and Evaluate Symbolic Functions with Matrices as Formulas
Open Live Script
Create a symbolic function and specify its formula by using a matrix of symbolic scalar variables.
syms xM = [x x^3; x^2 x^4];f(x) = M
f(x) =
Compute the function value at the point x = 2
.
f(2)
ans =
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 =
Create and Evaluate Symbolic Matrices as Functions of Two Variables
Open Live Script
Create a 2-by-2 symbolic matrix with automatically generated symbolic functions as its elements.
syms f(x,y) 2f
f(x, y) =
Assign symbolic expressions to the symbolic functions f1_1(x,y)
and f2_2(x,y)
. These functions are displayed as and 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) =
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) =
Evaluate the value of the symbolic matrix A
, which contains the substituted expressions, at x = 2
and y = 3
.
A(2,3)
ans =
Create Symbolic Matrix Variables
Since R2021a
Open Live Script
Create two symbolic matrix variables with size 2
-by-3
. Nonscalar symbolic matrix variables are displayed as bold characters in the Live Editor and Command Window.
syms A B [2 3] matrixA
A =
B
B =
Add the two matrices. The result is represented by the matrix notation .
X = A + B
X =
The data type of X
is symmatrix
.
class(X)
ans = 'symmatrix'
Convert the symbolic matrix variable X
to a matrix of symbolic scalar variables Y
. The result is denoted by the sum of the matrix components.
Y = symmatrix2sym(X)
Y =
The data type of Y
is sym
.
class(Y)
ans = 'sym'
Show that the converted result in Y
is equal to the sum of two matrices of symbolic scalar variables.
syms A B [2 3]Y2 = A + B
Y2 =
isequal(Y,Y2)
ans = logical 1
Commutation Relation of Symbolic Matrix Variables
Since R2021a
Open Live Script
Symbolic matrix variables represent matrices, vectors, and scalars in compact matrix notation. When representing nonscalars, these variables are noncommutative. When mathematical formulas involve matrices and vectors, writing them using symbolic matrix variables is more concise and clear than writing them componentwise.
Create two symbolic matrix variables.
syms A B [2 2] matrix
Check the commutation relation for multiplication between two symbolic matrix variables.
A*B - B*A
ans =
isequal(A*B,B*A)
ans = logical 0
Check the commutation relation for addition between two symbolic matrix variables.
isequal(A+B,B+A)
ans = logical 1
Find Hessian Matrix
Since R2021a
Open Live Script
Create 3
-by-3
and 3
-by-1
symbolic matrix variables.
syms A 3 matrixsyms X [3 1] matrix
Find the Hessian matrix of . Derived equations involving symbolic matrix variables are displayed in typeset as they would be in textbooks.
f = X.'*A*X
f =
H = diff(f,X,X.')
H =
Create Function of Vector and Scalar
Since R2022a
Open Live Script
Create the function , where is a 1-by-3 vector and is a scalar.
Create as a symbolic matrix variable and as a symbolic scalar variable. Create as a symbolic matrix function and keep existing definitions of and in the workspace.
syms r [1 3] matrixsyms tsyms v(r,t) [1 1] matrix keepargs
Assign the formula of .
v(r,t) = norm(r)/t
v(r, t) =
The symbolic matrix function accepts scalars, vectors, and matrices as its input arguments. Evaluate the function for the vector value and the scalar value .
vEval = v([1 2 2],3)
vEval =
Convert the evaluated function from the symmatrix
data type to the sym
data type.
vSym = symmatrix2sym(vEval)
vSym =
Create Symbolic Objects from Returned Symbolic Array
Open Live Script
Certain functions, such as solve
and symReadSSCVariables
, can return a vector of symbolic scalar variables or a cell array of symbolic scalar 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 xeqn = sin(x) == 1;[sol,parameter,condition] = solve(eqn,x,'ReturnConditions',true);parameter
parameter =
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 Objects
Open Live Script
Create some symbolic scalar variables, functions, matrix variables, matrix functions, and arrays.
syms a f(x)syms A [2 1]syms B [2 2] matrixsyms g(B) [2 2] matrix keepargs
Display a list of all symbolic scalar variables, functions, matrix variables, matrix functions, and arrays that currently exist in the MATLAB workspace by using syms
.
syms
Your symbolic variables are:A A1 A2 B a f g x
Instead of displaying a list, return a cell array by providing an output to syms
.
S = syms
S = 8x1 cell {'A' } {'A1'} {'A2'} {'B' } {'a' } {'f' } {'g' } {'x' }
Delete All Symbolic Objects
Open Live Script
Create several symbolic objects.
syms a b c f(x)syms D g(y) [2 2] matrix
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
var1 ... varN
— Symbolic scalar variables, matrices, arrays, or matrix variables
valid variable names separated by spaces
Symbolic scalar variables, matrices, arrays, or matrix variables, specified as valid variable names separated by spaces. That is, each variable name must begin with a letter and can contain only alphanumeric characters and underscores. To verify that a variable name is valid, use isvarname.
Example: x y123 z_1
[n1 ... nM]
— Dimensions of vector, matrix, or array of symbolic scalar variables
vector of nonnegative integers
Dimensions of vector, matrix, or array of symbolic scalar variables, specified as a vector of nonnegative integers.
Example: [2 3]
, [2,3]
n
— Dimensions of square matrix
nonnegative scalar integer
Dimensions of the square matrix, specified as a nonnegative scalar integer. For example, syms x 3 matrix
creates a square 3
-by-3
symbolic matrix variable.
Example: 3
set
— Assumptions on symbolic scalar variables
real
| positive
| integer
| rational
Assumptions on symbolic scalar variables, specified as real
, positive
, integer
, or rational
.
You can combine multiple assumptions using spaces. For example, syms x positive rational
creates a symbolic scalar variable x
with a positive rational value.
Example: rational
f(var1,...,varN)
— Symbolic function or matrix function with its input arguments
function and variable names with parentheses
Symbolic function or matrix function with its input arguments, specified as function and variable names 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 a variable name is valid, use isvarname.
Example: F(r,t)
, f(x,y)
[nrow ncol]
— Dimensions of symbolic matrix variables or functions
vector of nonnegative integers
Dimensions of symbolic matrix variables or functions, specified as a vector of nonnegative integers.
Example: [2 3]
, [2,3]
symArray
— Symbolic scalar variables and functions to create
vector of symbolic scalar variables | cell array of symbolic scalar variables and functions
Symbolic scalar variables and functions to create, specified as a vector of symbolic scalar variables or a cell array of symbolic scalar variables and functions. Such a vector or array is typically the output of another function, such as solve
or symReadSSCVariables
.
Output Arguments
collapse all
S
— Names of all symbolic scalar variables, functions, matrix variables, matrix functions, and arrays
cell array of character vectors
Names of all symbolic scalar variables, functions, matrix variables, matrix functions, and arrays in the MATLAB workspace, returned as a cell array of character vectors.
Limitations
Differentiation functions, such as divergence, curl, jacobian, and laplacian, currently do not accept symbolic matrix variables and symbolic matrix functions as input. To evaluate differentiation with respect to vectors and matrices, use the diff function instead.
To show all the functions in Symbolic Math Toolbox™ that accept symbolic matrix variables and symbolic matrix functions as input, use the command
methods symmatrix
andmethods symfunmatrix
.
Tips
You can create multiple symbolic objects in one call. For example,
syms f(x) g(t) y
creates two symbolic functions (f
andg
) and three symbolic scalar variables (x
,t
, andy
).syms
is a shortcut forsym
,symfun
,symmatrix
, andsymfunmatrix
. This shortcut lets you create several symbolic objects with one function call. For example, you can usesym
and create each symbolic scalar variable separately. However, when you create variables usingsym
, any existing assumptions on the created variables are retained. You can also usesymfun
to create symbolic functions.In functions and scripts, do not use
syms
to create symbolic scalar variables with the same names as MATLAB functions. For these names, MATLAB does not create symbolic scalar variables, but keeps the names assigned to the MATLAB functions. If you want to create a symbolic scalar variable with the same name as a MATLAB function inside a function or a script, use sym instead. For example, usealpha = sym('alpha')
.Avoid using
syms
within functions or parallel for loops because it generates variables without direct output assignment and modifies the global state of the workspace. Usingsyms
within functions can create side effects and other unexpected behaviors. Instead, use sym with left-side output assignment, such ast = sym('t')
. For more details, see Choose syms or sym Function.These variable names are invalid with
syms
:integer
,real
,rational
,positive
, andclear
. To create symbolic scalar variables with these names, usesym
. For example,real = sym('real')
.clear x
does not clear the symbolicobject of its assumptions, such as real, positive, or any assumptionsset byassume
,sym
, orsyms
.To remove assumptions, use one of these options:syms x
orsyms f(x)
clears all assumptions fromx
.assume(x,'clear')
clears all assumptions fromx
.clear all
clears all objects inthe MATLAB workspace and resets the symbolic engine.assume
andassumeAlso
provide more flexibility for setting assumptions on symbolic scalar variables.
When you replace one or more elements of a numericvector or matrix with a symbolic number, MATLAB converts thatnumber 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 scalar 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.When evaluating a symbolic matrix function, you must substitute values that have the same size as the defined input arguments. For example, see Create Function of Vector and Scalar. For comparison, this example returns an error:
syms X [2 2] matrixsyms f(X) [1 1] matrix keepargsf(ones(4))
Version History
Introduced before R2006a
expand all
R2022b: Return the names of all symbolic objects in the MATLAB workspace
You can list the names of all symbolic objects in the MATLAB workspace using syms
and return them as a cell array using S = syms
.
Starting in R2022b, the returned names of all symbolic objects include all symbolic matrix variables and matrix functions of type symmatrix
and symfunmatrix
in the MATLAB workspace. In previous releases, syms
only returns the names of all symbolic scalar variables, functions, and arrays of type sym
and symfun
.
R2022a: Create symbolic matrix functions of type symfunmatrix
Use these syntaxes to create symbolic matrix functions of type symfunmatrix
:
syms f(var1,...,varN) [nrow ncol] matrixsyms f(var1,...,varN) [nrow ncol] matrix keepargssyms f(var1,...,varN) n matrixsyms f(var1,...,varN) n matrix keepargs
Symbolic matrix functions allow you to represent parameter-dependent functions that operate on scalars, vectors, and matrices in compact matrix notation. Using them, you can represent matrix- and vector-based expressions in Symbolic Math Toolbox and perform linear algebra calculations. For example, see Create Function of Vector and Scalar.
R2021b: syms
clears assumptions on symbolic variables when creating symbolic functions
When creating symbolic functions with symbolic variables as input arguments, syms
clears all previously set assumptions on the symbolic variables. For example:
syms x y z realsyms t real positiveassumptions
ans = [in(x, 'real'), in(y, 'real'), in(z, 'real'), 0 < t]
syms f(x,y,z,t)assumptions
ans =Empty sym: 1-by-0
To set assumptions on the input arguments of symbolic functions, create the symbolic functions first, and then set the assumptions on the symbolic variables (or recreate the symbolic variables with set assumptions). For example:
syms g(x,y,z,t)assume(x,'real')assume(t,'positive')assumptions
ans = [in(x, 'real'), 0 < t]
syms y z realassumptions
ans = [in(x, 'real'), in(y, 'real'), in(z, 'real'), 0 < t]
R2021a: Create symbolic matrix variables of type symmatrix
Use these syntaxes to create symbolic matrix variables of type symmatrix
:
syms var1 ... varN [nrow ncol] matrixsyms var1 ... varN n matrix
Symbolic matrix variables offer a concise typeset display and show mathematical formulas with more clarity. Using them, you can represent matrix- and vector-based expressions in Symbolic Math Toolbox and perform linear algebra calculations. For example, see Commutation Relation of Symbolic Matrix Variables and Find Hessian Matrix.
R2018b: syms
clears assumptions on symbolic variables
syms
now clears any assumptions on the symbolic scalar variables that it creates. For example,
syms x realassume(x <= 5);assumptions(x)
ans = x <= 5
syms xassumptions(x)
ans = Empty sym: 1-by-0
To retain existing assumptions on a cleared variable, recreate it using sym instead of syms
. For example,
syms x realassume(x <= 5);clear xx = sym('x');assumptions(x)
ans = x <= 5
R2018b: syms
will no longer support clear
option
The syntaxes syms x clear
and the equivalent syms('x','clear')
now issue a warning that they will be removed in a future release.
In previous releases, both syntaxes cleared all assumptions applied to x
. To update your code, call syms
and specify the symbolic scalar variables whose assumptions you want to clear. For example, syms x
clears all assumptions applied to x
.
See Also
assume | assumeAlso | assumptions | displayFormula | isvarname | sym | symfun | symmatrix | symfunmatrix | symvar
Topics
- Create Symbolic Numbers, Variables, and Expressions
- Create Symbolic Functions
- Create Symbolic Matrices
- Create Symbolic Matrix Variables
- Use Symbolic Objects to Represent Mathematical Objects
- Choose syms or sym Function
- Use Assumptions on Symbolic Variables
- Add Subscripts, Superscripts, and Accents to Symbolic Variables in the Live Editor
- Change Output Format of Symbolic and Variable-Precision Arithmetic
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office