|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 thinkfunny 于 2021-1-22 10:20 编辑 * a2 p( i* T+ @2 A
$ ?0 v7 u# a$ e. k/ {- e
Assume
# `- c( l' V i+ \Syntax
+ [; v4 P. ?) C3 j- eDescription
$ }* G0 u+ ?. s0 PExamples( s) I) Z* R( ^9 z
Common Assumptions
: f. o/ K5 G) \' @Assume Variable Is Even or Odd1 v& W/ i( k: T! E
Multiple Assumptions
, c p8 \6 z C- x6 G% z2 `Assumptions on Integrand/ h2 c# v. Y. s* l% K- y
Assumptions on Parameters and Variables of Equation& m2 K1 [2 R" t# v K$ ^! q- B
Use Assumptions for Simplification
6 W, `. s6 q9 x6 z: rAssumptions on Expressions z8 P6 w3 P* K; u5 Q+ y/ [
Assumptions to Prove Relations
$ @, I$ ]5 {6 c T3 v ~ S$ bAssumptions on Matrix Elements. c6 t+ b2 e6 p- k
Tips
/ A: ]; g t4 |% ] v4 XSet assumption on symbolic objectcollapse all in page# _) |. T1 i# z8 L6 Q+ p# C6 I
; w3 S, q; a# N* K0 s
7 Q2 ]* ~7 W0 ^/ N! w+ ySyntax. c& A; j+ K) ^% C0 |
assume(condition)
- ^+ J* J. H$ h1 ^' M1 ?; qassume(expr,set)
3 q7 s8 s, S, ]6 Cassume(expr,‘clear’) y7 [ s+ F# a
/ r) m5 _; ~2 y7 `5 Y
5 d9 y. d! J! W$ J% u) wDescription
& m; |% s6 R5 i D' V* ^! jassume(condition) states that condition is valid. assume is not additive. Instead, it automatically deletes all previous assumptions on the variables in condition.
0 L9 E5 l- ^8 s+ p# Y8 j# L7 G$ r
assume(expr,set) states that expr belongs to set. assume deletes previous assumptions on variables in expr.
) E" ~" C0 Q' |8 Z; Z7 f
) H$ ^) j W$ D5 R& U8 R, _assume(expr,'clear') clears all assumptions on all variables in expr." |" U& E7 t7 s$ o8 b
- e: g8 @4 h; u( |( u# C. s/ q% d% U
2 r. E* g0 h. Z8 r0 L+ _Examples0 e" w& @( ~- I
Common Assumptions* H# Z, `& E j, T
Set an assumption using the associated syntax.
1 {( [7 {% q, }7 X- b% {+ Q9 y* \% f4 z) y
/ S( v/ `9 `; Y7 H
8 _- {0 ]! p q! _9 R# Q
% v/ h2 |4 {" x1 j. r2 z7 I. ]
" r5 S) l9 C5 O3 v# G- real:实数
- rational:有理数
- integer:整数
- multiple:倍数
- even:偶数
- odd:奇数
& Q0 s4 z- n# Z( t) N5 A4 E; _& t : d* f: j, Z0 `& h+ K& a
$ V8 t1 ?3 ^! NAssume Variable Is Even or Odd1 V& a( }+ |3 t v
7 Z" S' o/ h; H. q6 G/ d$ |
$ C z1 N f( u4 I# ^' lAssume x is even by assuming that x/2 is an integer. Assume x is odd by assuming that (x-1)/2 is an integer.4 {" T9 p* ]/ l; {, [% M- {8 d
* _ F* _& E9 @# C9 l: q; X V9 y2 x
- Assume x is even.7 p1 e9 `! U: j- X
- syms x
- assume(x/2,'integer')
. G! Q. j; G2 V) D1 U5 [0 K3 a
. c& j" p* s0 G4 M" j
_, ^' O! f$ R" S1 H' |4 Y9 E- Find all even numbers between 0 and 10 using solve.- a1 F' H- v- T1 `
- solve(x>0,x<10,x)' q. Q* f! \ v
6 L" \" `- @8 {# k& G- c
7 h5 Z8 ?, M" g c x- ans =
- 2
- 4
- 6
- 8
) H, s' B8 i, D# b) b2 s5 F' A
# m2 s7 `6 n8 H8 O! n& d1 f- u9 ? K
- Assume x is odd. assume无法累加,而是自动删除先前的假设(x/2,'integer')。
; a5 X5 |+ |9 ]1 s8 N4 Q2 v/ {
- assume((x-1)/2,'integer')
- solve(x>0,x<10,x)
1 i0 f% g' _( j m* t* F
4 h6 D4 P4 ]4 G2 `
+ L$ p/ u% r0 f& u2 i5 O- ans =
- 1
- 3
- 5
- 7
- 9
: z/ j& U1 ?: N2 g8 h0 t. C. c
, ]4 l$ h: q3 ?( y) P
# y+ p7 \6 y) w1 [( ~- ^- Clear the assumptions on x for further computations计算.
0 ]9 @1 a( e S" u' N+ D
- assume(x,'clear')
3 `; [6 n2 _! A# ^! [1 N1 |
% j5 g7 V' l1 B8 v- q# N
7 F1 r; K( k2 xMultiple Assumptions* h' |8 q( s1 `6 ~" U! o
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.6 P& _: o0 t p3 ~! z
8 U& z% N1 K+ _( u7 D& l' X5 {% O
连续的assume命令不会设置多个假设。相反,每个assume命令都会删除以前的假设并设置新的假设。使用assumeAlso或&运算符可以设置多个假设。1 m4 T4 }; q4 j2 m- f5 H/ {. i
8 E5 Y4 j) b0 Y& c, K# a
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.
: s* T/ |( y" R" y8 ?! V, f5 M
7 ?" U! b. g. a! u% x/ X- syms x
- assume(x > 5)
- assume(x < 10)
- assumptions" S+ [, B& g' j' D% w8 ]* S
6 A# e6 @1 I. Q6 M! B* q! |: P1 k9 J# H/ s
- ans =
- x < 108 J5 N. R* T3 b9 L# b! X
. M0 A* d0 i2 w9 ^) f- q& j
# k9 m1 }# h* X- ~! H% L9 iAssume the first assumption in addition to the second by using assumeAlso. Check that both assumptions exist.
& z( `" h7 |" p& t$ k2 f' G/ t/ E. r& E; \) K0 t
- assumeAlso(x > 5)
- assumptions
/ \7 M* y/ K. d- e( d. k 2 `6 j' ~! x- g* h& w- M; |
2 m$ O* ]# O, Q" W' x8 u
- ans =
- [ 5 < x, x < 10]
5 P3 R) I0 i3 W c 0 F' E5 a U. |! C; Y
9 j6 b. ?1 G5 ~3 e2 m( ?3 {* d1 y
Clear the assumptions on x.+ f0 w/ X$ e2 x9 T9 P3 q
/ j3 i( m8 L( f/ i% g
- assume(x,'clear')
# w, R9 T/ ?) H3 e ! [) a2 Y0 Z4 I
: l3 Q$ q1 f" C% y# e! h8 nAssume both conditions using the & operator. Check that both assumptions exist.
$ J& ?0 T0 f+ K- L9 v: S9 T( q8 g! {- Q7 z; \, w. `! R! G1 z
- assume(x>5 & x<10)
- assumptions+ \6 ?* p/ O+ \* ?$ G% C. L; K9 T
1 d& p% j V0 c' z
O( s; Z9 N( _2 H; m) M$ T- ans =
- [ 5 < x, x < 10]7 y; g0 [; m1 o! M! @
. _3 j7 L! c7 o( M
4 r0 t2 C. x+ b _1 E' C! LClear the assumptions on x for future calculations.0 o" Y2 Q2 L6 v1 D
! z6 J! E0 ~7 q: {8 a( o/ Q- assume(x,'clear')
) V) x- A0 M* A- o- B- I- W 7 I, R! x2 Q1 q# |' Z: G% P
! l7 L) m& X1 U# Z. f, nAssumptions on Integrand5 _9 e+ L- ~( F5 u$ p: V
Compute an indefinite integral with and without the assumption on the symbolic parameter a.0 H5 A; A# w' a5 T2 {
3 Q) z: \7 S! j! `/ [Use assume to set an assumption that a does not equal -1.
) z, ]/ Y8 g, w0 C, ^2 P" x3 H: ~. Q+ ~/ K
- syms x a
- assume(a ~= -1)
3 o; G5 l' l1 {$ h0 {0 }( ^ 5 o) B7 Z) s" q8 y/ z6 E
; Z; H* o' h2 W$ G) }% x* `6 HCompute this integral.计算积分
2 v8 n5 |* M( ]
8 x* n) K1 f2 f
5 X9 V7 i, c4 @$ V$ m( j! h' s$ q: T: e1 @3 G M* A" L
- ans =
- x^(a + 1)/(a + 1)
0 ~4 d5 O6 }9 `
5 n6 [2 }2 I, d' O. w& `$ f7 O% J) C% w# d6 `& U" v8 }
Now, clear the assumption and compute the same integral. Without assumptions, int returns this piecewise result.
/ f! ?$ I/ a8 d, Q
' l/ r) j" w! G2 s% U- assume(a,'clear')
- int(x^a, x)% U1 N+ ~3 A: T# w8 q/ N" A/ H3 N7 X+ t
" r' N7 k! A6 L
1 o% D* V/ N5 [! @# Y' \9 w- ans =
- piecewise(a == -1, log(x), a ~= -1, x^(a + 1)/(a + 1))
8 e3 B2 @6 P4 C6 \# Q
2 {* P' n) m1 v' z8 i
. I6 ~5 z& O$ B# |! @, I$ [Assumptions on Parameters and Variables of Equation+ M9 d! q. d$ h$ n% K
Use assumptions to restrict the returned solutions of an equation to a particular interval.
! {1 n. S- G$ r. D+ B# A! f# [ l" b
Solve this equation.4 O" f- h, T! e1 l( N- S* Q9 S3 ?0 a
. V" F7 A% D. l. O. p
- 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)
, n3 w! r2 m# s$ p {
, S8 |! f7 ?- j. _, K7 h/ m0 B/ K, g# m- C4 u4 I
- ans =
- -5
- -1
- -1/3
- 1/2
- 100
" K' B6 a& @% h# h+ x
" L. n0 I s- y7 M7 k _
5 R8 Z6 ?8 K: x; rUse assume to restrict the solutions to the interval –1 <= x <= 1.
n: G! w! K8 c+ g0 [: C8 d: `! L: P/ t& o
- assume(-1 <= x <= 1)
- solve(eqn, x)& h/ \; \, d; k( H+ G7 Y
) U- n+ Y* c& N9 {' U7 T) K s0 F
, P9 R9 T7 T o9 r5 V# i
- ans =
- -1
- -1/3
- 1/29 A2 w6 Z' Y: |. W0 u/ q
) B$ {) ?4 r9 U: y1 S' s6 e6 Q& s7 U3 C$ m$ i+ B
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.
9 q5 X1 k3 o" A0 p1 J, y6 F# X5 b$ j" Z+ j! z+ c* y3 |2 p
- assume(x < -1 | x > 1)
- solve(eqn, x)) @' l; v/ X5 D* r; [5 Q
9 q- F# {2 k" O7 \/ u1 M3 u a! ^
5 N$ J/ y% b8 P- R' j: q) e; ~- ans =
- -5
- 100) \: b# u: ^+ K
* T! A6 E$ Y; ]" K4 s; T3 S a) l& ^" a3 a g
For further computations, clear the assumptions.) o. {( s) W* N4 U, u
& z2 p( |9 ~& s3 q) @
- assume(x,'clear')- h& B: l6 K6 u& |" H& ^
4 q6 ~* B! P8 U/ g2 j+ S+ ?/ V k
" f5 J' C; w# wUse Assumptions for Simplification
( E, P1 v% y1 G0 zSetting appropriate assumptions can result in simpler expressions.& g- ~$ v4 u' k+ h( F2 k8 o* P2 Q
8 \. G* |: v; g5 S0 o7 {Try to simplify the expression sin(2pin) using simplify. The simplify function cannot simplify the input and returns the input as it is.
) k, a, \' ]' K+ {) Z* c- n' S G
' t [, W' H1 v# Y0 i- syms n
- simplify(sin(2*n*pi))$ M. v" R% V D+ s1 _& X! ~
0 J" w2 j8 l2 N# C
2 g+ S2 v7 x. g! q. U+ O3 K- ans =
- sin(2*pi*n)
W3 ?5 T, |1 F0 p) ^% t 9 x C( A) D( G0 z- e
6 U4 Q Y. ~. Z/ A; wAssume n is an integer. simplify now simplifies the expression.
! z6 D2 A! x" x3 v3 h8 {5 U8 F! s
- M9 M" M- ^6 r7 T2 L9 k( G- assume(n,'integer')
- simplify(sin(2*n*pi))
0 n$ S& S# c$ G6 j( Z
! `2 E/ z4 j' d7 x7 c+ X% X6 D) v8 s2 ?- \& F$ y
- ans =
- 0
/ s( @2 m: l) M$ [1 \: N7 ^
% N$ h; X7 t4 R9 q; k1 m: I- k
( p6 G; Z- x5 A% R; G" _For further computations, clear the assumption.
4 x2 q3 @4 M; F! c
+ z4 |2 J0 T+ h' w X! e# ^- assume(n,'clear')9 F2 p5 U1 d; p [ L8 Q8 Y
8 K: r L9 W& @& m3 N2 g" w1 A
4 y8 k9 ?1 v6 U5 i# X* g! a
Assumptions on Expressions
4 B3 h: `' l" ISet assumption on the symbolic expression.9 S8 g( m. ~" W" W, C0 G# K3 J/ Z
2 V; O/ k/ z+ V) o+ y. b. V2 NYou can set assumptions not only on variables, but also on expressions. For example, compute this integral.
4 `9 g& u$ {) g* |7 m( y G: J5 x# e2 Y$ p" u# C: u/ R4 \6 U! V1 `1 _
- syms x
- f = 1/abs(x^2 - 1);
- int(f,x)
. ?$ T5 x* s! @3 [7 Y: T
+ |9 J: e1 ^% A$ T* T$ a: a4 @
. R% _& [5 n( B, N, {; b' O- ans =
- -atanh(x)/sign(x^2 - 1)
5 o3 Y8 l; d# I- j1 H. f9 R: n
% Z7 H- [! j/ U" m( D& S( A& f" \8 K+ x9 V5 _
Set the assumption x2 – 1 > 0 to produce a simpler result.
4 M- D) N/ y; |! G
/ V& t2 v* \: }" W3 d; h' l( R- assume(x^2 - 1 > 0)
- int(f,x)
9 f5 N& e" h* x7 \2 } , B4 N1 A1 h; U" U
- n Z; r; i/ j/ R- ans =
- -atanh(x)
8 K! R0 _* T4 g) g
9 e0 e% `3 r. [( ~2 }( Q
' c5 j( K" Q( H% w1 Y& }, {6 C1 A% X+ ZFor further computations, clear the assumption.
& p6 Z0 } P! X& x( h) n& R6 t" _; L, E& j
- assume(x,'clear'): P3 j/ |1 h( n M0 p
4 W2 z7 l; |& P2 l+ h$ h# \
3 ~! s: U6 p/ [3 ?1 B& W1 m2 }
Assumptions to Prove Relations3 v+ Y- I& V+ O
Prove relations that hold under certain conditions by first assuming the conditions and then using isAlways.- I) @ N' P/ }+ u: z0 J
1 G4 O9 J: g$ O9 e3 M# ?1 i; M
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.
8 B8 _; [9 Z, A! V0 y
( _$ l( A( {# _# f8 g- syms x
- assume(~in(x,'integer'))
- isAlways(sin(pi*x) ~= 0)
0 ?! _1 d7 A2 {/ s1 |
& q: g* ^) j4 T( i* I1 k6 N; |& x- b) O1 Z6 \: F# Q& z
- ans =
- logical
- 1( K4 Q5 q) y7 S j, \; {
" s/ i0 w! i4 p, o' j }0 M
; P9 L" k3 z5 N- a9 kAssumptions on Matrix Elements
1 i, N2 t. ~; V% o% K- t5 L% T/ {Set assumptions on all elements of a matrix using sym.; G+ e) m. d# o2 v( E0 p
/ O/ k! v# ~! U& K N# Q
Create the 3-by-3 symbolic matrix A with auto-generated elements. Specify the set as rational.# c4 X3 e0 L% W( j& e
! o: W* {- X- V2 ^0 n/ ~- A = sym('A',[3 3],'rational')
' g) c& U" s9 f) I
: J, s; ]1 n8 [: v A9 V. Z4 ?% l
- A =
- [ A1_1, A1_2, A1_3]
- [ A2_1, A2_2, A2_3]
- [ A3_1, A3_2, A3_3]+ I( K2 [2 p0 [
3 o+ V; i0 Y: x( ^- Z: d* T! I' g
) k1 G& m. l8 S( ?' ~Return the assumptions on the elements of A using assumptions.: D/ n. e- E1 R5 ~; o T0 g
' b* Z& |. v. ~& n: @0 C8 }3 k- assumptions(A), ?8 [; {2 o2 M% f, N
( P' F9 j, M8 k) |5 y/ R* c
8 H% l; \* r: q! z' j+ {
- 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')]
1 ]. E g+ c( h8 \2 Q+ i. m( z 5 L! u0 l0 [: Q* e
- r2 M. p- }2 \" G
You can also use assume to set assumptions on all elements of a matrix. Assume all elements of A are positive using assume.8 y" }* m/ z% E( J: Y! r
9 W5 e8 j% D; ^* q" V: |. R
- assume(A,'positive')
* o8 J! @% j. e; d
( y1 F3 X) @1 ]4 R8 A' h
9 n/ y6 p" c: rFor further computations, clear the assumptions.
& }% |2 j4 V3 X7 u4 }6 N* R# E
( d% G4 s6 |* Y( a$ n1 {) e3 F- assume(A,'clear')
$ `. o H: t4 S8 y" k ' Y" E% c t( |' K+ @' F
* x& C6 s& n4 m6 I7 a
Tips7 F2 p3 H7 m! k2 l
- assume removes any assumptions previously set on the symbolic variables. To retain previous assumptions while adding an assumption, use assumeAlso.
! N, u' f8 ~; a* w% R0 y% v' H5 E; } 6 D/ o1 d0 v6 G2 l1 ~. ^1 }
- 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.7 ?" k1 n0 Q5 p5 w! n3 [* Z
" M8 g* ~: n. l- V4 }
- To clear all assumptions set on a symbolic variable var, use this command.
+ K* i5 ^% o$ [( X) I5 j% Q+ o assume(var,'clear')
" q7 |* H9 H8 d1 a* `* B6 G1 Q# y4 m+ Z, x
- 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:/ D; P5 q5 n' U/ ?6 Y; y1 M3 f' V& d
clear all) j: p* P* Q& v2 p5 ` v( p$ b
2 F, z1 B* I5 O3 H8 Y; S- 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.% P4 N8 f- k6 r0 T' @! n! h+ t
& E5 \7 i5 B8 b+ A! ]" y
- The toolbox does not support assumptions on symbolic functions. Make assumptions on symbolic variables and expressions instead.) A: E% {- F6 h5 o6 X9 Q& z! I
- f- q5 A0 G9 ^3 X+ f. |' x
- 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.) ~) O0 l9 r7 }8 G" } [1 w# t* ]
/ W- D$ V) v1 k% ]* K3 S
- a = sym('a','real');
- b = sym('b','integer');
- c = sym('c','positive');
- d = sym('d','positive');
- e = sym('e','rational');9 a/ o# _, u5 t: L, F
6 U. T# n( D- l1 p- v2 Z* O
+ C& A' s# ]# A2 `or more efficiently:
8 X7 P) S2 [+ c1 M; `4 c' C8 y7 ~% v( l& Y5 V& W
- syms a real
- syms b integer
- syms c d positive
- syms e rational* u: X+ R" W+ p! X! I( {
7 x }/ ~& q% y; a; K |
|