Documentation |
Set assumption on symbolic object
assume(condition) states that condition is valid for all symbolic variables in condition. It also removes any assumptions previously made on these symbolic variables.
Compute this indefinite integral with and without the assumption on the symbolic parameter a.
Use assume to set an assumption that a does not equal -1:
syms x a assume(a ~= -1)
Compute this integral:
int(x^a, x)
ans = x^(a + 1)/(a + 1)
Now, clear the assumption and compute the same integral. Without assumptions, int returns this piecewise result:
syms a clear int(x^a, x)
ans = piecewise([a == -1, log(x)], [a ~= -1, x^(a + 1)/(a + 1)])
To assume the symbolic variable x is even, set the assumption that x/2 is an integer. To assume x is odd, set the assumption that (x-1)/2 is an integer.
Assume x is even.
syms x assume(x/2,'integer')
Find all even numbers between 0 and 10 using solve.
solve(x>0, x<10, x)
ans = 2 4 6 8
Assume x is odd. Find all odd numbers between 0 and 10 using solve.
assume((x-1)/2,'integer') solve(x>0, x<10, x)
ans = 1 3 5 7 9
Clear assumptions on x for further computations.
syms x clear
Use assumptions on the symbolic parameter and variable in the kinematic equation for the free fall motion.
Calculate the time during which the object falls from a certain height by solving the kinematic equation for the free fall motion. If you do not consider the special case where no gravitational forces exist, you can assume that the gravitational acceleration g is positive:
syms g h t assume(g > 0) solve(h == g*t^2/2, t)
ans = (2^(1/2)*h^(1/2))/g^(1/2) -(2^(1/2)*h^(1/2))/g^(1/2)
You can also set assumptions on variables for which you solve an equation. When you set assumptions on such variables, the solver compares obtained solutions with the specified assumptions. This additional task can slow down the solver.
assume(t > 0) solve(h == g*t^2/2, t)
Warning: The solutions are valid under the following conditions: 0 < h. To include parameters and conditions in the solution, specify the 'ReturnConditions' option. > In solve>warnIfParams at 514 In solve at 356 ans = (2^(1/2)*h^(1/2))/g^(1/2)
The solver returns a warning that h must be positive. This follows as the object is above ground.
For further computations, clear the assumptions:
syms g t clear
Use assumption to simplify the sine function.
Simplify this sine function:
syms n simplify(sin(2*n*pi))
ans = sin(2*pi*n)
Suppose n in this expression is an integer. Then you can simplify the expression further using the appropriate assumption:
assume(n,'integer') simplify(sin(2*n*pi))
ans = 0
For further computations, clear the assumption:
syms n clear
Set assumption on the symbolic expression.
You can set assumptions not only on variables, but also on expressions. For example, compute this integral:
syms x int(1/abs(x^2 - 1), x)
ans = -atanh(x)/sign(x^2 - 1)
If you know that x^{2} – 1 > 0, set the appropriate assumption:
assume(x^2 - 1 > 0) int(1/abs(x^2 - 1), x)
ans = -atanh(x)
For further computations, clear the assumption:
syms x clear
Use assumptions to restrict the returned solutions of an equation to a particular interval.
Solve this equation:
syms x solve(x^5 - (565*x^4)/6 - (1159*x^3)/2 - (2311*x^2)/6 + (365*x)/2 + 250/3, x)
ans = -5 -1 -1/3 1/2 100
Use assume to restrict the solutions to the interval –1 <= x <= 1:
assume(-1 <= x <= 1) solve(x^5 - (565*x^4)/6 - (1159*x^3)/2 - (2311*x^2)/6 + (365*x)/2 + 250/3, x)
ans = -1 -1/3 1/2
To set several assumptions simultaneously, use the logical operators and, or, xor, not, or their shortcuts. For example, all negative solutions less than -1 and all positive solutions greater than 1:
assume(x < -1 | x > 1) solve(x^5 - (565*x^4)/6 - (1159*x^3)/2 - (2311*x^2)/6 + (365*x)/2 + 250/3, x)
ans = -5 100
For further computations, clear the assumptions:
syms x clear
Use the assumption on a matrix as a shortcut for setting the same assumption on each matrix element.
Create the 3-by-3 symbolic matrix A with the auto-generated elements:
A = sym('A', [3 3])
A = [ A1_1, A1_2, A1_3] [ A2_1, A2_2, A2_3] [ A3_1, A3_2, A3_3]
Suppose that all elements of this matrix represent rational numbers. Instead of setting an assumption on each element separately, you can set the assumption on the matrix:
assume(A,'rational')
To see the assumptions on the elements of A, use assumptions:
assumptions(A)
ans = [ in(A3_1, 'rational'), in(A2_1, 'rational'), in(A1_1, 'rational'),... in(A3_2, 'rational'), in(A2_2, 'rational'), in(A1_2, 'rational'),... in(A3_3, 'rational'), in(A2_3, 'rational'), in(A1_3, 'rational')]
For further computations, clear the assumptions:
syms A clear