数学建模社区-数学中国

标题: Matlab调用Maple的GlobalSolve?(全局优化) [打印本页]

作者: apple91    时间: 2009-9-8 11:08
标题: Matlab调用Maple的GlobalSolve?(全局优化)
我想用Matlab调用Maple的全局优化函数GlobalSolve,7 o# X" B1 s7 D3 |8 X. e
Matlab语句应该怎么写,
, p5 h& N' l0 \2 N# I7 _大家有没有谁试过这个问题。
: F' i& j0 E3 Z$ ~- D9 P: [谢谢大家。
作者: madio    时间: 2009-9-9 02:16
如何在matlab中调用maple , U5 ~+ ^, k. U4 J

; k4 C8 m" \1 G4 H8 n9 [0 o(不用安装maple软件就可调用)
; X% p4 M' f( z7 u1 F0 \( R8 ~5 i# |* m2 S
方法1:   
; i/ c. r4 X8 J9 z$ L4 B7 z9 x) K1 c" u, @; }: J9 K2 A4 ]
maple(’maplestatement’)
# u' q8 o2 F! h7 v7 r/ K6 z
/ m6 ]& `) x" P* L6 u' m+ W其中maplestatement 是完整的maple语句,由一条或几条命令组成,必须符合maple 的语法 2 u* l9 U$ U: Q* ?- P

( g: A2 h% ~6 y) @0 t) _方法2: 1 q, \$ F4 @$ r: @, U1 G& f& B

- p* G: P3 X: N  ]maple(’function’,arg1, arg2,…)
4 \. R+ a/ I& [) E: f' ]. a
. b6 t/ b* l. ~: j其中function为maple中的函数名称,arg1, arg2,…是函数function所用的参数。
+ T/ ]0 z5 M$ i# y+ E- A9 j: Q
) o3 E0 G7 b  k; z( O注:如果方法1行不通,可尝试方法2(个人经验)。
  |9 v! W! O9 F! E" W! B! }0 b4 X( @1 n3 t
基本代数部分
6 A5 |" V7 y: J" c* t+ w; i& _
, {9 V- M0 d- w: b& J5 g& B7 @如何用matlab求阶乘
% R) f+ c% V& D& L' `
& F8 k) b) A  m: G0 ofactorial(n) 求n的阶乘
4 k/ v: [) K. m% _0 h4 M2 T$ t: X; `; [. ]$ h: T8 x( w3 G3 T* u* ~; t
如何用matlab配方  + p- E6 h. z: E
7 t, ^  M" M3 x# w8 C( E
没有发现matlab有这一命令,不过我们可以调用maple的命令,调用方法如下: ' g- @% R) U9 q# V) o
: o9 X- U' P% l' C9 {! W, D1 ?
首先加载maple中的student函数库,加载方法为:maple(’with(student)’) $ X% v' j; D& Q, k: w0 ?8 D

1 J) C6 W% x; h然后运行maple中的配方命令,格式为:
$ W+ ]6 ^: r' b4 M; R, ?! @/ o, Z6 z* v6 T- {  Y
maple(’completesquare(f)’)       把f配方,其中f为代数表达式或代数方程 ! y1 c  w. V# Q& t

3 ]# Y8 ?; F/ U, Nmaple(’completesquare(f,x)’)       把f按指定的变量x配方,其中f同上
* u0 m( r5 Y4 {, p0 y; \9 d& Q4 @
maple(’completesquare(f,{x,y,...})’)    把f按指定的变量x,y,...配方
- ?& h+ n+ `* U; g$ R0 V8 y, C6 \
maple(’completesquare(f,[x,y,...])’)     把f按指定的变量x,y,...配方,
6 l; ~% O' R3 U7 ?' {  }% b) M- ?4 t3 _; y1 t' n9 }
如何用matlab进行多项式运算 4 y; U* w/ Z) I# m6 z) }+ g1 F% L" @
( G& N" v+ J8 U4 A3 g2 h1 p
(1)       合并同类项  
: k; b7 ^2 A( ^3 A$ U8 O4 T- M& L0 R( w7 l  A6 W( k4 g. Z9 Z. T/ k' |
syms 表达式中包含的变量     collect(表达式,指定的变量) 3 f5 c5 D; ?' s1 W7 @2 y% |
4 a1 U5 z* k2 ?  J; G
(2)因式分解  9 c' u8 q& A. E* j" a) M
- W* W& o* o" |7 f9 O7 U6 b2 m* a
syms 表达式中包含的变量     factor(表达式)
! w& t/ j, R6 t9 a! J9 {; j0 T5 b
7 I, j, ~; R2 c(3)展开
. @. M, m0 A3 ?" E( r
  F" o3 y. z2 g3 p4 P: C6 isyms 表达式中包含的变量     expand(表达式)
8 ]8 a  y* ~8 s' W* a, {5 x& t9 h4 T! _3 Z6 Q1 f% l/ w
我们也可在matlab中调用maple的命令进行多项式的运算,调用格式如下:
  o( U: k- u6 @5 c8 V5 P% f* t* n
; e  E, g7 w1 }0 C                             maple(’maple中多项式的运算命令’) : T# H3 ]) o6 ?) t

* i3 d- i5 N) {+ s) D& k如何用matlab进行分式运算  8 y( ~" l- d) N1 X) D. H/ @8 f
$ K% i3 K- T* ?& q/ S/ y7 S
发现matlab只有一条处理分式问题的命令,其使用格式如下: + C/ Q5 l/ J; K
( K1 Q# ?. ~$ e8 D6 k( \6 Z2 Q
[n,d]=numden(f)    把符号表达式f化简为有理形式,其中分子和分母的系数为整数且分子分母不含公约项,返回结果n为分子,d为分母。注意:f必须为符号表达式 & P5 x0 B5 I- C7 p7 i
! o0 k5 M+ C4 @( x% ]
不过我们可以调用maple的命令,调用方法如下:
( @$ w5 X5 t8 l- M7 E
& e0 u$ P1 @% s  |" w. R' a- H2 Jmaple(’denom(f)’)
( M; a7 e+ ?8 L0 y: d 提取分式f的分母 6 o; G# U* T. _0 r) l' l3 @& w

- _3 W2 s. u) g) ~* dmaple(’numer(f)’) % r# H; h4 g+ |/ |+ @$ z* P/ ~
提取分式f的分子
" s- V/ a5 Q4 }' _ 2 t/ `' Q( ]0 b- ]* z8 L
maple(’normal(f)’ ) - B/ Z' P6 o1 w
把分式f的分子与分母约分成最简形式
, R. Y6 O8 v  j" F
" c8 w* U# O* ?" |: r; |! Bmaple(’expand(f)’) " L& W4 P- P: ~
把分式f的分子展开,分母不变且被看成单项。
* E5 m: ]1 r6 f/ b5 ^
# d  m4 w* _4 c# R5 X' R3 Qmaple(’factor(f)’)
- a$ I) O2 W4 F. H: h 把分式f的分母和分子因式分解,并进行约分。
- L. k' e4 n" r4 C- h$ E
9 v( `' w, Q3 v4 T$ ~4 j9 e3 {0 G& A) b4 Z9 |
如何用Matlab进行因式分解 6 W0 Q% j  G1 Y
' g' f2 f( U* Z
syms 表达式中包含的变量           factor(表达式) 1 B! a9 ~( O: v# n* T) i0 j* M

7 a2 H, d, V# M5 q: d4 |) I; x  如何用Matlab展开
& h1 W! s& ^8 p0 m- L5 Q" S* ^& ~$ ^( M7 R/ N; ]0 y0 k! J# q. ^" D! U
syms 表达式中包含的变量        expand(表达式)  $ L2 o! Q8 K. F) s5 e
9 U4 D( \% O9 b5 l( o% H
 如何用Matlab进行化简 ; J5 C2 B- A6 d3 o- v/ I

/ D+ m: b2 N# C' P" usyms 表达式中包含的变量     simplify(表达式)  * v4 u$ j( u$ Q6 Z# X2 c

6 e8 B& e9 ?, E: y+ Q如何用Matlab合并同类项 : R0 e3 r$ v. C% N7 r
1 h6 E1 G; S/ v+ W
syms 表达式中包含的变量     collect(表达式,指定的变量) 2 ~2 E! ?- d; a  u5 e. \/ }
7 K( ]( Z8 B1 k* k( R
如何用Matlab进行数学式的转换 
3 E6 i" ?6 e6 ^% ~0 x6 u/ m6 x4 x) p
调用Maple中数学式的转换命令,调用格式如下:
  ]+ r+ W+ D0 U3 ]: W8 o9 l# ]! P; m2 |3 b& W+ |& Z, q
maple(‘Maple的数学式转换命令’) + I) b8 u8 r% o5 `0 G' U

6 O' X8 T. {# Y$ N即:maple(‘convert(表达式,form)’)     将表达式转换成form的表示方式
! W( ], }+ G, }9 I3 g& o/ i; }1 ~1 h& ~; ]$ U% j3 h
maple(‘convert(表达式,form, x)’) 指定变量为x,将依赖于变量x的函数转换成form的表示方式(此指令仅对form为exp与sincos的转换式有用) . g" {' k& `- q) o; L: u; X9 C
7 I+ C" y( }4 D4 f% S) t/ P& c% N
如何用Matlab进行变量替换 % T2 T$ _. R0 Q8 ~( x# J8 }" Q
8 \" v3 v/ o7 k9 E. ?0 |$ _+ I
syms 表达式和代换式中包含的所有变量    subs(表达式,要替换的变量或式子,代换式)  0 z7 y% j2 M* E7 L4 E: ?

4 P( d' |( i) y4 N5 ~ 如何用matlab进行复数运算 
$ l$ N+ `. O* X0 v( I( z+ L* J
# Q1 A: B4 u; w  ka+b*i 或 a +b*j
, P+ j4 }( g; l6 O1 _& p 表示复数a+bi 或 a+bj & n# k7 U4 T! J9 F
. E, X3 d9 |, ]4 p9 T' p
real(z)
. N6 D2 g: J7 k! v/ \1 y6 r 求复数z的实部 3 N8 B& p6 r. w

" [% T- M: ?  Y7 A" L/ p& jimag(z)
! W% l8 a* r& F 求复数z的虚部 1 H$ N: E3 L8 v9 Q, B
1 R& y9 W, c: o' }7 d! N
abs(z)
* D$ |' @0 Y$ l" p+ C 求复数z的模 2 i/ X# V% ]  e, Z

! F+ |0 u) x6 J, K( O; i5 vangle(z)
$ f/ X/ s) _3 s. t 求复数z的辐角,
/ w' t! [3 o/ c+ e1 o2 U . m0 f, y# K* r1 f/ @' b2 k% }+ m' }
conj(z)
" A: u9 }7 g2 p& R& u 求复数z的共轭复数 ) u) L6 F% f; h" ~3 F( Q5 F

4 B7 S9 O8 N* Y- `0 Dexp(z)   E! V. q& k( Q3 N0 s/ c; S4 l
复数的指数函数,表示e^z
8 \1 R5 ?/ m! s7 |9 I2 q9 c
* S: ^: a7 d) Q. v: a+ h, ^0 C
/ x- t! X5 x" {" D# W7 p如何在matlab中表示集合 2 O; K* l; r9 E+ O
: l3 G: B1 S  Y" ]! u+ D1 P$ g
[a, b, c,…]  表示由a, b, c,…组成的集合    (注意:元素之间也可用空格隔开)
+ O# ?+ t2 Z. G8 {2 E6 A1 A* n2 o7 a; k- M9 N
unique(A)   表示集合A的最小等效集合(每个元素只出现一次)
8 i" Q# p' X! v3 j+ R, \3 M7 g
7 g9 n! A9 s" R; |6 k, t也可调用maple的命令,格式如下:   n# ~) s# X% t* s' i

9 D2 \' n. n' H; O7 w1 f. M4 ~% lmaple('{a, b, c,…}')                     表示由a, b, c,…组成的集合
( n0 T3 B2 Z9 Q+ N4 g( S+ \6 i
1 L( j8 `, G/ x4 }$ e- c下列命令可以生成特殊的集合:
8 |) u9 ]/ g1 M" W+ q
5 T  I; ?; Z4 \2 R, S3 O9 O2 Xmaple(‘{seq(f(i),i=n..m)}’)               生成集合{f(n), f(n+1), f(n+2), … , f(m)}
% j6 d. g5 ]1 C: W% b+ _! @$ ?# O5 |4 u
7 w+ s$ |0 z. K2 h; u/ N, V如何用Matlab求集合的交集、并集、差集和补集 
$ T; [5 w1 z# ?: ^4 n( Z- x* d) {8 r* z) B
union(A,B)
5 I( S& y3 [7 k 求集合A和B的并集
, d; T2 E# O2 i* R  G
# w$ p: I* q; f4 I* Aintersect(A,B)
$ D, D3 {/ M! ?7 Y, ] 求集合A和B的交集
/ Y) b- p2 ^5 ` . C( b% }% B; D# {
setdiff(A,B)
9 a& \% a+ v* h  B' J 求集合A和B的差集A-B
" e: x& t6 N* ]3 P  n: a6 S ! b1 j% t0 I$ _" W! z) U
setdiff(U,A) - ^4 e' [3 o) Y
求集合A关于全集U的补集* G& U7 Y+ |2 [" }, ^2 X# F
/ S; P4 @7 C' g! g" J2 g
& V" l0 R$ r& X+ `. e0 Y0 P
我们也可以调用Maple的相应功能,调用方法如下:
3 h% ^7 x( o+ z0 K% x6 r' [* [  e' r" b( k3 N7 {8 v
maple('用Maple求集合的交集、并集、差集和补集的命令 ')
$ ]1 N9 K  M4 ~" l, B/ X6 R6 C" j2 c9 d1 Q- `0 H4 @& i( ^
具体地说,共有下列几个调用命令:
# l' U( M& W& y! o3 W8 q- I; ^( _7 ~3 X2 G( c. ~. B
maple( 'A union B' )                                          求集合A和B的并集 4 ^! N  m( s* ]- f) d

! u+ l1 C- T9 H& I5 A' gmaple( 'A intersect B ' )                                   求集合A和B的交集
, v6 T/ a" L: g. V! T& _, ]# j
' i! H2 ~' l. J+ B8 r( v1 n' r3 Xmaple('A minus B ' )                                      求差集A-B
" \0 R- r8 L' P4 _- b
  ~: k! q) }4 \: @% ?1 {7 omaple( '全集I minus A ' )                                求集合A关于全集I的补集 + f* F& m* m; T( b
' g) T4 P, L, O
如何用matlab排序 5 l  R6 d- s3 {  l% U- V. ]8 A7 L

. w5 A" r# l9 @# Csort(v)                            将向量v的元素从小到大排列(升序排列)
作者: madio    时间: 2009-9-9 02:18
Symbolic Math Toolbox   maple
3 D1 Q! M2 @' C' l" b
( ~# i& I& A4 ~
# Z# _2 c8 s$ i( aAccess Maple kernel $ z0 W" V$ ^1 r2 d
  |7 V# [- Q' t
Syntax
' s; A% z2 A3 L& x5 k0 s
6 p: q! j. c2 S- C: dr = maple('statement')
0 A- q; o! k! u" ~$ ?) n1 Hr = maple('function',arg1,arg2,...)
' O" D+ C2 |1 C  Y% a: J[r, status] = maple(...)
7 @7 Z/ R" i7 h$ O: Wmaple('traceon') or maple trace on
3 \  T8 W7 V! y! |- amaple('traceoff') or maple trace off4 F2 O' U$ @! ~( z7 O

8 l+ c5 u3 o( s
1 r# o: }; k, \7 O8 z* f; K4 O  Q- E3 u2 ~  @
Description  H! }) x4 R: F6 S1 z

, O  a1 u& i1 W# b
* ^6 x$ t1 g5 p" h9 s6 smaple('statement') sends statement to the Maple kernel and returns the result. A semicolon for the Maple syntax is appended to statement if necessary.
2 \7 X9 N+ [. v$ E0 W  [' `; ?$ C3 |. X7 K# W- T
maple('function',arg1,arg2,...) accepts the quoted name of any Maple function and associated input arguments. The arguments are converted to symbolic expressions if necessary, and function is then called with the given arguments. If the input arguments are syms, then maple returns a sym. Otherwise, it returns a result of class char.
2 e" {" f5 A8 j0 c: d8 Y3 b$ f; D( O4 i$ e+ U
[r,status] = maple(...) is an option that returns the warning/error status. When the statement execution is successful, r is the result and status is 0. If the execution fails, r is the corresponding warning/error message, and status is a positive integer.
9 w# ~& a  K# e! w0 I* r- E
0 R) _5 k$ S8 M# {& @; Kmaple('traceon') (or maple trace on) causes all subsequent Maple statements and results to be printed. maple('traceoff') (or maple trace off) turns this feature off.
- j+ D* }8 J5 `% L2 ?) v: p1 M0 G
  U4 W9 ^* p* w  k* h9 D0 q- fExamples
; l+ u# e# T6 \8 \5 ]. ?# H
1 T7 B9 O+ a6 `* w: d& D. q5 M1 j
8 }( j, W- F/ }% AEach of the following statements evaluate  to 100 digits. / o7 D& }/ x* n" ~3 g
maple('evalf(Pi,100)')$ Y: C: h$ e2 y% s
maple evalf Pi 100
6 P# T4 c( L. d+ {2 U( e5 dmaple('evalf','Pi',100)! t2 u9 Y% E1 b" Y

6 M6 C, j2 B% @0 w0 {. m$ [. V4 x5 o6 L6 Y

. p- M( Z! p% a* lThe statement 3 W, e( Q7 O$ C' p0 D
[result,status] = maple('BesselK',4.3)
1 t1 k3 c; ^2 v4 o- i' p3 y
! i; E- l* g& d3 i
( Q) ]' G8 v4 I5 B& @) u$ C2 M* ?2 \# [# w# I4 T2 D
returns the following output because Maple's BesselK function needs two input arguments. 2 O+ _5 y! i8 D. r
result =2 s9 |' b1 ~8 A4 G) V% g
Error, (in BesselK) expecting 2 arguments, got 1* }7 ]. N1 e) c6 N8 s) {! ]" H
status =# g1 G4 A3 o# x- I( e5 P4 Z
2& q8 \! C0 o/ D. m8 N

1 G+ c5 f' T4 O9 M0 v9 z* r: u% _

6 Y; g- l! p0 Q7 f2 [The traceon command shows how Symbolic Math Toolbox commands interact with Maple. For example, the statements
- F! u9 U& ?: ~8 Z1 B# M6 A- ^& qsyms x' p- }) E# S) [' Y
v = [x^2-1;x^2-4]
0 f' e- d' ?9 w# w' e* w' Dmaple traceon % or maple trace on
0 p% a$ ?4 h9 s1 t, ww = factor(v)' S6 f8 k) R1 [
* R+ l: G7 h! k
+ D9 o) x6 Q, s' ^
0 C% J8 g% K; M: ~3 m/ C. |
return . ?# J/ }& a; M0 x
v =( h6 s) a. t6 A/ v6 o& s
[ x^2-1]* B: J* A0 O/ A6 z
[ x^2-4]) W5 d% U5 M) E3 p$ @

  ^; J: q1 K6 X1 p' t4 _1 @statement:
