matlab学习之优化模型 --(matlab编程)-----数模

这里主要对于matlab优化工具箱进行整理

官方文档有例子,用到哪个根据目录检索,再官方文档查阅

linprog求解线性规划:

求解一个线性不等式、线性等式和边界的简单线性规划。
这里写图片描述
调用格式为:

[x,fval] =linprog(f,A,b,Aeq,beq,lb,ub)

这里fval 返回目标函数的值,lb 和ub分别是变量x 的下界和上界. 在 Matlab 指令窗运行 help linprog 可以看到所有的函数调用形式.官方文档

intlinprog求解求解整数或混合整数规划。

这里写图片描述
官方文档

例题1:

这里写图片描述
matlab代码

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

所以结果为
这里写图片描述
利润:1.1786e+03即1178.6元

例题2

这里写图片描述
matlab代码:

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

所以结果为
这里写图片描述
摧毁敌方军事目标可能性基本趋近于1

例题3

这里写图片描述
matlab代码:

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

例题4

这里写图片描述
matlab代码:

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

例题5

这里写图片描述
matlab代码:

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求一元函数在定区间上最小值(有导数),传统优化算法

fminbnd是一个函数文件。该算法基于黄金分割搜索和抛物线插值。可以求解一元的可导函数的最小值
限制

  • 要最小化的函数必须是连续的。
  • fminbnd可能只提供本地解决方案。
  • 当解决方案位于间隔的边界上时, fminbnd可以呈现缓慢的收敛性。

官方文档

fminsearch使用无导数法(Nelder-Mead直接搜索法)计算无约束的多元函数的最小值(无导数)传统优化算法

fminsearch 使用 Lagarias 等的单纯形搜索方法。[1]这是一种直接搜索方法,不像在 fminunc 中那样使用数值或解析梯度。

提示

  • fminsearch 仅对实数求最小值,即向量或数组 x 只能由实数组成,并且 f(x) 必须只返回实数。当 x 具有复数值时,将 x 拆分为实部和虚部。

  • 使用 fminsearch 解决不可微分的问题或者具有不连续性的问题,尤其是在解附近没有出现不连续性的情况下。

官方文档

fminunc无约束的多元函数的最小值传统优化算法

官方文档

ga使用遗传算法(GA)求解无约束优化问题的最小值

官方文档

例题1

这里写图片描述

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的修改可以参考

例题2

这里写图片描述

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 =
  17 列
   -0.0000    0.0016    0.0002    0.0029   -0.0020    0.0071   -0.0002
  8140.0030    0.0003    0.0003   -0.0001    0.0047   -0.0002    0.0064
  1521 列
   -0.0001   -0.0016    0.0015    0.0010    0.0094    0.0009    0.0045
  22280.0014    0.0060    0.0060   -0.0001    0.0012    0.0007   -0.0002
  29300.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

例题3

这里写图片描述

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)
  • global是定义全局变量的函数,在其他函数中用时,也要用global再次指明调用的是全局变量
结果

这里写图片描述

函数x为几元:10
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x =
  17420.9627  420.9846  420.9301 -302.4783  420.9803  420.9537  420.9884
  810 列
 -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

例题4

这里写图片描述

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

例题5

这里写图片描述

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

例题6

这里写图片描述
matlab代码

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

可见退出原因是没有其他可行点,认为得出的点就是解

particleswarm使用粒子群优化算法(PSO)求解无约束优化问题的最小值

官方文档

patternsearch使用模式搜索法(PS)求解无约束优化问题的最小值

官方文档

fzero非线性函数的零解(非线性方程的根)

官方文档

fsolve解非线性方程组

官方文档

polyfit多项式曲线拟合

lsqlin解线性二乘问题

lsqnonneg解非负线性最小二乘问题

lsqcurvefit解非线性曲线拟(数据拟合)合问题

lsqnonlin解非线性二乘问题(非线性数据拟合)

未完待续················

DONE!!!

  • 25
    点赞
  • 174
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值