官方文档有例子,用到哪个根据目录检索,再官方文档查阅
求解一个线性不等式、线性等式和边界的简单线性规划。
调用格式为:
[x,fval] =linprog(f,A,b,Aeq,beq,lb,ub)
这里fval 返回目标函数的值,lb 和ub分别是变量x 的下界和上界. 在 Matlab 指令窗运行 help linprog 可以看到所有的函数调用形式.官方文档
clear
clc
f=-1*[1,1.65,2.4,1,1.65,2.4,0,0,0,0,0,0,0,0,0];
intcon=[1:15];
A=[5,10,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,7,9,12,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,6,8,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,4,0,11,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,7,0,0];
b=[6000;10000;4000;7000;4000];
Aeq=[1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0;
0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0;
0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1];
beq=[0,0,0];
lb=zeros(15,1);
ub=[Inf;Inf;0;Inf;Inf;Inf;Inf;Inf;0;Inf;0;Inf;Inf;0;0];
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
-1*fval-300-321-250-783-200
结果:
LP: Optimal objective value is -3032.696552.
Cut Generation: Applied 1 strong CG cut.
Lower bound is -3032.600000.
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 =
1.0e+03 *
1.2000
0
0
0.2300
0.5000
0.3240
0
0.5000
0
0.8590
0
0.3240
0.5710
0
0
ans =
1.1786e+03
clear
clc
f=[log(0.9),log(0.92),log(0.8),log(0.84),log(0.85),log(0.88),log(0.75),log(0.8)];
intcon=(1:8);
A=[450/2+200+450/4,450/3+200+450/4,480/2+200+480/4,480/3+200+480/4,540/2+200+540/4,540/3+200+540/4,600/2+200+600/4,600/3+200+600/4;
1,0,1,0,1,0,1,0;
0,1,0,1,0,1,0,1];
b=[48000;48;32];
lb=zeros(8,1);
ub=[Inf,Inf,Inf,Inf,Inf,Inf,Inf,Inf];
Aeq=[];
beq=[];
[x,fval,exitflag,output] = intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
1-exp(fval)
结果
LP: Optimal objective value is -20.624761.
Cut Generation: Applied 1 strong CG cut, 3 Gomory cuts,
and 1 mir cut.
Lower bound is -20.560232.
Heuristics: Found 1 solution using rounding.
Upper bound is -20.309430.
Relative gap is 1.18%.
Branch and Bound:
nodes total num int integer relative
explored time (s) solution fval gap (%)
12 0.02 2 -2.053257e+01 1.272330e-01
28 0.02 3 -2.054832e+01 4.449189e-02
40 0.02 3 -2.054832e+01 0.000000e+00
Optimal solution found.
Intlinprog stopped 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
0
1
1
0
0
46
31
ans =
1.0000
clear
clc
f=-1*[40,90];
intcon=(1:2);
A=[9,7;-7,-20];
b=[56;-70];
Aeq=[];
beq=[];
lb=zeros(2,1);
ub=[Inf;Inf];
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
-1*fval
结果:
LP: Optimal objective value is -720.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
8
ans =
720
clear
clc
f=-1*[3,-2,5];
intcon=(1:3);
A=[1,2,-1;
1,4,1;
1,1,0;
0,4,1];
b=[2;4;3;6];
Aeq=[];
beq=[];
lb=zeros(3,1);
ub=[Inf;Inf;Inf];
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
-1*fval
结果
LP: Optimal objective value is -20.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
0
4
ans =
20
clear
clc
c=input('输入各井的钻探费用:');
f=c;
intcon=(1:10);
A=[0,0,0,0,1,1,1,1,0,0];
b=[2];
Aeq=[1,1,1,1,1,1,1,1,1,1;
1/2,0,0,0,0,0,1/2,0,1,0;
0,0,1,0,1,0,0,0,0,0;
0,0,0,1,1,0,0,0,0,0];
beq=[5;1;1;1];
lb=zeros(10,1);
ub=ones(10,1);
[x,fval,exitflag,output]= intlinprog(f,intcon,A,b,Aeq,beq,lb,ub);
x
fval
结果
输入各井的钻探费用:ones(1,10)
LP: Optimal objective value is 5.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.0000
1.0000
1.0000
0
0
0
0
1.0000
1.0000
fval =
5.0000
fminbnd是一个函数文件。该算法基于黄金分割搜索和抛物线插值。可以求解一元的可导函数的最小值
限制
官方文档
fminsearch 使用 Lagarias 等的单纯形搜索方法。[1]这是一种直接搜索方法,不像在 fminunc 中那样使用数值或解析梯度。
提示
fminsearch 仅对实数求最小值,即向量或数组 x 只能由实数组成,并且 f(x) 必须只返回实数。当 x 具有复数值时,将 x 拆分为实部和虚部。
使用 fminsearch 解决不可微分的问题或者具有不连续性的问题,尤其是在解附近没有出现不连续性的情况下。
官方文档
官方文档
官方文档
clc
clear
fun = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
A = [];
b = [];
Aeq = [];
beq = [];
lb = -30*ones(2,1);
ub = 30*ones(2,1);
nonlcon = [];
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf,'MaxGenerations',800);
[x,fval,exitflag,output] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
0.9893 0.9788
fval =
1.1468e-04
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'boundconstraints'
rngstate: [1×1 struct]
generations: 473
funccount: 23700
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.'
maxconstraint: 0
options的修改可以参考
function f=Fun(x)
f1=0;
f2=1;
for i=1:30
f1=f1 +1/4000*x(i)^2;
f2=f2*cos(x(i)/sqrt(i));
end
f=f1-f2+1;
end
%主函数
clc
clear
A = [];
b = [];
Aeq = [];
beq = [];
lb = -600*ones(30,1);
ub = 600*ones(30,1);
nonlcon = [];
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf);
[x,fval,exitflag,output] = ga(@Fun,30,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
1 至 7 列
-0.0000 0.0016 0.0002 0.0029 -0.0020 0.0071 -0.0002
8 至 14 列
0.0030 0.0003 0.0003 -0.0001 0.0047 -0.0002 0.0064
15 至 21 列
-0.0001 -0.0016 0.0015 0.0010 0.0094 0.0009 0.0045
22 至 28 列
0.0014 0.0060 0.0060 -0.0001 0.0012 0.0007 -0.0002
29 至 30 列
0.0017 0.0028
fval =
1.4155e-05
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'boundconstraints'
rngstate: [1×1 struct]
generations: 399
funccount: 80000
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.'
maxconstraint: 0
function f = Fun(x)
global m;
f = 0;
for i = 1:m
f = f -x(i)*sin(sqrt(abs(x(i))));
end
%主函数
clc
clear
global m;
m=input('函数x为几元:');
A = [];
b = [];
Aeq = [];
beq = [];
lb = -500*ones(m,1);
ub = 500*ones(m,1);
nonlcon = [];
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf,'MaxGenerations',800);
[x,fval,exitflag,output] = ga(@Fun,m,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
函数x为几元:10
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
1 至 7 列
420.9627 420.9846 420.9301 -302.4783 420.9803 420.9537 420.9884
8 至 10 列
-302.5260 420.9911 420.9851
fval =
-3.9530e+03
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'boundconstraints'
rngstate: [1×1 struct]
generations: 199
funccount: 40000
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.'
maxconstraint: 0
matlab代码
clc
clear
fsurf(@(x,y)2*x.^2-4*x.*y+4*y.^2-6*x-3*y);%画个三维图看看
fun=@(x)2*x(1).^2-4*x(1).*x(2)+4*x(2).^2-6*x(1)-3*x(2);
A = [1,1;
4,1];
b = [3;9];
Aeq = [];
beq = [];
lb = zeros(2,1);
ub = Inf*ones(2,1);
nonlcon = [];
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf);
[x,fval,exitflag,output] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
其中函数说明
fsurf函数
画三维曲面图的函数,官方文档
plotfch参数
ga中的画图参数,上面选择的是迭代次数与解的图
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
1.9476 1.0534
fval =
-11.0273
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'linearconstraints'
rngstate: [1×1 struct]
generations: 82
funccount: 4150
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance.'
maxconstraint: 1.0000e-03
clc
clear
fsurf(@(x,y)x.^2+y.^2+8);%画个三维图看看
fun=@(x)x(1).^2+x(2).^2+8;
A = [];
b = [];
Aeq = [];
beq = [];
lb = zeros(2,1);
ub = Inf*ones(2,1);
nonlcon = @ellipsetilt;
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf);
[x,fval,exitflag,output] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance
and constraint violation is less than options.ConstraintTolerance.
x =
1.5406 0.6779
fval =
10.8330
exitflag =
1
output =
包含以下字段的 struct:
problemtype: 'nonlinearconstr'
rngstate: [1×1 struct]
generations: 3
funccount: 9350
message: 'Optimization terminated: average change in the fitness value less than options.FunctionTolerance↵ and constraint violation is less than options.ConstraintTolerance.'
maxconstraint: 1.4362e-04
clc
clear
fun=@(x)1.10471*x(1)^2*x(2)+0.04811*x(3)*x(4)*(14.0+x(2));
A = [1,0,0,-1;
-1,0,0,0];
b = [0;-0.125];
Aeq = [];
beq = [];
lb = [0.1;0.1;0.1;0.1];
ub = [2.0;10.0;10.0;2.0];
nonlcon = @ellipsetilt;
IntCon=[];
options = optimoptions('ga','PlotFcn', @gaplotbestf);
[x,fval,exitflag,output] = ga(fun,4,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
Optimization terminated: no feasible point found.
x =
0.5737 0.1000 0.1000 0.7913
fval =
0.0900
exitflag =
-2
output =
包含以下字段的 struct:
problemtype: 'nonlinearconstr'
rngstate: [1×1 struct]
generations: 1
funccount: 2662
message: 'Optimization terminated: no feasible point found.'
maxconstraint: 1.7183e+08
可见退出原因是没有其他可行点,认为得出的点就是解
官方文档
官方文档
官方文档
官方文档
未完待续················