matlab中的assume函数

Set assumption on symbolic objectcollapse all in page

Syntax


assume(condition)
assume(expr,set)
assume(expr,‘clear’)

Description


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.

Examples


Common Assumptions

Set an assumption using the associated syntax.

Assume ‘x’ isSyntax
实数assume(x,'real')
有理数assume(x,'rational')
正数assume(x > 0)
less than -1 or greater than 1assume(x<-1 | x>1)
2到10之间的整数assume(in(x,'integer') & x>2 & x<10)
not an integerassume(~in(z,'integer'))
not equal to 0assume(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 Variable Is Even or 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.

  • 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.  assume无法累加,而是自动删除先前的假设(x/2,'integer')
assume((x-1)/2,'integer')
solve(x>0,x<10,x)
ans =
 1
 3
 5
 7
 9
  • Clear the assumptions on x for further computations计算.
assume(x,'clear')
Multiple Assumptions

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')
Assumptions on Integrand

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))
Assumptions on Parameters and Variables of Equation

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')
Use Assumptions for Simplification

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')
Assumptions on Expressions

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')
Assumptions to Prove Relations

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
Assumptions on Matrix Elements

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')

Tips

  • 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
    
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43964993

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值