|On this page…|
In Symbolic Math Toolbox™, symbolic variables are complex variables by default. For example, if you declare z as a symbolic variable using
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:
ans = Empty sym: 1-by-0
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:
assume and assumeAlso let you state that a variable or an expression belongs to one of these sets: integers, 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
There are two assumptions that you can assign to a symbolic object within the sym or syms command: real and positive.
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:
To see all assumptions used for all symbolic variables in the MATLAB workspace, use assumptions without input arguments:
For details, see Check Assumptions Set On Variables.
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
the assumption that x is real stays in the symbolic engine. If you declare a new symbolic variable x later, 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. For example, the assumption that x is real causes the polynomial x2 + 1 to have no roots:
syms x real clear x syms x solve(x^2 + 1 == 0, x)
Warning: Explicit solution could not be found. > In solve at 81 ans = [ empty sym ]
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 clear
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 subsequent commands:
To clear the assumption, enter
syms x clear
To delete the symbolic object, enter
For details on clearing symbolic variables, see Clear Assumptions and Reset the Symbolic Engine.