Use Assumptions on Symbolic Variables
Default Assumption
In Symbolic Math Toolbox™, symbolic variables are complex variables by default. For example, if
you declare z
as a symbolic variable using
syms z
then MATLAB® assumes that z
is a complex variable. You can
always check if a symbolic variable is assumed to be complex or real by using
assumptions
. If z
is complex, assumptions(z)
returns an empty symbolic
object:
assumptions(z)
ans = Empty sym: 1-by-0
Set Assumptions
To set an assumption on a symbolic variable, use the assume
function. For example, assume
that the variable x
is nonnegative:
syms x assume(x >= 0)
assume
replaces all previous assumptions on the variable with
the new assumption. If you want to add a new assumption to the existing assumptions,
use assumeAlso
. For example, add the assumption that
x
is also an integer. Now the variable x
is a nonnegative integer:
assumeAlso(x,'integer')
assume
and assumeAlso
let you state that a
variable or an expression belongs to one of these sets: integers, positive numbers,
rational numbers, and real numbers.
Alternatively, you can set an assumption while declaring a symbolic variable using
sym
or syms
. For example, create the real
symbolic variables a
and b
, and the positive
symbolic variable c
:
a = sym('a', 'real'); b = sym('b', 'real'); c = sym('c', 'positive');
or more efficiently:
syms a b real syms c positive
The assumptions that you can assign to a symbolic object with
sym
or syms
are real, rational, integer
and positive.
Check Existing Assumptions
To see all assumptions set on a symbolic variable, use the assumptions
function with the name
of the variable as an input argument. For example, this command returns the
assumptions currently used for the variable x
:
assumptions(x)
To see all assumptions used for all symbolic variables in the MATLAB workspace, use assumptions
without input
arguments:
assumptions
For details, see Check Assumptions Set on Variables.
Delete Symbolic Objects and Their Assumptions
Symbolic objects and their assumptions are stored separately. When you set an
assumption that x
is real using
syms x assume(x,'real')
you actually create a symbolic object x
and the assumption that
the object is real. The object is stored in the MATLAB workspace, and the assumption is stored in the symbolic engine. When
you delete a symbolic object from the MATLAB workspace using
clear x
the assumption that x
is real stays in the symbolic engine. If
you declare a new symbolic variable x
later using
sym
, it inherits the assumption that x
is
real instead of getting a default assumption. If later you solve an equation and
simplify an expression with the symbolic variable x
, you could
get incomplete results.
Note
If you declare a variable using syms
, existing assumptions
are cleared. If you declare a variable using sym
, existing
assumptions are not cleared.
For example, the assumption that x
is real causes the
polynomial x
2 + 1 to have no
roots:
syms x real clear x x = sym('x'); solve(x^2 + 1 == 0, x)
ans = Empty sym: 0-by-1
The complex roots of this polynomial disappear because the symbolic variable
x
still has the assumption that x
is real
stored in the symbolic engine. To clear the assumption, enter
syms x
After you clear the assumption, the symbolic object stays in the MATLAB workspace. If you want to remove both the symbolic object and its assumption, use two commands:
To clear the assumption, enter
syms x
To delete the symbolic object, enter
clear x
For details on clearing symbolic variables, see Clear Assumptions and Reset the Symbolic Engine.