sym
Create symbolic variables, expressions, functions, matrices
Syntax
Description
creates an
A
= sym("a"
,[n1 ... nM]
)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
.
sym(___,
creates a symbolic variable
or array and sets the assumption that the variable or all array elements belong
to set
)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
.
Examples
Input Arguments
Output Arguments
Tips
Statements like
pi = sym(pi)
anddelta = sym("1/10")
create symbolic numbers that avoid the floating-point approximations inherent in the values ofpi
and1/10
. Thepi
created in this way stores the symbolic number in a workspace variable namedpi
, which temporarily replaces the built-in numeric function with the same name. Useclear pi
to restore the floating-point representation ofpi
.sym
always treatsi
in character vector input as an identifier. To input the imaginary numberi
, use1i
instead.clear x
does not clear the symbolic object of its assumptions, such as real, positive, or any assumptions set byassume
,sym
, orsyms
. To remove assumptions, use one of these options:assume(x,"clear")
removes all assumptions affectingx
.clear all
clears all objects in the MATLAB workspace and resets the symbolic engine.assume
andassumeAlso
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])
, thesym
function assigns only the symbolic arrayA
to the MATLAB workspace. To also assign the automatically generated elements ofA
, use thesyms
function instead. For example,syms a [1 3]
creates the row vectora = [a1 a2 a3]
and the symbolic variablesa1
,a2
, anda3
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 R2006aSee Also
assume
| double
| str2sym
| symfun
| syms
| symvar
Topics
- Create Symbolic Numbers, Variables, and Expressions
- Create Symbolic Functions
- Create Symbolic Matrices
- 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