|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
如下为NAG library里面e04wc的程序,是别人文章里给的,用来求某函数最小值的,但是我运行时会说错误使用e04wc,输出参数太多,球球大佬怎么搞?
& I2 f3 d" h7 y" |6 g! i% F5 q* G8 `/ y3 E
, O1 i+ {( R. _4 T0 V! y7 ?; @
function e04wc_example
8 V# q8 k4 s+ d5 W3 `! Y4 o- z' D0 T3 P
fprintf('e04wc example results\n\n');7 C d0 C0 G! E$ z7 S: O4 R
a = [1, 1, 1, 1];2 n5 h9 r; E9 R+ R) D
bl = [1, 1, 1, 1, -1e25, -1e25, 25];: p% {* e4 g6 {- N* I/ k
bu = [5, 5, 5, 5, 20, 40, 1e25];' c% h: _3 i1 R
istate = zeros(7, 1, 'int64');
% z% c5 I4 q* i+ Y* |ccon = zeros(2,1);
0 n L! ?$ D& ^2 w3 x) n' n" fcjac = zeros(2,4);! b& H1 l: J8 c! j; N
clamda = zeros(7,1);
/ d5 w" b t E8 x, Y' l4 ~# |hess = zeros(4,4); Y, o8 ]" \- M; a
x = [1; 5; 5; 1];, q4 i8 h, i" O7 R
[iw,rw,ifail]=e04wc;
9 G* ~; [- }- a# y[majits, istate, ccon, cjac, clamda, objf, grad, hess, x, .../ L9 D$ w" T: k2 @) U3 d3 ]
iw, rw, user, ifail] = ...
* i, i, G6 q) N: o( C, K! ?8 E4 ~ e04wd(...
* m4 ~7 |7 y+ m! N3 @- a8 C' H* A a, bl, bu, @confun, @objfun, istate, ccon, cjac, clamda, ...
' b3 I- y$ A- g5 ? hess, x, iw, rw);' b* e% l5 E6 \$ a% s
fprintf('Final objective value : %8.1f\n',objf);* D' S% M& Q" T+ ^
fprintf('Optimal values for x : ');3 P% ^2 z# E. U8 P
fprintf(' %9.2f',x);5 ?' m! O- F8 C# o
fprintf('\nGradients at x : ');
5 h# u. E3 G. y; t1 v- Rfprintf(' %9.2f',grad);1 P1 F- c' ?$ k/ x5 R$ u
fprintf('\nConstraint functions at x: ');& l3 T/ i1 m4 F
fprintf(' %9.2f',ccon);% T/ C: K. n u. u
fprintf('\nNumber of major iterations = %5d\n',majits);
& L5 n. X& N3 C' A7 h& {; T1 [0 j) {! R- V
function [mode, ccon, cjac, user] = ...
) f" `! a! K& C. [9 ~ confun(mode, ncnln, n, ldcj, needc, x, cjac, nstate, user)8 E3 u# {) k$ H3 H% _
ccon = zeros(ncnln, 1);' ~( X1 d5 D0 u; ` ^6 z% U
if (nstate == 1)
. y' Y$ I1 A- x7 V0 D7 k0 f% first call to confun. set all jacobian elements to zero.4 m& ?, K1 }3 f4 _
% note that this will only work when 'derivative level = 3'6 D# b" [7 D8 t1 P d% J. ?" t
% (the default; see section 11.2).) V; f! |+ Q; A; c: N- x
cjac = zeros(ncnln, n);
2 P. c9 w- p8 p: S& D end
+ o/ ?1 H) V- ? if (needc(1) > 0)6 W( s1 S, j; ~- t2 `! x& b9 g
if (mode == 0 || mode == 2): r: A4 _ L$ d; E" `+ _$ E
ccon(1) = x(1)^2 + x(2)^2 + x(3)^2 + x(4)^2;
: e! N: u2 |7 m% ?) p: m* \ end
9 ?( I% {% l1 n$ J* k if (mode == 1 || mode == 2)
& W9 }8 e* Q- e4 A8 Y% O cjac(1,1) = 2*x(1);1 J" t6 a! w3 V- X
cjac(1,2) = 2*x(2);
1 [, f* |9 \& d" ^) w- w3 F( ? cjac(1,3) = 2*x(3);
6 o2 ~3 y R: K- }9 Z0 [$ Z+ K cjac(1,4) = 2*x(4);
9 u0 c) s; f( Z& F ]7 k ?1 r end% D- |3 i1 l- f. P2 F5 K
end
+ W2 W6 ]' E! i if (needc(2) > 0)4 N) J: O: X. I4 _
if (mode == 0 || mode == 2)
9 [3 d" d: q% F* H# _6 g ccon(2) = x(1)*x(2)*x(3)*x(4);; y4 K3 G" p! n. i6 A
end
- O3 v+ u' U6 Y7 i" M1 d if (mode == 1 || mode == 2)+ Q9 a, w: t* o
cjac(2,1) = x(2)*x(3)*x(4);9 ?" D ~( U4 H* q. X
cjac(2,2) = x(1)*x(3)*x(4);
& f; D6 |( i# @( k) N1 x ^ cjac(2,3) = x(1)*x(2)*x(4);
% H- S' E+ a6 W6 z, U# [& g# J4 d cjac(2,4) = x(1)*x(2)*x(3);
* }; @& Y5 ]/ T% Q end+ O' c- e2 _/ J) _. q
end
+ S4 p* j9 e- R2 p* i" @" ]7 ~# h, w- D8 L2 O9 @. w
function [mode, objf, grad, user] = objfun(mode, n, x, grad, nstate, user)
( V; Q4 p, G9 O' [! t. F if (mode == 0 || mode == 2), ~: P# J4 m! G7 e
objf = x(1)*x(4)*(x(1)+x(2)+x(3)) + x(3);
! h a9 Q3 N/ p0 Q: _3 C' `' M- K end
& B4 p( H2 h1 ~: F* b& Z if (mode == 1 || mode == 2)
& R' [$ T4 Q/ x. R# n, e; W7 s grad(1) = x(4)*(2*x(1)+x(2)+x(3));- j; x& z, m% Z! | g9 }
grad(2) = x(1)*x(4);2 ^* b2 |4 [+ K
grad(3) = x(1)*x(4) + 1;+ i Q/ U2 ^% ?2 g) ~" y/ q) i
grad(4) = x(1)*(x(1)+x(2)+x(3));
% d6 {% S. p9 O- [ end8 l" h% }6 y- k! f1 I$ w
|
|