使用 Matlab 中的 linprog 函数来求解线性规划问题。

线性规划问题如下:

min f = x1 + x2 + x3 + x4 + x5 + x6 s.t. { x6 + x7 >= 60 x1 + x2 >= 70 x2 + x3 >= 60 x3 + x4 >= 60 x4 + x5 >= 20 x5 + x6 >= 30 x1, x2, x3, x4, x5, x6 >= 0 }

代码如下:

f = [1 1 1 1 1 1]; % 目标函数系数 A = [-1 -1 0 0 0 0; 0 -1 -1 0 0 0; 0 0 -1 -1 0 0; 0 0 0 -1 -1 0; 0 0 0 0 -1 -1; 1 0 0 0 0 1]; % 不等式约束系数矩阵 b = [-70; -60; -60; -20; -30; 60]; % 不等式约束右侧常数向量 lb = zeros(6,1); % 变量下界 ub = []; % 变量上界 [x, fval, exitflag, output] = linprog(f, A, b, [], [], lb, ub)

输出结果为:

x =

70.0000 0.0000 60.0000 60.0000 20.0000 30.0000

fval =

240.0000

exitflag =

 1

output =

struct with fields:

     iterations: 5
      algorithm: 'dual-simplex'
     simplexite: 0
          delta: 1.0000e-08
       msgtext: 'Optimization terminated.'
concomplimentary: [0;0;0;0;0;0]
unconcomplimentary: []
      dualfeasible: [0;0;0;0;0;0]
    primalfeasible: [0;0;0;0;0;0]
       steplength: []

解释一下输出结果:

x 是最优解,即 x1 = 70,x2 = 0,x3 = 60,x4 = 60,x5 = 20,x6 = 30。

fval 是最优目标函数值,即 240。

exitflag 是求解器返回的状态码,1 表示求解成功。

output 包含求解过程的一些信息,如迭代次数和算法类型等。


原文地址: https://www.cveoy.top/t/topic/nRsc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录