* E& Y! N, t5 C0 V' Q   map(ifactor,array([[x^2-1],[x^2-4]]));
7 y- c9 R( f+ B0 cresult:1 Q4 k5 p6 |$ d# H  a/ c) z5 m9 W
   Error, (in ifactor) invalid arguments* b. O+ D& y( C- E
statement:
! u; {6 ^  s, E9 p. v; e   map(factor,array([[x^2-1],[x^2-4]]));
% X* W4 Q! s; P/ x: T  P# }& ?result:
, H: l0 j8 b. U; o4 V' ^( g; x8 v   matrix([[(x-1)*(x+1)], [(x-2)*(x+2)]])  ]  T6 f. m) l% s
5 Q! y! s5 T" T0 r( T( j/ x2 x' R
w =6 Z% L  W  a0 T: d# }

/ m: ~. n- c/ L8 M[ (x-1)*(x+1)]
! ]4 j/ |/ r- `; y- l, }[ (x-2)*(x+2)]
* l8 K+ x/ P8 W; j  W9 A" [6 n8 g# X+ Q# T

. O: A$ x( S5 j6 }% [, d- _3 t+ Q1 U# {
This example reveals that the factor statement first invokes Maple's integer factor (ifactor) statement to determine whether the argument is a factorable integer. If Maple's integer factor statement returns an error, the Symbolic Math Toolbox factor statement then invokes Maple's expression factoring statement. / N# [: X/ x2 |2 j' I! D: X4 E
, L: y- K% L3 n. ^
See Also. p- I) |  `* I& t; K
, H5 {  r+ r5 L% E; m6 Z
! v8 R. ?! D* T5 R2 g9 e
mhelp, procread
作者: suolunga    时间: 2009-9-10 13:30
学习了!
7 [* L0 [% a! y' A/ z谢谢!!!!!!




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5