优化(Optimization)
单变量优化
fminbnd - 查找单变量函数在定区间上的最小值
fminbnd 是一个一维最小值,用于求由以下条件指定的问题的最小值:
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(___)
输入参数
fun - 要计算最小值的函数
函数句柄 | 函数名称
x1 - 下界
有限实数标量
x2 - 上界
有限实数标量
options - 优化选项
结构体,例如 optimset 返回的结构体
problem - 问题结构体
结构体
输出参数
x - 解
实数标量
fval - 解处的目标函数值
实数
exitflag - fminbnd 停止的原因
整数
output - 有关优化过程的信息
结构体
eg1.简单线搜索

fcn = @sin ;
x1 = 0;
x2 = 2*pi;
[x,fval,exitflag,output] = fminbnd(fcn,x1,x2)
x =
4.7124
fval =
-1.0000
exitflag =
1
output =
包含以下字段的 struct:
iterations: 8
funcCount: 9
algorithm: 'golden section search, parabolic interpolation'
message: '优化已终止:↵ 当前的 x 满足使用 1.000000e-04 的 OPTIONS.TolX 的终止条件↵'
eg2.复杂函数线搜索

x1 = 1;
x2 = 3;
x = fminbnd(@fcn01,x1,x2)
x =
2.0061
这里的x即为所求的极值点。
function f = fcn01(x)
k = -10:10;
f = sum((k+1).^2.*cos(k.* x).*exp(-k.^2./2));
eg3.附加可变参数

objfcn = @(x,a) sin(x-a) ;
a = 9/7;
x1 = 1;
x2 = 2*pi;
[x,fval] = fminbnd(@(x)objfcn(x,a),x1,x2)
x =
5.9981
fval =
-1.0000
optimset 为四个 MATLAB® 优化求解器设置选项:fminbnd、fminsearch、fzero 和 lsqnonneg。
使用optimset进行优化
MaxFunEvals - 函数计算的最大次数,对于fminbnd默认500
Display - 显示级别 其中'iter' - 在每次迭代时显示输出(不适用于 lsqnonneg)
对可选的选项优化
objfcn = @(x,a) sin(x-a) ;
a = 9/7;
x1 = 1;
x2 = 2*pi;
options = optimset("Display","iter","MaxFunEvals",20);
[x,fval] = fminbnd(@(x)objfcn(x,a),x1,x2,options)
Func-count x f(x) Procedure
1 3.018 0.986989 initial
2 4.26519 0.16141 golden
3 5.03599 -0.571791 golden
4 5.51238 -0.884337 golden
5 5.8068 -0.981757 golden
6 6.05174 -0.998562 parabolic
7 5.99981 -0.999999 parabolic
8 5.99804 -1 parabolic
9 5.9981 -1 parabolic
10 5.99814 -1 parabolic
优化已终止:
当前的 x 满足使用 1.000000e-04 的 OPTIONS.TolX 的终止条件
x =
5.9981
fval =
-1.0000
fminbnd计算的函数要是连续的,只能给出局部解
当解在区间边界上时,fminbnd可能表现出慢收敛
fminbnd算法是基于黄金分割搜索和抛物线插值方法

无约束优化
在matlab里支持fminuc、fminsearch
fminunc - 求无约束多变量函数的最小值
非线性规划求解器。
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(___)
输入参数
fun - 要计算最小值的函数
函数句柄 | 函数名称
x0 - 初始点
实数向量 | 实数数组
options - 优化选项
optimoptions 的输出 | 结构体,例如 optimset 返回的结构体
problem - 问题结构体
结构体
输出参数
x - 解
实数向量 | 实数数组
fval - 解处的目标函数值
实数
exitflag - fminunc 停止的原因
整数
output - 有关优化过程的信息
结构体
grad - 解处的梯度
实数向量
hessian - 逼近 Hessian 矩阵
实矩阵
fminsearch - 使用无导数法计算无约束的多变量函数的最小值
非线性规划求解器。搜索由以下公式指定的问题的最小值:
x = fminsearch(fun,x0)
x = fminsearch(fun,x0,options)
x = fminsearch(problem)
[x,fval] = fminsearch(___)
[x,fval,exitflag] = fminsearch(___)
[x,fval,exitflag,output] = fminsearch(___)
输入参数
fun - 要计算最小值的函数
函数句柄 | 函数名称
x0 - 初始点
实数向量 | 实数数组
options - 优化选项
结构体,例如 optimset 返回的结构体
problem - 问题结构体
结构体
输出参数
x - 解
实数向量 | 实数数组
fval - 解处的目标函数值
实数
exitflag - fminsearch 停止的原因
整数
output - 有关优化过程的信息
结构体
eg1.Simple Function

