Use Assumptions on Symbolic Variables
You can set mathematical assumptions or conditions on symbolic variables to restrict the domain and range of the variables. This example shows how to set, check, and clear assumptions.
In Symbolic Math Toolbox™, symbolic variables are complex variables by default. For example, if you create
z as a symbolic variable, then Symbolic Math Toolbox assumes that
z is a complex variable.
You can check if a symbolic variable is assumed to be complex by using
z is complex, then
assumptions(z) returns an empty symbolic object.
ans = Empty sym: 1-by-0
To set mathematical assumptions or conditions on symbolic variables, use the
assume function. For example, assume that the variable
x is larger than 2.
syms x assume(x > 2)
assume replaces all previous assumptions on the variable with the new assumption. If you want to add a new assumption to the existing assumptions, then use
assumeAlso. For example, add the assumption that
x is also an integer. Now the variable
x is an integer larger than 2.
assumeAlso let you state that a variable or an expression belongs to these sets: real numbers, rational numbers, integers, and positive numbers. You can also use
assumeAlso to set mathematical conditions on symbolic variables or expressions, such as
x > 2.
Alternatively, you can set assumptions when declaring symbolic variables using
syms, where the assumptions are limited to these sets: real numbers, rational numbers, integers, and positive numbers. For example, create the real symbolic variables
b, and the positive symbolic variable
a = sym("a","real"); b = sym("b","real"); c = sym("c","positive");
You can also use
syms to create these symbolic variables with assumptions.
syms a b real syms c 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 set on the variable
To see all assumptions set on all symbolic variables in the MATLAB® workspace, use
assumptions without input arguments.
Delete Symbolic Objects and Their Assumptions
Symbolic objects and their assumptions are stored separately. When you set an assumption that the symbolic variable
x is 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, the assumption that
x is real stays in the symbolic engine.
syms x assume(x,"real") clear x
If you recreate a variable using
sym, then the existing assumptions apply. If you recreate a variable using
syms, then the existing assumptions on that variable are cleared.
For example, the assumption that
x is real causes the polynomial
x^2 + 1 == 0 to have no roots.
x = sym("x"); solve(x^2 + 1 == 0, x)
ans = Empty sym: 0-by-1
The complex roots of this polynomial do not appear as solutions because the symbolic variable
x still has the assumption that
x is real stored in the symbolic engine. To clear the assumption, recreate the variable using
Solve the same polynomial equation again.
solve(x^2 + 1 == 0, x)
If you want to delete both the symbolic object and its assumption, first clear the assumption on the symbolic object using
assume(x,"clear") and then clear the object using
assume(x,"clear") clear x
For details on clearing symbolic variables, see Clear Assumptions and Reset the Symbolic Engine.