用于求解一元非线性规划问题。其调用格式:
x=fminbnd(fun,x1,x2) 返回目标函数f(x)在区间(x1,x2)上的极小值。
x=fminbnd(fun,x1,x2,option); option为指定优化参数选项。
[x,fval]=fminbnd(…) 除返回极小值x外,还得相应的目标函数值fval。
[x,fval,exitflag]=fminbnd(…) 下面是关于exitflag的说明:
exitflag | 说明 |
---|---|
1 | 表示函数收敛到最优解 |
0 | 表示达到函数的最大估计值或者迭代次数 |
-1 | 表示算法被输出函数终止 |
-2 | 表示输入的区间有误,即a>b |
output | 说明 |
---|---|
iterations | 迭代次数 |
funcCount | 函数赋值次数 |
algorithm | 函数所调用的算法 |
message | 算法终止的信息 |
例1:求下面函数在区间(1,5)内的最小值。 f ( x ) = sin ( x ) x 2 + 3 x cos x f(x)=\frac{\sin (x)}{x^{2}}+3 x \cos x f(x)=x2sin(x)+3xcosx
clear all;
%方法一
fun=inline('sin(x)/x^2+3*x*cos(x)')
[x,fval,exitflag,output]=fminbnd(fun,1,5)
%方法二
[x,fval,exitflag,output]=fminbnd('sin(x)/x^2+3*x*cos(x)',1,5)
%方法三
[x,fval,exitflag,output]=fminbnd(@(x)sin(x)/x^2+3*x*cos(x),1,5)
%运行结果
fun = 内联函数:
fun(x) = sin(x)/x^2+3*x*cos(x)
x = 3.4314
fval = -9.8892
output =
包含以下字段的 struct:
iterations: 8
funcCount: 9
algorithm: 'golden section search, parabolic interpolation'
message: '优化已终止:↵ 当前的 x 满足使用 1.000000e-04 的 OPTIONS.TolX 的终止条件↵'
用于求解非线性多元规划问题,其问题表述如下:
min f ( x ) st. A x < = b Aeq.x = b e q C ( x ) < = 0 Ceq ( x ) = 0 l b < = x < = u b \begin{array}{l} \min f(x) \\ \text { st. } \\ A x<=b \\ \text { Aeq.x }=b e q \\ C(x)<=0 \\ \operatorname{Ceq}(x)=0 \\ l b<=x<=u b \end{array} minf(x) st. Ax<=b Aeq.x =beqC(x)<=0Ceq(x)=0lb<=x<=ub
function [c1,c2,gc1,gc2]=nonlcon(x)
c1=...
c2=...
if nargout>2
gc1=...
gc2=...
end
%非线性约束条件
function [c,ceq]=nonlcon(x)
c=x(1)-[x(2)-2].^2; %不等式约束
ceq=[]; %等式约束
%运行情况
clear all;
fun='sin(x(1)^2-3*x(2))+exp(-x(1)^2)*cos(x(2))';
x0=[1.2,1];
A=[-1 2];
b=1;
[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(fun,x0,A,b,[],[],[],[],@nonlcon)
%运行结果:
x =1.1398 0.9324
fval = -0.8348
exitflag = 1
output =
包含以下字段的 struct:
iterations: 9
funcCount: 30
constrviolation: 0
stepsize: 4.1710e-06
algorithm: 'interior-point'
firstorderopt: 4.6284e-07
cgiterations: 0
message: 'Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the default value of the optimality tolerance,↵and constraints are satisfied to within the default value of the constraint tolerance.↵↵Stopping criteria details:↵↵Optimization completed: The relative first-order optimality measure, 4.628370e-07,↵is less than options.OptimalityTolerance = 1.000000e-06, and the relative maximum constraint↵violation, 0.000000e+00, is less than options.ConstraintTolerance = 1.000000e-06.↵↵Optimization Metric Options↵relative first-order optimality = 4.63e-07 OptimalityTolerance = 1e-06 (default)↵relative max(constraint violation) = 0.00e+00 ConstraintTolerance = 1e-06 (default)'
lambda =
包含以下字段的 struct:
eqlin: [0×1 double]
eqnonlin: [0×1 double]
ineqlin: 7.2833e-08
lower: [2×1 double]
upper: [2×1 double]
ineqnonlin: 0.2048
grad =
-0.2048
-0.4372
hessian =
6.0242 -6.4486
-6.4486 8.4952