主题:请教一下fmincon函数的使用?
headtohead
[专家分:0] 发布于 2008-06-07 23:17:00
求最小值,不知道fmincon里面 A,b,Aeq,Beq,Lb,Ub,nonlcon,options分别怎么写,谢谢
最后更新于:2008-06-07 23:20:00
回复列表 (共11个回复)
沙发
ronei [专家分:360] 发布于 2008-06-08 06:25:00
查查help文件不就都有了嘛
FMINCON finds a constrained minimum of a function of several variables.
FMINCON attempts to solve problems of the form:
min F(X) subject to: A*X <= B, Aeq*X = Beq (linear constraints)
X C(X) <= 0, Ceq(X) = 0 (nonlinear constraints)
LB <= X <= UB (bounds)
X=FMINCON(FUN,X0,A,B) starts at X0 and finds a minimum X to the function
FUN, subject to the linear inequalities A*X <= B. FUN accepts input X and
returns a scalar function value F evaluated at X. X0 may be a scalar,
vector, or matrix.
X=FMINCON(FUN,X0,A,B,Aeq,Beq) minimizes FUN subject to the linear equalities
Aeq*X = Beq as well as A*X <= B. (Set A=[] and B=[] if no inequalities exist.)
X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB) defines a set of lower and upper
bounds on the design variables, X, so that a solution is found in
the range LB <= X <= UB. Use empty matrices for LB and UB
if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below;
set UB(i) = Inf if X(i) is unbounded above.
X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON) subjects the minimization to the
constraints defined in NONLCON. The function NONLCON accepts X and returns
the vectors C and Ceq, representing the nonlinear inequalities and equalities
respectively. FMINCON minimizes FUN such that C(X)<=0 and Ceq(X)=0.
(Set LB=[] and/or UB=[] if no bounds exist.)
板凳
ronei [专家分:360] 发布于 2008-06-08 06:25:00
X=FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS) minimizes with the
default optimization parameters replaced by values in the structure
OPTIONS, an argument created with the OPTIMSET function. See OPTIMSET
for details. Used options are Display, TolX, TolFun, TolCon,
DerivativeCheck, Diagnostics, FunValCheck, GradObj, GradConstr,
Hessian, MaxFunEvals, MaxIter, DiffMinChange and DiffMaxChange,
LargeScale, MaxPCGIter, PrecondBandWidth, TolPCG, TypicalX, Hessian,
HessMult, HessPattern, PlotFcns, and OutputFcn. Use the GradObj option
to specify that FUN also returns a second output argument G that is the
partial derivatives of the function df/dX, at the point X. Use the Hessian
option to specify that FUN also returns a third output argument H that
is the 2nd partial derivatives of the function (the Hessian) at the
point X. The Hessian is only used by the large-scale method, not the
line-search method. Use the GradConstr option to specify that NONLCON
also returns third and fourth output arguments GC and GCeq, where GC is
the partial derivatives of the constraint vector of inequalities C, and
GCeq is the partial derivatives of the constraint vector of equalities
Ceq. Use OPTIONS = [] as a place holder if no options are set.
X=FMINCON(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
structure with the function FUN in PROBLEM.objective, the start point
in PROBLEM.x0, the linear inequality constraints in PROBLEM.Aineq
and PROBLEM.bineq, the linear equality constraints in PROBLEM.Aeq and
PROBLEM.beq, the lower bounds in PROBLEM.lb, the upper bounds in
PROBLEM.ub, the nonlinear constraint function in PROBLEM.nonlcon, the
options structure in PROBLEM.options, and solver name 'fmincon' in
PROBLEM.solver. Use this syntax to solve at the command line a problem
exported from OPTIMTOOL. The structure PROBLEM must have all the fields.
[X,FVAL]=FMINCON(FUN,X0,...) returns the value of the objective
function FUN at the solution X.
[X,FVAL,EXITFLAG]=FMINCON(FUN,X0,...) returns an EXITFLAG that describes the
exit condition of FMINCON. Possible values of EXITFLAG and the corresponding
exit conditions are listed below.
Both medium- and large-scale:
1 First order optimality conditions satisfied to the specified tolerance.
0 Maximum number of function evaluations or iterations reached.
-1 Optimization terminated by the output function.
Large-scale only:
2 Change in X less than the specified tolerance.
3 Change in the objective function value less than the specified tolerance.
Medium-scale only:
4 Magnitude of search direction smaller than the specified tolerance and
constraint violation less than options.TolCon.
5 Magnitude of directional derivative less than the specified tolerance
and constraint violation less than options.TolCon.
-2 No feasible point found.
3 楼
ronei [专家分:360] 发布于 2008-06-08 06:26:00
[X,FVAL,EXITFLAG,OUTPUT]=FMINCON(FUN,X0,...) returns a structure OUTPUT with
the number of iterations taken in OUTPUT.iterations, the number of function
evaluations in OUTPUT.funcCount, the norm of the final step in OUTPUT.stepsize,
the algorithm used in OUTPUT.algorithm, the first-order optimality in
OUTPUT.firstorderopt, and the exit message in OUTPUT.message. The medium scale
algorithm returns the final line search steplength in OUTPUT.lssteplength, and
the large scale algorithm returns the number of CG iterations in OUTPUT.cgiterations.
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA]=FMINCON(FUN,X0,...) returns the Lagrange multipliers
at the solution X: LAMBDA.lower for LB, LAMBDA.upper for UB, LAMBDA.ineqlin is
for the linear inequalities, LAMBDA.eqlin is for the linear equalities,
LAMBDA.ineqnonlin is for the nonlinear inequalities, and LAMBDA.eqnonlin
is for the nonlinear equalities.
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD]=FMINCON(FUN,X0,...) returns the value of
the gradient of FUN at the solution X.
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]=FMINCON(FUN,X0,...) returns the
value of the HESSIAN of FUN at the solution X.
Examples
FUN can be specified using @:
X = fmincon(@humps,...)
In this case, F = humps(X) returns the scalar function value F of the HUMPS function
evaluated at X.
FUN can also be an anonymous function:
X = fmincon(@(x) 3*sin(x(1))+exp(x(2)),[1;1],[],[],[],[],[0 0])
returns X = [0;0].
If FUN or NONLCON are parameterized, you can use anonymous functions to capture
the problem-dependent parameters. Suppose you want to minimize the objective
given in the function myfun, subject to the nonlinear constraint mycon, where
these two functions are parameterized by their second argument a1 and a2, respectively.
Here myfun and mycon are M-file functions such as
function f = myfun(x,a1)
f = x(1)^2 + a1*x(2)^2;
and
function [c,ceq] = mycon(x,a2)
c = a2/x(1) - x(2);
ceq = [];
To optimize for specific values of a1 and a2, first assign the values to these
two parameters. Then create two one-argument anonymous functions that capture
the values of a1 and a2, and call myfun and mycon with two arguments. Finally,
pass these anonymous functions to FMINCON:
a1 = 2; a2 = 1.5; % define parameters first
options = optimset('LargeScale','off'); % run medium-scale algorithm
x = fmincon(@(x)myfun(x,a1),[1;2],[],[],[],[],[],[],@(x)mycon(x,a2),options)
See also optimset, fminunc, fminbnd, fminsearch, @, function_handle.
Reference page in Help browser
doc fmincon
4 楼
headtohead [专家分:0] 发布于 2008-06-08 13:45:00
谢谢ronei,帮助我也看了,还看了一些例子,不过还是不太明白,这些约束条件该怎么组合, x1,x2一个有上边界没有下边界,一个有下边界没有上边界,不知道该怎么写啊
5 楼
ronei [专家分:360] 发布于 2008-06-09 05:03:00
结果是
x =
2.2570 0.5430
x1 = 2.257, x2= 0.543
6 楼
ronei [专家分:360] 发布于 2008-06-09 05:07:00
Make a file fun1.m containing the function to be minimized:
function y = fun1(x)
y = x(1)-0.25*x(2)+0.6;
Then make another file, such as testfmincon.m :
clc;
x0 = [0 0]; % starting point
A = [-0.038 1]; b = 0 % matrix and rhs vector for linear inequality constraint
options = optimset('Display','iter'); % show progress after each iteration
x = fmincon('fun1',x0,A,b,[],[],[-Inf 1],[1.8 Inf],[],options);
Run it
7 楼
ronei [专家分:360] 发布于 2008-06-09 05:23:00
对于你说的 x1,x2一个有上边界没有下边界,一个有下边界没有上边界,不知道该怎么写啊
没有下界,就设置下界为 -Inf, 负无穷, 没有上界,就设置上界为 Inf, 正无穷,这样就可以了。
8 楼
ronei [专家分:360] 发布于 2008-06-09 05:27:00
这个函数的基本形式为
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
其中fun为你要求最小值的函数,可以单写一个文件设置函数,如以上给的例子中。
1.如果fun中有N个变量,如x y z, 或者是X1, X2,X3, 什么的,自己排个顺序,在fun中统一都是用x(1),x(2)....x(n) 表示的。
2. x0, 表示初始的猜测值,大小要与变量数目相同
3. A b 为线性不等约束,A*x <= b, A应为n*n阶矩阵,学过线性代数应不难写出A和b
4 Aeq beq为线性相等约束,Aeq*x = beq。 Aeq beq同上可求
5 lb ub为变量的上下边界, 正负无穷用 -Inf和Inf表示, lb ub应为N阶数组
6 nonlcon 为非线性约束,可分为两部分,非线性不等约束 c,非线性相等约束,ceq
可按下面的例子设置
function [c,ce] = nonlcon1(x)
c = -x(1)+x(2)^2-4;
ce = []; % no nonlinear equality constraints
7, 最后是options, 可以用OPTIMSET函数设置,见上例
具体可见OPTIMSET函数的帮助文件。
ps: 以上x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
括号中的参数,需从左到右依次给出,可只给部分。
如可写为x = fmincon(fun,x0,A,b) x = fmincon(fun,x0,A,b,Aeq,beq) x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
如中间某些约束为空,可以用[]表示, 如可写为 x = fmincon(fun,x0,A,b,[],[],lb,ub)
9 楼
headtohead [专家分:0] 发布于 2008-06-09 13:44:00
非常感谢ronei详细的回答,不过你的结果好像不满足条件哦, 我再按您说的试试看[em2]
10 楼
ronei [专家分:360] 发布于 2008-06-09 15:57:00
1. 解答中A b的设置有误, x2/x1>= 0.038, 我直接把x1乘过去了,且正负取错了。
2. 正确解答是把这一项用非线性不等约束来做
function [c,ce] = nonlcon1(x)
c = 0.038-x(2)/x(1);
ce = []; % no nonlinear equality constraints
clc;
x0 = [0 0]; % starting point
A = [0.038 -1]; b = 0; % matrix and rhs vector for linear inequality constraint
options = optimset('Display','iter'); % show progress after each iteration
x = fmincon('fun1',x0,[],[],[],[],[-Inf; 1],[1.8; Inf],'nonlcon1',options)
结果是 x1 =0 x2= 1.0001
返回来看你要求解的函数, 直接观察可得
它的最小值应该取在 X1= 0 附近的微小区域, X2=1
但约束条件2限制X1不能为零, 所以此题无解, 或者就是 x1 =0 x2= 1.0
我来回复