myfun = @(x) x(1)^2+x(2)^2 ;
x0 = [-1;2];
options = optimset("MaxFunEvals",100,"Display","iter") ;
[x,fval,exitflag,output] = fminunc(myfun,x0,options)
Iteration Func-count f(x) Step-size optimality
0 3 5 4
1 6 1.25 0.25 2
2 9 4.996e-16 1 5.96e-08
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
<stopping criteria details>
x =
1.0e-07 *
0.2235
0.0000
fval =
4.9960e-16
exitflag =
1
output =
包含以下字段的 struct:
iterations: 2
funcCount: 9
stepsize: 1.1180
lssteplength: 1
firstorderopt: 5.9605e-08
algorithm: 'quasi-newton'
message: 'Local minimum found.
Optimization completed because the size of the gradient is less than↵the value of the optimality tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The first-order optimality measure, 1.192093e-08, is less ↵than options.OptimalityTolerance = 1.000000e-06.'
eg2 Rosenbrock函数
在数学最优化中,Rosenbrock函数是一个用来测试最优化算法性能的非凸函数,由Howard Harry Rosenbrock在1960年提出 。也称为Rosenbrock山谷或Rosenbrock香蕉函数,也简称为香蕉函数。(摘自百度百科)

myfun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
x0 = [0;0];
options = optimset("MaxFunEvals",500,"Display","iter","TolX",1e-14);
[x,fval,flag] = fminunc(myfun,x0,options)
Iteration Func-count f(x) Step-size optimality
0 3 1 2
1 12 0.771192 0.0817341 5.34
2 15 0.610658 1 6.73
3 18 0.522451 1 7.11
4 24 0.261629 0.7075 1.88
5 30 0.248996 0.5 3.44
6 33 0.207486 1 2.94
7 36 0.125351 1 1.5
8 39 0.0893498 1 3.93
9 42 0.0308666 1 1.23
10 48 0.0200762 0.322023 1.95
11 51 0.0138484 1 1.57
12 54 0.0044155 1 0.303
13 60 0.00268685 0.5 1.14
14 63 0.000276528 1 0.28
15 66 4.2104e-05 1 0.122
16 69 1.37272e-06 1 0.00796
17 72 7.48542e-07 1 0.0303
18 75 3.34562e-09 1 0.00221
19 78 6.71604e-11 1 7.22e-05
First-order
Iteration Func-count f(x) Step-size optimality
20 81 1.94742e-11 1 1.06e-06
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
<stopping criteria details>
x =
1.0000
1.0000
fval =
1.9474e-11
flag =
1
fminunc针对的是可以微分的连续函数,使用的算法Quasi-Newton 、Trust Region Algorithms,它可以提供Hessian矩阵。
myfun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
x0 = [0;0];
options = optimset("PlotFcns","optimplotfval","MaxFunEvals",500,"Display","iter","TolX",1e-14,"TolFun",1e-14);
[x,fval,flag] = fminsearch(myfun,x0,options)

Iteration Func-count min f(x) Procedure
0 1 1
1 3 0.9995 initial simplex
2 4 0.9995 reflect
3 6 0.998515 expand
4 8 0.998001 expand
5 10 0.995798 expand
6 12 0.993647 expand
7 14 0.988291 expand
8 16 0.981289 expand
9 18 0.967873 expand
10 20 0.947913 expand
11 22 0.918174 expand
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
148 279 4.53053e-28 contract inside
149 281 4.53053e-28 contract outside
150 283 2.92914e-28 contract inside
151 285 1.90115e-28 contract inside
152 287 4.44227e-29 contract inside
153 288 4.44227e-29 reflect
154 290 4.44227e-29 contract inside
155 292 1.59744e-29 contract inside
156 294 5.71924e-30 contract inside
157 296 5.71924e-30 contract inside
优化已终止:
当前的 x 满足使用 1.000000e-14 的 OPTIONS.TolX 的终止条件,
F(X) 满足使用 1.000000e-14 的 OPTIONS.TolFun 的收敛条件
x =
1.0000
1.0000
fval =
5.7192e-30
flag =
1
从函数使用来看,fminsearch可用的参数设置远多于fminunc,所以有时会取得更好的效果,但它需要的计算和迭代过程远超过fminunc。
fminsearch仅对实数取最小值,及向量或数组x只能由实数组成,且f(x)必须只返回实数,当x具有复数值时,将x拆分为实部和虚部。
fminsearch使用lagarias等单纯形搜索法,可解决不可微分问题或者具有不连续问题。

