一、分析
1)max z = 2X1 + 3X2 - 5X3 就是我们需要求解的目标函数,其中包括三个未知数,X1,X2,X3。如何利用matlab找到其中的X1,X2,X3就是我们问题求解的关键!
2)s.t. 表述 subject to 约束条件,其中的X1,X2,X3有他规定的范围,可以理解为限定条件或者约束条件,函数的定义域等等。
二、利用matlab解题
1)利用linprog函数来解决线性规划问题,linprog是专门解决线性规划问题的。
x = linprog(f,A,b,Aeq,beq,lb,ub,options)
其中:求解 min f*x,求解 f*x的最小函数,(调试经验来看,直接横着输入不等式的数字即可)其中s.t.约束条件为:A≤b;
Aeq,beq,是增加的约束条件,即 Aeq*x = beq。如果没有等式存在可以用 :A = [ ] and b = [ ]
lb和ub表示定义变量的下界和上界,确保x始终在该范围内,即lb ≤ x ≤ ub。如果没有约束条件,Aeq = [ ] and beq = [ ]
2)matlab求解
K>> f = [2 3 -5];
K>> a = [-2 1; 5 3; -1 1];
K>> b = [-10;12];
K>> aeq = [1;1;1];
K>> be1 = [7];
K>> lb = [0;0;0];
K>> ub = [inf;inf;inf];
K>> [x,fval] = linprog(-f,a,b,aeq,beq,lb,ub);
错误使用 keyboard
函数或变量 'beq' 无法识别。
错误1:错误原因是打错了变量名字,beq达成be1
错误使用 linprog
The number of rows in A must be the same as the number of elements of b.
错误2:f*x 约束条件理解出错,便携记忆:等式的数字依次输入。线代转化,其运算过程自己的可以手动推到。
K>> a = [-2,5,-1;,1,1,1];
K>> [x,fval] = linprog(-f,a,b,aeq,beq,lb,ub);
错误使用 linprog
The number of columns in Aeq must be the same as the number of elements of f.
错误3:aeq只是一维数组,不是三维数组,符号输入错误。
K>> [x,fval] = linprog(-f,a,b,aeq,beq,lb,ub);
错误使用 linprog
The number of rows in Aeq must be the same as the number of elements of beq.
错误4:beq = 7 ,输入为 beq = [7];
K>> beq = 7;
K>> [x,fval] = linprog(-f,a,b,aeq,beq,lb,ub);
Optimal solution found.
K>> x
x =
6.4286
0.5714
0
K>> best = f*x
best =
14.5714
K>>