|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 thinkfunny 于 2021-1-22 10:20 编辑
# T- k- d9 ^9 `$ N. k9 d% ^+ J
Assume7 C% }9 _8 c4 z! ^0 W/ y$ s
Syntax! o6 R% t4 Z# @! w
Description. J" n+ H2 l+ b& [
Examples, C9 O& a# ?) {1 P& ^1 E$ A0 D
Common Assumptions; a" O; ?" E2 {* f* G
Assume Variable Is Even or Odd. @ [. l7 O, a2 c K
Multiple Assumptions+ `' R$ p) t1 R
Assumptions on Integrand
! `. f6 S" y$ E) e( P2 d$ e: wAssumptions on Parameters and Variables of Equation
* |$ i8 |: a# y P( P8 HUse Assumptions for Simplification) T: Y4 P: ^& J8 T# O5 }; Q
Assumptions on Expressions
* Y/ e! n3 @! ^& }Assumptions to Prove Relations9 y. J; t9 d$ b: c3 D
Assumptions on Matrix Elements
% i' a8 }( Y0 iTips- x$ j8 c0 C* s8 [" R" F
Set assumption on symbolic objectcollapse all in page
- w( p; y( ^, l: W9 C4 H# _; f% p2 @% f* t
# Q3 C: j7 s: t# F4 l3 U; d
Syntax) j- B, v) P7 O+ L5 }6 @' T
assume(condition)8 a" r g. d( L5 A" n4 C- _+ a2 B
assume(expr,set)
3 L: T1 {/ U) cassume(expr,‘clear’)
Q7 s7 ]5 O( Z# W/ [: t+ l* z6 V9 m) }& X0 T
( q# r$ |# R. m% NDescription; f* s! ^) Z+ H1 h* p
assume(condition) states that condition is valid. assume is not additive. Instead, it automatically deletes all previous assumptions on the variables in condition.4 |9 S) H7 |2 k e, ?
2 k, D u, h9 Q# p$ ^. l# x
assume(expr,set) states that expr belongs to set. assume deletes previous assumptions on variables in expr.2 m2 g! g) q+ _* X
6 U7 I# P' K: f0 A( l/ k% S
assume(expr,'clear') clears all assumptions on all variables in expr.
2 K1 Y1 D8 W# @
& R" u) Z8 u+ v/ ]9 f; n$ `
& H. c! p: ^5 {- `Examples9 U0 g! m. {8 J, D; Y5 z+ ? _
Common Assumptions. X( a' E, H7 V9 k' \. }+ P) w/ j
Set an assumption using the associated syntax.
6 J: B8 E) L, F! `# G0 v/ V0 b5 B
0 W( c" R& f- r( I
# @- ~/ H6 c& V2 b6 v, J- Y
* C! A% `# ]; j1 f1 T5 R7 @; [
. I% a" @" @( s' {* W/ }
& |. Z$ E) e% Z y( u- real:实数
- rational:有理数
- integer:整数
- multiple:倍数
- even:偶数
- odd:奇数
+ i. G2 M0 m. ]& ^, Q2 D
2 P3 f$ K/ o$ }9 R) ?* p+ R# U0 E' `% R6 `
Assume Variable Is Even or Odd
- ~) S9 Q1 t% M# z& ~. q$ B% i% U$ X8 r/ a+ o$ F' m( M
t% f/ F5 y! G# c$ w( G
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.
* g2 |& E% J" m/ J# o( \
0 X; ~3 f1 z$ Y8 b- Assume x is even.0 D; L9 h; \9 i8 e
- syms x
- assume(x/2,'integer')
9 c$ j6 g: `" l& A4 ~2 c
9 R' ~' l& A( x5 l
7 B k; n' W" M- N n1 Y4 |% u3 `- Find all even numbers between 0 and 10 using solve.$ s0 ~3 m* y/ e! M
- solve(x>0,x<10,x) i5 V1 \4 @7 X% b! D* |
- T6 ~+ b1 S; q2 L' [/ p) l4 h2 F
( y9 ]. M+ U5 q( V( {
- ans =
- 2
- 4
- 6
- 8, l2 x/ O4 G W4 Y5 V8 a8 ~
, a F2 ]+ I! P) y5 W' F( K' |$ J% l: K3 P/ d- M+ O
- Assume x is odd. assume无法累加,而是自动删除先前的假设(x/2,'integer')。
. @. g7 i; s9 P5 U# L6 `
- assume((x-1)/2,'integer')
- solve(x>0,x<10,x)0 J- k/ ~2 u5 g6 t8 n# c
) m8 S$ b. m) D% e; h6 y4 j+ M+ L& j3 l6 U7 E- f1 f& P
- ans =
- 1
- 3
- 5
- 7
- 9' | Q+ A6 S$ N! G3 t. Q$ x R
6 L" D7 }0 n2 T" v0 W4 T" h
. K" {; m- n6 n' K9 {
- Clear the assumptions on x for further computations计算., D y: ]% j) L; l! I4 N
- assume(x,'clear')
6 e' r9 `1 S* }7 _! m. p" I $ @1 p4 |5 t3 t. O; ^
6 U4 j2 l8 K' `( |' U' NMultiple Assumptions
3 ^$ E! g+ Q9 l7 {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.
+ {+ M0 `) D& D7 D' ]8 z1 M$ b8 ]/ m( W
连续的assume命令不会设置多个假设。相反,每个assume命令都会删除以前的假设并设置新的假设。使用assumeAlso或&运算符可以设置多个假设。
) i# l9 k0 {! o, E- a! Y
4 \4 F. L) W' p3 U3 B, @# a# i, eAssume 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.. c$ J) A. B6 N
. t" X" L. z5 C! y" ?! l9 |' g
- syms x
- assume(x > 5)
- assume(x < 10)
- assumptions. p) T8 }. r- W% v( N- f1 F6 H
, m; X% {5 @: g3 _ z! H4 K. ]# }; V
- ans =
- x < 10/ a% _8 e) c2 O! {! ?" ^0 W' E! y, ~
) c% s" T, m* u0 s8 z4 o- @- R
3 @; Q8 G f- `* m9 fAssume the first assumption in addition to the second by using assumeAlso. Check that both assumptions exist.
( u: B2 \! @ W3 W+ G! P3 N$ m
0 h* K; b5 R% Y# ^# g' r9 P- assumeAlso(x > 5)
- assumptions
) n* @0 r2 ?% Y: x$ n* N2 c4 ? ( s# L" u8 t& r/ j3 O$ z
0 G0 F Z2 K- Y5 {" `- ans =
- [ 5 < x, x < 10]
+ s' M& n `6 p, k0 M; ^ u1 ^
/ c6 m' G% D' @9 e# O7 o4 G" G: ?3 v5 P" P* A6 n8 a4 b5 ?7 e. M
Clear the assumptions on x.
- s1 \8 b: A' Y7 z$ Q. _- N# W3 B/ R& Q M
- assume(x,'clear'). e# {. M% R- r. M4 l8 C
+ {3 ?$ C) K c1 k
p* b( }1 b( H+ w+ s! z% g! V
Assume both conditions using the & operator. Check that both assumptions exist.+ o7 i8 n/ _- b. ?% C
- K% |# @& i# A V% S- assume(x>5 & x<10)
- assumptions$ |& z, h% n* W1 |6 }8 X, @1 h
8 G/ e4 `1 S( ^$ h2 A. b& d& n
. I* X; d& y) N/ l
- ans =
- [ 5 < x, x < 10]
9 z1 s/ j% W0 k- P% a, N! u & n% y: M- Q6 V
) L: w4 n9 f$ y( M
Clear the assumptions on x for future calculations.8 ^7 |6 v' N2 V7 c1 ]$ Q
7 p+ A( _$ s8 ]8 \! C9 @% D A+ E- assume(x,'clear')
' K- ]) d' t7 x1 R. b- E
! j" _2 N7 _% i/ ?7 g
3 ]6 C5 q" l4 F. v& t; nAssumptions on Integrand
' t& A, u: A# _0 `Compute an indefinite integral with and without the assumption on the symbolic parameter a.
5 u0 p, u3 D! l. x9 a* L g& H9 a
Use assume to set an assumption that a does not equal -1.# [/ w0 Q4 u8 K9 b0 A8 P$ a
* Z5 W" l# F$ c1 L" t- syms x a
- assume(a ~= -1)
) D- U. T+ p' r5 @0 u/ Q/ K+ y
3 a2 w* n' _) P; K9 l0 [7 ?6 |) w3 b9 F1 x) o2 }* G0 Z/ l1 g
Compute this integral.计算积分
1 z3 \/ M* ~' D [( ~) A6 I
4 |3 t& i, @. P. i9 w. M- int(x^a,x)# |. X3 ]1 p4 R8 L
7 E5 c3 C$ \& s: B8 R
/ a; x5 S9 H" c- ans =
- x^(a + 1)/(a + 1)
$ {/ X$ A, \2 S" N- w0 `% S
- d. `" ]: ~ _0 ~# |4 @
! e3 Z; S8 R, aNow, clear the assumption and compute the same integral. Without assumptions, int returns this piecewise result.
Y# Z( m: K, Z1 _! C. Q7 F
) W. u0 @1 t8 y" L6 A# u s, C- assume(a,'clear')
- int(x^a, x)
, t$ U+ f; a/ }4 t! L2 H 5 e, {3 e% i7 k1 ]5 C
) G3 _& ~& d& @
- ans =
- piecewise(a == -1, log(x), a ~= -1, x^(a + 1)/(a + 1))9 X4 C% o: Z- V
* {7 v' [1 p$ R! \! g
! [/ m. f7 c4 L; I/ D9 E( v% Q2 RAssumptions on Parameters and Variables of Equation
; |4 r+ q x9 O1 P$ n7 v+ q, m+ MUse assumptions to restrict the returned solutions of an equation to a particular interval.# ~9 N- ?6 K$ \& N2 g' h2 V
& ]6 w; l8 k: `
Solve this equation.
& Y' I8 P$ o R2 e( {. m
4 V2 B3 J) ^: l- 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)
" N/ g) v/ _3 C & j8 j5 i6 O Z1 r( h% @7 Y
V; W3 \$ ?% J/ k/ ], G. p- ans =
- -5
- -1
- -1/3
- 1/2
- 100
+ p v+ w- z4 ?. F
# p5 ?; g( g( l& d U6 o* P. y5 g' G, D& R3 a- L) z- M
Use assume to restrict the solutions to the interval –1 <= x <= 1./ j' {4 G3 O6 w) g- n
1 J) d1 x! b0 {1 V8 `7 L8 H9 x- assume(-1 <= x <= 1)
- solve(eqn, x)* I7 t; |3 t% m2 J
/ }8 @2 {: w$ f( c
/ C# S) X+ E. I2 J2 u9 D- ans =
- -1
- -1/3
- 1/2 z" p% ^. n- T! i% r0 ]/ a
" H- F- m$ v" e- K6 A w# Z
1 ~# ~! }9 B* M0 a
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.
* e# l3 U# C( t, i1 K9 F! W, ?$ ?" j* I0 w0 B: @
- assume(x < -1 | x > 1)
- solve(eqn, x)
; m, C Q* Z$ o( y 0 J4 ]7 ^: u" h8 s% K- p! j8 J
, j$ K3 }: L+ {. b
- ans =
- -5
- 100# i" f2 ?- M$ _6 n
, P6 d& U1 h/ W2 ^) d2 w) e( u4 j5 Q+ f& S9 x2 L
For further computations, clear the assumptions.# M$ X+ j" ]/ K; I7 U. q
& U1 c5 Y/ H/ U
- assume(x,'clear')- k, m: x- a, f5 z. V& V+ u
1 e3 g8 Z4 y. B
4 S: S% L' \3 Y& e% EUse Assumptions for Simplification) P( a) E6 ]6 m; E
Setting appropriate assumptions can result in simpler expressions.. S$ h9 B' t# S7 U$ k# o) o" n7 p: x
( {6 t% M: o* R0 U# j& [+ `- m& _
Try to simplify the expression sin(2pin) using simplify. The simplify function cannot simplify the input and returns the input as it is.
' }& Q [4 D/ E& T8 Q/ q" @* Y5 W: T9 j; O
- syms n
- simplify(sin(2*n*pi))9 |! H- f1 G! c( A, ~' t
4 l* A# e+ `# l" W2 c
- G7 U& |, L% F8 D
- ans =
- sin(2*pi*n)# Y* Q3 F' F3 P% K1 I
: s1 W" c5 s& o' x4 O. E6 s
( |2 N! v6 O3 C! SAssume n is an integer. simplify now simplifies the expression.& E$ E) M$ i0 q: `$ G
% H; B4 a% D$ H4 ~" \
- assume(n,'integer')
- simplify(sin(2*n*pi))
0 J3 j* v, @, d, O2 T; a2 C4 |
4 L/ K6 F" D8 g+ o* `+ ~8 V* \' Q" |+ Q; f' M
- ans =
- 07 b9 P3 m7 a# D c1 q) l) _6 [
5 N, g ?/ V4 O. S
; l2 t& j) a7 a5 P8 _1 K# a# i2 |+ UFor further computations, clear the assumption., m7 k6 ]6 d; J; y
2 m9 V, X% \" ~* h. `- assume(n,'clear')" x4 u$ l* N1 ^" F
. m9 l1 }2 `0 Z5 }( ~# j5 Q; F5 X. w f; a
Assumptions on Expressions
2 D2 [( m$ Q3 f& RSet assumption on the symbolic expression.) D& P) j" s2 i, Y* m2 z' x
) X/ t& f5 J lYou can set assumptions not only on variables, but also on expressions. For example, compute this integral.
$ w/ |1 W E' p. @4 A; l3 b+ O) c* J' M* v
- syms x
- f = 1/abs(x^2 - 1);
- int(f,x)
, j& n1 t- R! c : }( G8 J8 y7 P
: f" w, e) m! t. o# ~/ }+ P; ^- ans =
- -atanh(x)/sign(x^2 - 1)
+ s: G9 X& _4 l M1 @- b* F
, }2 X* y" v+ X- w7 l+ V
- p3 Y6 y! Q! CSet the assumption x2 – 1 > 0 to produce a simpler result.
; \$ D: X4 [+ O6 o1 j5 o [$ l7 d. v7 p. ]7 }; H) b% Q7 a
- assume(x^2 - 1 > 0)
- int(f,x)9 K& l1 L% M5 D5 ~$ U) f
/ b( r- l, g( q4 O0 k5 K! V
& i3 T8 _2 p9 L6 Q$ o e2 P- ans =
- -atanh(x)
) h! J7 U: {7 \ 4 m* V1 s4 j0 i! T7 ^
0 P* T' u" w( e" V/ i7 _' r, E
For further computations, clear the assumption.
# ]: \/ e4 y$ ?; b" \0 o& H" u
7 Y' Q4 k8 z7 K% M- assume(x,'clear')
0 }- K9 X! L- _1 r
* V1 [8 J3 ^" k% A0 F' p2 X
. Y( a. u4 |" l5 X$ N" W1 @3 ~Assumptions to Prove Relations( s' n9 T& C! N0 L0 K6 G. G2 l& |
Prove relations that hold under certain conditions by first assuming the conditions and then using isAlways.( s5 X1 W& d& g; H2 V
/ j) ?/ ^4 \2 e8 P& M$ HProve 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.
5 m5 j* ]0 r! J) v
, y" h9 L `+ X3 u' i- syms x
- assume(~in(x,'integer'))
- isAlways(sin(pi*x) ~= 0)
% W" P( k$ I6 U: X) f/ P; r: ~) n3 j6 M 9 I! l& q3 t& w. a
( W3 h2 A8 ~7 E( A3 n7 f$ `
- ans =
- logical
- 1, F3 B+ L+ U6 S" P$ d0 ? m: h
+ ^, a" x; j2 {
4 l9 j* n a8 g8 ] ]1 q( @Assumptions on Matrix Elements
8 M% }$ k4 M# w" s( uSet assumptions on all elements of a matrix using sym.
8 T" ]% O0 u0 o! W/ P9 r6 n; _
! }7 i" [9 t t% dCreate the 3-by-3 symbolic matrix A with auto-generated elements. Specify the set as rational.
: N g, R9 Q* M: R6 u- {. u/ B8 X, e, ~# d9 A
- A = sym('A',[3 3],'rational')
% X7 V* g9 z, ]" g 9 l) |' t& Q8 C! b9 Q- c$ f
1 f* w* h3 m" M& G
- A =
- [ A1_1, A1_2, A1_3]
- [ A2_1, A2_2, A2_3]
- [ A3_1, A3_2, A3_3]$ ?0 C. \7 H* @+ n' t
, D* @3 o: b/ E8 z- Z* _0 H; I1 s# X5 I7 p# x
Return the assumptions on the elements of A using assumptions.
8 |2 O0 g4 Y/ y, F, R" }2 A$ @' B5 E8 E- F: x8 }+ h# Q
- assumptions(A)2 Q$ p, A3 I4 ], f
9 ~, x h* v1 i: p0 L8 u2 a! J% }( Y/ L! H- c
- 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')]$ y' f% d" s: H, O6 U1 w, b! B
9 e4 i1 b; i1 I$ o
, H5 c* e0 I7 {/ ]
You can also use assume to set assumptions on all elements of a matrix. Assume all elements of A are positive using assume.
0 q6 D' {% e5 n" p R+ _4 O8 e# f; ]7 N" L8 _# w: g o' y
- assume(A,'positive')
1 ~% e* h* ?, H" a * [: Y& ^+ ~- r8 ~2 O: W7 u4 w
+ p1 {, ?# Q' e# c- V( \6 z
For further computations, clear the assumptions.# p/ K/ v! I( z0 K8 {+ Z
, H# T2 j9 }/ g# ~3 q- assume(A,'clear')
; d$ k6 x( a! t! P
2 E+ v6 r9 R9 D; G: `6 r
1 A: |& [% D' XTips
+ f, [. Q# g4 V- assume removes any assumptions previously set on the symbolic variables. To retain previous assumptions while adding an assumption, use assumeAlso.$ Z. {8 |; z" R8 n9 {; K
6 E; @/ f+ o1 P
- 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.
/ C1 i$ s7 m `. q% Y& I0 Y9 S
: h' y4 B/ O9 I) U# h- To clear all assumptions set on a symbolic variable var, use this command.
! l9 L! ^3 @$ N" t assume(var,'clear')
+ E0 S- E& u( ]1 D" Y/ M& c( g9 P* y v# E* P2 |3 \1 i$ W
- 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:
/ |% k) d ?( T9 t9 f, |" ? clear all6 y, r+ F5 u4 P2 z3 e1 E# i
3 S6 d3 Y( Y/ v+ s; B% a- 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.
0 C# J1 {# q( {; g& Q
) [8 v! ` k$ x- The toolbox does not support assumptions on symbolic functions. Make assumptions on symbolic variables and expressions instead./ S+ @1 ]! [. K9 h1 b
0 t% B% K- \- {+ I) u
- 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./ y8 x& \( Q# I1 m* }% ?9 d- W
# x: h7 J% ~* g0 F: A/ ^( m9 {% ~- a = sym('a','real');
- b = sym('b','integer');
- c = sym('c','positive');
- d = sym('d','positive');
- e = sym('e','rational');
' y9 r# _9 X3 q) v
2 k1 N; I J" i R
' h6 _6 ?$ N1 J! Yor more efficiently:* M; t( |* { J. T. k* q( N
& d4 A/ B0 m8 v* {* v* ]6 t- syms a real
- syms b integer
- syms c d positive
- syms e rational
# f5 V! s8 A# r7 J# N 6 ?9 ~! |1 O# \$ n3 ^: w2 H) D
|
|