Create symbolic variables, expressions, functions, matrices
sym('pi')
now creates a symbolic variable named
pi
instead of a symbolic number representing the mathematical
constant π. For more information, see Compatibility Considerations.
Support of character vectors that are not valid variable names and that do not define a number
has been removed. To create symbolic expressions, first create symbolic variables, and
then use operations on them. For example, use syms x; x + 1
instead
of sym('x + 1')
, exp(sym(pi))
instead of
sym('exp(pi)')
, and syms f(var1,...varN)
instead of f(var1,...varN) = sym('f(var1,...varN)')
.
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 a 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(
converts a number or numeric matrix
specified by num
)num
to a symbolic number or symbolic
matrix.
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 variable.
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.