约束优化问题
fmincon - 寻找约束非线性多变量函数的最小值
非线性规划求解器。
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(___)
输入参数
fun - 要计算最小值的函数
函数句柄 | 函数名称
x0 - 初始点
实数向量 | 实数数组
A - 线性不等式约束
实矩阵
b - 线性不等式约束
实数向量
Aeq - 线性等式约束
实矩阵
beq - 线性等式约束
实数向量
lb - 下界
实数向量 | 实数数组
ub - 上界
实数向量 | 实数数组
nonlcon - 非线性约束
函数句柄 | 函数名称
options - 优化选项
optimoptions 的输出 | 结构体,例如 optimset 返回的结构体
problem - 问题结构体
结构体
输出参数
x - 解
实数向量 | 实数数组
fval - 解处的目标函数值
实数
exitflag - fmincon 停止的原因
整数
output - 有关优化过程的信息
结构体
lambda - 解处的拉格朗日乘数
结构体
grad - 解处的梯度
实数向量
hessian - 逼近 Hessian 矩阵
实矩阵
fseminf - 求解半无限约束多变量非线性函数的最小值
fseminf 是非线性规划求解器,用于求由下式指定的问题的最小值
x = fseminf(fun,x0,ntheta,seminfcon)
x = fseminf(fun,x0,ntheta,seminfcon,A,b)
x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq)
x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub)
x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub,options)
x = fseminf(problem)
[x,fval] = fseminf(___)
[x,fval,exitflag,output] = fseminf(___)
[x,fval,exitflag,output,lambda] = fseminf(___)
输入参数
fun - 要计算最小值的函数
函数句柄 | 函数名称
x0 - 初始点
实数向量 | 实数数组
ntheta - 半无限约束的数量
正整数
seminfcon - 计算非线性约束和半无限约束的函数
函数句柄 | 函数名称
A - 线性不等式约束
实矩阵
b - 线性不等式约束
实数向量
Aeq - 线性等式约束
实矩阵
beq - 线性等式约束
实数向量
lb - 下界
实数向量 | 实数数组
ub - 上界
实数向量 | 实数数组
options - 优化选项
optimoptions 的输出 | 结构体,例如 optimset 返回的结构体
problem - 问题结构体
结构体
输出参数
x - 解
实数向量 | 实数数组
fval - 解处的目标函数值
实数
exitflag - fseminf 停止的原因
整数
output - 有关优化过程的信息
结构体
lambda - 解处的拉格朗日乘数
结构体
eg1.

myfun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
A = [1,2];
b = 1;
x0 = [0;0];
[x,fval,flag] = fmincon(myfun,x0,A,b)
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
<stopping criteria details>
x =
0.5022
0.2489
fval =
0.2489
flag =
1
eg2.线性约束

myfun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
A = [1,2];
b = 1;
Aeq = [1,1];
beq = 1;
x0 = [0;0];
[x,fval,flag] = fmincon(myfun,x0,A,b,Aeq,beq)
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
<stopping criteria details>
x =
1.0000
-0.0000
fval =
100.0000
flag =
1
eg3、无线性约束,箱集

myfun = @(x) 1+x(1)/(1+x(2))-3*x(1)*x(2)+x(2)*(1+x(1));
lb = [0;0];
ub = [1;2];
x0 = [0;0];
[x,fval,flag] = fmincon(myfun,x0,[],[],[],[],lb,ub)
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
<stopping criteria details>
x =
1.0000
2.0000
fval =
-0.6667
flag =
1
eg4、全约束

myfun = @(x) x(1)^2+x(2)^2-2*x(1)*x(2)+x(3)^2+x(4)^2;
A = [1 1 1 0];
b = 6;
Aeq=[1 1 1 0;1 5 0 1];
beq = [3;6];
lb = zeros(4,1);
ub = [];
x0 = zeros(4,1);
[x,fval,flag] = fmincon(myfun,x0,A,b,Aeq,beq,lb,ub,@fcons01)
function [c,ceq] = fcons01(x)
c = x(1)^2+x(2)^2+x(3)^2-5;
ceq = x(2)^2+x(3)^2+x(4)^2-4;
x =
0.6086
0.8837
1.5076
0.9727
fval =
3.2947
flag =
1
最后感谢西南科技大学龙强老师的分享
