Set assumption on symbolic objectcollapse all in page
assume(condition)
assume(expr,set)
assume(expr,‘clear’)
assume(condition)
states that condition is valid. assume is not additive. Instead, it automatically deletes all previous assumptions on the variables in condition.
assume(expr,set)
states that expr belongs to set. assume deletes previous assumptions on variables in expr.
assume(expr,'clear')
clears all assumptions on all variables in expr.
Set an assumption using the associated syntax.
Assume ‘x’ is | Syntax |
---|---|
實數 | assume(x,'real') |
有理數 | assume(x,'rational') |
正數 | assume(x > 0) |
less than -1 or greater than 1 | assume(x<-1 | x>1) |
2到10之間的整數 | assume(in(x,'integer') & x>2 & x<10) |
not an integer | assume(~in(z,'integer')) |
not equal to 0 | assume(x ~= 0) |
偶數 | assume(x/2,'integer') |
奇數 | assume((x-1)/2,'integer') |
from 0 through 2π | assume(x>0 & x<2*pi) |
a multiple of π | assume(x/pi,'integer') |
real
:實數rational
:有理數integer
:整數multiple
:倍數even
:偶數odd
:奇數Assume x is even by assuming that x/2 is an integer. Assume x is odd by assuming that (x-1)/2 is an integer.
even
.syms x
assume(x/2,'integer')
even numbers
between 0 and 10 using solve.solve(x>0,x<10,x)
ans =
2
4
6
8
odd
. assume無法累加,而是自動刪除先前的假設(x/2,'integer')
。assume((x-1)/2,'integer')
solve(x>0,x<10,x)
ans =
1
3
5
7
9
計算
.assume(x,'clear')
Successive assume
commands do not set multiple assumptions. Instead, each assume
command deletes previous assumptions and sets new assumptions. Set multiple assumptions by using assumeAlso or the & operator.
連續的
assume
命令不會設定多個假設。相反,每個assume
命令都會刪除以前的假設並設定新的假設。使用assumeAlso
或&
運算子可以設定多個假設。
Assume x > 5 and then x < 10 by using assume. Use assumptions to check that only the second assumption exists because assume deleted the first assumption when setting the second.
syms x
assume(x > 5)
assume(x < 10)
assumptions
ans =
x < 10
Assume the first assumption in addition to the second by using assumeAlso. Check that both assumptions exist.
assumeAlso(x > 5)
assumptions
ans =
[ 5 < x, x < 10]
Clear the assumptions on x.
assume(x,'clear')
Assume both conditions using the & operator. Check that both assumptions exist.
assume(x>5 & x<10)
assumptions
ans =
[ 5 < x, x < 10]
Clear the assumptions on x for future calculations.
assume(x,'clear')
Compute an 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.
assume(a,'clear')
int(x^a, x)
ans =
piecewise(a == -1, log(x), a ~= -1, x^(a + 1)/(a + 1))
Use assumptions to restrict the returned solutions of an equation to a particular interval.
Solve this equation.
syms x
eqn = x^5 - (565*x^4)/6 - (1159*x^3)/2 - (2311*x^2)/6 + (365*x)/2 + 250/3;
solve(eqn, 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(eqn, x)
ans =
-1
-1/3
1/2
Set several assumptions simultaneously by using 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(eqn, x)
ans =
-5
100
For further computations, clear the assumptions.
assume(x,'clear')
Setting appropriate assumptions can result in simpler expressions.
Try to simplify the expression sin(2pin) using simplify. The simplify function cannot simplify the input and returns the input as it is.
syms n
simplify(sin(2*n*pi))
ans =
sin(2*pi*n)
Assume n is an integer. simplify now simplifies the expression.
assume(n,'integer')
simplify(sin(2*n*pi))
ans =
0
For further computations, clear the assumption.
assume(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
f = 1/abs(x^2 - 1);
int(f,x)
ans =
-atanh(x)/sign(x^2 - 1)
Set the assumption x2 – 1 > 0 to produce a simpler result.
assume(x^2 - 1 > 0)
int(f,x)
ans =
-atanh(x)
For further computations, clear the assumption.
assume(x,'clear')
Prove relations that hold under certain conditions by first assuming the conditions and then using isAlways.
Prove that sin(pi*x) is never equal to 0 when x is not an integer. The isAlways function returns logical 1 (true), which means the condition holds for all values of x under the set assumptions.
syms x
assume(~in(x,'integer'))
isAlways(sin(pi*x) ~= 0)
ans =
logical
1
Set assumptions on all elements of a matrix using sym.
Create the 3-by-3 symbolic matrix A with auto-generated elements. Specify the set as rational.
A = sym('A',[3 3],'rational')
A =
[ A1_1, A1_2, A1_3]
[ A2_1, A2_2, A2_3]
[ A3_1, A3_2, A3_3]
Return the assumptions on the elements of A using assumptions.
assumptions(A)
ans =
[ in(A1_1, 'rational'), in(A1_2, 'rational'), in(A1_3, 'rational'),...
in(A2_1, 'rational'), in(A2_2, 'rational'), in(A2_3, 'rational'),...
in(A3_1, 'rational'), in(A3_2, 'rational'), in(A3_3, 'rational')]
You can also use assume to set assumptions on all elements of a matrix. Assume all elements of A are positive using assume.
assume(A,'positive')
For further computations, clear the assumptions.
assume(A,'clear')
assume removes any assumptions previously set on the symbolic variables. To retain previous assumptions while adding an assumption, use assumeAlso
.
When you delete a symbolic variable from the MATLAB® workspace using clear, all assumptions that you set on that variable remain in the symbolic engine. If you later declare a new symbolic variable with the same name, it inherits these assumptions.
To clear all assumptions set on a symbolic variable var, use this command.
assume(var,'clear')
To delete all objects in the MATLAB workspace and close the Symbolic Math Toolbox™ engine associated with the MATLAB workspace clearing all assumptions, use this command:
clear all
MATLAB projects complex numbers in inequalities to the real axis. If condition is an inequality, then both sides of the inequality must represent real values. Inequalities with complex numbers are invalid because the field of complex numbers is not an ordered field. (It is impossible to tell whether 5 + i
is greater or less than 2 + 3*i
.) For example, x > i
becomes x > 0
, and x <= 3 + 2*i
becomes x <= 3
.
The toolbox does not support assumptions on symbolic functions. Make assumptions on symbolic variables and expressions instead.
When you create a new symbolic variable using sym and syms, you also can set an assumption that the variable is real, positive, integer, or rational.
a = sym('a','real');
b = sym('b','integer');
c = sym('c','positive');
d = sym('d','positive');
e = sym('e','rational');
or more efficiently:
syms a real
syms b integer
syms c d positive
syms e rational