所有调用格式为
x = fminsearch(fun,x0)
x = fminsearch(fun,x0,options)
x = fminsearch(problem)
[x,fval] = fminsearch(___)
[x,fval,exitflag] = fminsearch(___)
[x,fval,exitflag,output] = fminsearch(___)
options结构字段 | 说明 |
---|---|
Display | 设置结果的显示方式:off----不显示任何结果;iter----显示每步迭代结果;final----显示最后结果;notify----只有当求解不收敛时才显示结果 |
FunValCheck | 检查目标函数值是否可接受:on----当目标函数值为复数或NaN时显示出错信息;off----不显示任何错误信息 |
MaxFunEvals | 最大的目标函数检查步数 |
MaxIter | 最大的迭代步数 |
OutputFcn | 用户自定义的输出函数,它将在每个迭代步调用 |
PlotFcns | 用户自定义的函数,它将在每个迭代步调用 |
TolFun | 目标函数值的精度 |
TolX | 自变量的精度 |
定义格式例如 o p t = o p t i m s e t ( ′ T o l X ′ , 0.00001 ) opt=optimset('TolX',0.00001) opt=optimset(′TolX′,0.00001)。
problem结构字段 | 说明 |
---|---|
objective | 目标函数 |
x0 | 初始点 |
solver | 求解方法 |
options | options结构 |
exitflag结构字段 | 说明 |
---|---|
4 | 搜索方向幅值小于给定精度,约束的满足精度小于options.TolCon |
5 | 方向梯度幅值小于给定精度,约束的满足精度小于options.TolCon |
1 | 成功求得优化解 |
0 | 由于目标函数检查步数达到最大或迭代步数达到最大值而退出 |
-1 | 用户自定义函数引起的退出 |
-2 | 边界条件不协调 |
output结构字段 | 说明 |
---|---|
output.algorithm | 优化算法 |
output.funcCount | 目标函数检查步数 |
output.iterations | 优化迭代步数 |
output.message | 退出信息 |
f=@(x)-1/((x(1)-1)^2+1)-1/((x(2)-2)^2+2)
opt=optimset('TolX',0.0001)
[x,fval,exitflag,output]=fminsearch(f,[0,0],opt)
x =
1.0000 2.0000
fval =
-1.5000
exitflag =
1
output =
包含以下字段的 struct:
iterations: 73
funcCount: 140
algorithm: 'Nelder-Mead simplex direct search'
message: '优化已终止:…'
fminsearch函数求解得到自变量取值1和2的时候得到目标函数最小值-1.5,成功求得最优解,并且优化迭代步数73,目标函数检查步数140,采用的是直接法中的单纯形搜索法,如果还想得到更多信息可以扩展message查看。
所有调用格式为
x = fminunc(fun,x0)
x = fminunc(fun,x0,options)
x = fminunc(problem)
[x,fval] = fminunc(___)
[x,fval,exitflag,output] = fminunc(___)
[x,fval,exitflag,output,grad,hessian] = fminunc(___)
输出参数grad返回函数fun在极小点x处的梯度
输出参数hessian返回函数fun在极小点x处的海森矩阵
f=@(x)-1/((x(1)-3)^2+3)-1/((x(2)-4)^2+4)
pro.objective=f
pro.solver='fminunc'
pro.x0=[0 0]
pro.options=optimset('TolX',0.0001)
[x,fval,exitflag,output,grad,hessian]=fminunc(pro)
警告: Gradient must be provided for trust-region algorithm; using quasi-newton algorithm instead.
> In fminunc (line 395)
Local minimum found.
Optimization completed because the size of the gradient is less than
the default value of the optimality tolerance.
<stopping criteria details>
Computing finite-difference Hessian using objective function.
x =
3.0000 4.0000
fval =
-0.5833
exitflag =
1
output =
包含以下字段的 struct:
iterations: 9
funcCount: 48
stepsize: 1.1147e-05
lssteplength: 1
firstorderopt: 1.8626e-07
algorithm: 'quasi-newton'
message: 'Local minimum found.…'
grad =
1.0e-06 *
0.0646
0.1863
hessian =
0.2222 0
0 0.1250
最后得到的结果表明fminunc函数成功求得优化解,自变量取值3和4的时候得目标函数最小值-0.5833,采用的算法是拟牛顿法。在这儿可以看到优化开始时弹出来一个警告,这个警告是说信赖域算法需要用户提供梯度矩阵,如果没有提供则采用拟牛顿法,这个不是错误信息。
所有调用格式为
x = fminbnd(fun,x1,x2)
x = fminbnd(fun,x1,x2,options)
x = fminbnd(problem)
[x,fval] = fminbnd(___)
[x,fval,exitflag] = fminbnd(___)
[x,fval,exitflag,output] = fminbnd(___)
[x,fval,exitflag,output]=fminbnd('x^4-x^3+x^2-x+1',-10,10)
x =
0.6058
fval =
0.6736
exitflag =
1
output =
包含以下字段的 struct:
iterations: 12
funcCount: 13
algorithm: 'golden section search, parabolic interpolation'
message: '优化已终止:…'
这一例说明目标函数最小值为0.6736,在0.6058处取得最小值,成功求得优化解,采用的优化算法为黄金分割法与抛物线法结合。下一个例子如下
[x,fval,exitflag,output]=fminbnd('sin(x)+2*sin(2*x+2)+3*sin(3*x+3)',-10,10)
x =
6.9719
fval =
-2.6460
exitflag =
1
output =
包含以下字段的 struct:
iterations: 13
funcCount: 14
algorithm: 'golden section search, parabolic interpolation'
message: '优化已终止:…'
目标函数图像为
由此可见,对于包含多个极小值点的函数,fminbnd函数不能很好的确定最小值点,但是如果缩小区间,也可以得到令人满意的结果。
所有调用格式如下
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,Aeq,beq,lb,ub,nonlcon)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = fmincon(problem)
[x,fval] = fmincon(___)
[x,fval,exitflag,output] = fmincon(___)
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(___)
输出参数lambda返回解x处的拉格朗日乘子信息
接下来用一个例子简要说明,求如下方程最小值
约束条件为
因为里面包含了一个非线性不等式约束,因此需要先建立一个函数文件
function [c,ceq]=nonlcon(x)
c=9-x(1)^2-x(2)^2
ceq=[]
随后开始进行计算
f=@(x)x(1)^4-4*x(1)-8*x(2)+15
x0=[0 0]
A=[2 3;-1 1]
b=[2;5]
[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(f,x0,A,b,[],[],[],[],@nonlcon)
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>
x =
-2.1454 2.0969
fval =
27.9921
exitflag =
1
output =
包含以下字段的 struct:
iterations: 14
funcCount: 52
constrviolation: 0
stepsize: 4.8432e-10
algorithm: 'interior-point'
firstorderopt: 2.0000e-06
cgiterations: 0
message: 'Local minimum found that satisfies the constraints.…'
lambda =
包含以下字段的 struct:
eqlin: [0×1 double]
eqnonlin: [0×1 double]
ineqlin: [2×1 double]
lower: [2×1 double]
upper: [2×1 double]
ineqnonlin: 5.3856
grad =
-43.4999
-8.0000
hessian =
45.5235 0.0294
0.0294 0.0000
表明fmincon函数成功求得优化解,迭代步长为4.8432e-10,优化算法为内点罚函数法。
fminimax函数解决如下问题
对每个定义域中的向量x,向量函数都存在一个值最大的分量,但是随着向量x的取值不同,值最大的分量也会发生变化,当把分量的值记录下来,找到最小值,就是fminimax的任务。它的所有调用格式如下
x = fminimax(fun,x0)
x = fminimax(fun,x0,A,b)
x = fminimax(fun,x0,A,b,Aeq,beq)
x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub)
x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
x = fminimax(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = fminimax(problem)
[x,fval] = fminimax(...)
[x,fval,maxfval] = fminimax(...)
[x,fval,maxfval,exitflag] = fminimax(...)
[x,fval,maxfval,exitflag,output] = fminimax(...)
[x,fval,maxfval,exitflag,output,lambda] = fminimax(...)
f=@(x)[x(1)*sin(x(2));1-x(1)*x(2)]
[x,fval,maxfval,exitflag,output]=fminimax(f,[0.5,0.5],[],[],[],[],[0 0],[1 1])
Local minimum possible. Constraints satisfied.
fminimax stopped because the size of the current search direction is less than
twice the default value of the step size tolerance and constraints are
satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
x =
0.5430 1.0000
fval =
0.4570
0.4570
maxfval =
0.4570
exitflag =
4
output =
包含以下字段的 struct:
iterations: 6
funcCount: 30
lssteplength: 1
stepsize: 9.7601e-12
algorithm: 'active-set'
firstorderopt: []
constrviolation: 5.5511e-17
message: 'Local minimum possible. Constraints satisfied.…'
结果表明搜索方向幅值小于给定精度,自变量取值0.543和1,此时最小最大值为0.4570,最大值为0.4570,优化算法为活跃集算法。
x = fgoalattain(fun,x0,goal,weight)
x = fgoalattain(fun,x0,goal,weight,A,b)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = fgoalattain(problem)
[x,fval] = fgoalattain(...)
[x,fval,attainfactor] = fgoalattain(...)
[x,fval,attainfactor,exitflag] = fgoalattain(...)
[x,fval,attainfactor,exitflag,output] = fgoalattain(...)
[x,fval,attainfactor,exitflag,output,lambda] = fgoalattain(...)
goal是目标函数的实现目标
weight是表征目标函数的重要性,值一般为正数,表示目标函数小于目标值,负数表示大于目标值,很少用
具体用法还不是很清楚,后续更新。
x = linprog(f,A,b)
x = linprog(f,A,b,Aeq,beq)
x = linprog(f,A,b,Aeq,beq,lb,ub)
x = linprog(f,A,b,Aeq,beq,lb,ub,options)
x = linprog(problem)
[x,fval] = linprog(___)
[x,fval,exitflag,output] = linprog(___)
[x,fval,exitflag,output,lambda] = linprog(___)
f=[1;1;1]
Aeq=[1 2 3;2 1 5]
beq=[15;20]
[x,fval,exitflag,output]=linprog(f,[],[],Aeq,beq,[0;0;0],[])
警告: Your current settings will run a different algorithm ('dual-simplex') in a future release.
> In linprog (line 204)
Optimization terminated.
x =
0.0000
2.1429
3.5714
fval =
5.7143
exitflag =
1
output =
包含以下字段的 struct:
iterations: 6
algorithm: 'interior-point-legacy'
cgiterations: 0
message: 'Optimization terminated.'
constrviolation: 3.5527e-15
firstorderopt: 1.8794e-09
这个是混合整数线性规划函数,它和linprog函数的求解问题形式一样,不同的只是它里面有一些自变量要求是整数,它的所有调用格式为
x = intlinprog(f,intcon,A,b)
x = intlinprog(f,intcon,A,b,Aeq,beq)
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub)
x = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub,options)
x = intlinprog(problem)
[x,fval,exitflag,output] = intlinprog(___)
输入参数intcon代表了整数型自变量所在的位置
f=[-3;-4]
A=[1 1;5 8]
b=[5;32]
[x,fval,exitflag,output]=intlinprog(f,[1 2],A,b)
LP: Optimal objective value is -17.333333.
Heuristics: Found 1 solution using rounding.
Upper bound is -14.000000.
Relative gap is 20.00%.
Heuristics: Found 2 solutions using rss.
Upper bound is -17.000000.
Relative gap is 0.00%.
Optimal solution found.
Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value,
options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
x =
3.0000
2.0000
fval =
-17
exitflag =
1
output =
包含以下字段的 struct:
relativegap: 0
absolutegap: 0
numfeaspoints: 3
numnodes: 0
constrviolation: 8.8818e-16
message: 'Optimal solution found.…'
在整数规划中还有一个特殊的0–1规划,也就是自变量只能取0或者1,其实就是整数规划函数的上下界定为0和1就好了,看下例
f=[-3;-4;-5;-6]
A=[1 1 1 1;5 8 0 -4;3 0 10 -5]
b=[3;7;7]
[x,fval,exitflag,output]=intlinprog(f,[1 2 3 4],A,b,[],[],[0;0;0;0],[1;1;1;1])
LP: Optimal objective value is -12.000000.
Optimal solution found.
Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value,
options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
x =
0
1
0
1
fval =
-10
exitflag =
1
output =
包含以下字段的 struct:
relativegap: 0
absolutegap: 0
numfeaspoints: 1
numnodes: 0
constrviolation: 0
message: 'Optimal solution found.…'
二次规划所解决的其实就是非线性方程当中的二次方程问题,一般形式如下
它的所有调用格式如下
x = quadprog(H,f)
x = quadprog(H,f,A,b)
x = quadprog(H,f,A,b,Aeq,beq)
x = quadprog(H,f,A,b,Aeq,beq,lb,ub)
x = quadprog(H,f,A,b,Aeq,beq,lb,ub,x0)
x = quadprog(H,f,A,b,Aeq,beq,lb,ub,x0,options)
x = quadprog(problem)
[x,fval] = quadprog(H,f,...)
[x,fval,exitflag] = quadprog(H,f,...)
[x,fval,exitflag,output] = quadprog(H,f,...)
[x,fval,exitflag,output,lambda] = quadprog(H,f,...)
输入参数H代表二次项矩阵
输入参数f代表一次项向量
H=[6 -4;-4 4]
f=[3;-4]
A=[2 1;-1 2]
b=[4;4]
[x,fval,exitflag,output,lambda]=quadprog(H,f,A,b,[],[],[0;0],[])
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>
x =
0.5000
1.5000
fval =
-2.2500
exitflag =
1
output =
包含以下字段的 struct:
message: 'Minimum found that satisfies the constraints.…'
algorithm: 'interior-point-convex'
firstorderopt: 1.3589e-09
constrviolation: 0
iterations: 4
cgiterations: []
lambda =
包含以下字段的 struct:
ineqlin: [2×1 double]
eqlin: [0×1 double]
lower: [2×1 double]
upper: [2×1 double]
它是解决如下形式的问题
我们事先已经获得xdata和ydata的数据,并且它们之间存在 y d a t a = F ( x , x d a t a ) ydata=F(x,xdata) ydata=F(x,xdata)的关系,但是并不知道系数x的值,这个函数就是为了解决系数的取值问题,依据就是残差平方和最小原理,相当于黑箱问题。它的所有调用格式为
x = lsqcurvefit(fun,x0,xdata,ydata)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
x = lsqcurvefit(problem)
[x,resnorm] = lsqcurvefit(___)
[x,resnorm,residual,exitflag,output] = lsqcurvefit(___)
[x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(___)
输出参数resnorm为残差平方和
输出参数residual为残差
输出参数jacobian为最优解处的雅可比矩阵
接下来用一个例子简单说明,已知输入输出数据,求解如下方程的系数
xdata=[0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3]
ydata=[455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5]
f=@(x,xdata)x(1)*exp(x(2)*xdata)
x0=[100 -1]
[x,resnorm,residual,exitflag,output]=lsqcurvefit(f,x0,xdata,ydata)
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
<stopping criteria details>
x =
498.8309 -0.1013
resnorm =
9.5049
residual =
0.1817 -0.0610 -0.7628 -0.1196 0.2659 0.5979 1.0261 1.5124 1.5615 1.6327
exitflag =
3
output =
包含以下字段的 struct:
firstorderopt: 0.0114
iterations: 28
funcCount: 87
cgiterations: 0
algorithm: 'trust-region-reflective'
stepsize: 4.6226e-04
message: 'Local minimum possible.…'
exitflag为3表示求得给定精度的解,这个精度就是residual,采用的是信赖域反射算法,最后结果得到为
为了更加直观的显示模型拟合结果,显示图像为
它是解决如下问题的
它同样可以用来进行模型拟合,它的所有调用格式如下
x = lsqnonlin(fun,x0)
x = lsqnonlin(fun,x0,lb,ub)
x = lsqnonlin(fun,x0,lb,ub,options)
x = lsqnonlin(problem)
[x,resnorm] = lsqnonlin(___)
[x,resnorm,residual,exitflag,output] = lsqnonlin(___)
[x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqnonlin(___)
f=@(x)[sin(x(1)+x(2)+3);1/(-(x(1)-2)^2+3);exp(x(1))+exp(2*x(2))]
[x,resnorm,residual,exitflag,output] = lsqnonlin(f,[0 0])
Local minimum found.
Optimization completed because the size of the gradient is less than
the default value of the optimality tolerance.
<stopping criteria details>
x =
-1.8365 -1.2080
resnorm =
0.0711
residual =
-0.0444
-0.0853
0.2487
exitflag =
1
output =
包含以下字段的 struct:
firstorderopt: 2.6874e-07
iterations: 25
funcCount: 78
cgiterations: 0
algorithm: 'trust-region-reflective'
stepsize: 1.1604e-05
message: 'Local minimum found.…'
它主要是解决如下形式问题
已知C和d求解x,它的所有调用格式如下
x = lsqlin(C,d,A,b)
x = lsqlin(C,d,A,b,Aeq,beq,lb,ub)
x = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options)
x = lsqlin(problem)
[x,resnorm,residual,exitflag,output,lambda] = lsqlin(___)
接下来用一个例子简单说明
C = [0.9501 0.7620 0.6153 0.4057
0.2311 0.4564 0.7919 0.9354
0.6068 0.0185 0.9218 0.9169
0.4859 0.8214 0.7382 0.4102
0.8912 0.4447 0.1762 0.8936]
d = [0.0578
0.3528
0.8131
0.0098
0.1388]
A = [0.2027 0.2721 0.7467 0.4659
0.1987 0.1988 0.4450 0.4186
0.6037 0.0152 0.9318 0.8462]
b = [0.5251
0.2026
0.6721]
[x,resnorm,residual,exitflag,output] = lsqlin(C,d,A,b)
警告: The trust-region-reflective algorithm can handle bound constraints only;
using active-set algorithm instead. In a future release, for this problem lsqlin will switch to the
interior-point algorithm.
> In lsqlin (line 308)
Optimization terminated.
x =
0.1299
-0.5757
0.4251
0.2438
resnorm =
0.0176
residual =
-0.0126
-0.0208
-0.1295
-0.0057
0.0137
exitflag =
1
output =
包含以下字段的 struct:
iterations: 3
constrviolation: 0
algorithm: 'active-set'
message: 'Optimization terminated.'
firstorderopt: 5.5511e-17
cgiterations: []
它所求解的问题形式和上面一样,只不过此时它的自变量不能为负数,而且没有约束限制,它的所有调用格式如下
x = lsqnonneg(C,d)
x = lsqnonneg(C,d,options)
x = lsqnonneg(problem)
[x,resnorm,residual] = lsqnonneg(___)
[x,resnorm,residual,exitflag,output] = lsqnonneg(___)
[x,resnorm,residual,exitflag,output,lambda] = lsqnonneg(___)
接下来举个简单例子
C = [0.0372 0.2869
0.6861 0.7071
0.6233 0.6245
0.6344 0.6170];
d = [0.8587
0.1781
0.0747
0.8405];
>> [x,resnorm,residual,exitflag,output] = lsqnonneg(C,d)
x =
0
0.6929
resnorm =
0.8315
residual =
0.6599
-0.3119
-0.3580
0.4130
exitflag =
1
output =
包含以下字段的 struct:
iterations: 1
algorithm: 'active-set'
message: '已终止优化。'
它所求解的是 F ( x ) = 0 F(x)=0 F(x)=0这一类非线性方程组零点形式,它的所有调用格式如下
x = fsolve(fun,x0)
x = fsolve(fun,x0,options)
x = fsolve(problem)
[x,fval] = fsolve(___)
[x,fval,exitflag,output] = fsolve(___)
[x,fval,exitflag,output,jacobian] = fsolve(___)
接下来举个简单例子
f=@(x)[exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5]
[x,fval,exitflag,output] = fsolve(f,[0 0])
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
x =
0.3532 0.6061
fval =
1.0e-06 *
-0.2407
-0.0383
exitflag =
1
output =
包含以下字段的 struct:
iterations: 4
funcCount: 15
algorithm: 'trust-region-dogleg'
firstorderopt: 2.0232e-07
message: 'Equation solved.…
它求解非线性方程零点,它的所有调用格式如下
x = fzero(fun,x0)
x = fzero(fun,x0,options)
x = fzero(problem)
[x,fval,exitflag,output] = fzero(___)
接下来举一个简单例子
f=@(x)exp(x)-x^2
[x,fval,exitflag,output] = fzero(f,0)
x =
-0.7035
fval =
0
exitflag =
1
output =
包含以下字段的 struct:
intervaliterations: 11
iterations: 6
funcCount: 28
algorithm: 'bisection, interpolation'
message: '在区间 [-0.905097, 0.64] 中发现零'