Create symbolic variables, expressions, functions, matrices
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.
Statements like pi = sym(pi) and delta =
sym("1/10") create symbolic numbers that avoid the floating-point
approximations inherent in the values of pi and
1/10. The pi created in this way
stores the symbolic number in a workspace variable named pi,
which temporarily replaces the built-in numeric function with the same name. Use
clear pi to restore the floating-point representation of
pi.
sym always treats i in
character vector input as an identifier. To input the imaginary number i,
use 1i instead.
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, A(1,1) = sym("a")
throws an error.
When you use the syntax A = sym("a",[n1 ... nM]), the
sym function assigns only the symbolic array
A to the MATLAB workspace. To also assign the
automatically generated elements of A, use the syms function instead. For
example, syms a [1 3] creates the row vector a = [a1
a2 a3] and the symbolic variables a1,
a2, and a3 in the MATLAB
workspace.
To create several symbolic variables in one function call, use syms.
Using syms also clears assumptions from the named
variables.
assume | double | str2sym | symfun | syms | symvar