1.简述
语法:[X,FVAL] = linprog(f,a,b,Aeq,Beq,LB,UB,X0);
X 为最终解 , FVAL为最终解对应的函数值
*注意:求最大值时,结果FVAL需要取反*
f 为决策函数的系数矩阵。
*注意:当所求为最大值时,系数矩阵需要取反*
a 为约束条件中不等式组的系数矩阵 ,a的列数等于f的列数
*注意:当不等号为 > 或 ≥ 时,矩阵需要取反*
b 为约束条件中不等式组右边的值
*注意:当不等号为 > 或 ≥ 时,矩阵需要取反*
Aeq 为约束条件中等式组的系数矩阵 ,Aeq的列数等于f的列数
Beq 为约束条件中等式组右边的值
LB、UB 是解的范围
X0 为初始值
2.代码
主程序:
%% 投资问题
f=[-0.2;-0.12;-0.09;-0.14];
A=[1,-1,-1,-1;0,-1,-1,1];
b=[0;0];
Aeq=[1,1,1,1];
beq=1;
l=[0 0 0];
u=[1 1 1];
[xo,yo,exitflag]=linprog(f,A,b,Aeq,beq,l,u)
子程序:
function [x,fval,exitflag,output,lambda]=linprog(f,A,B,Aeq,Beq,lb,ub,x0,options)
%LINPROG Linear programming.
% X = LINPROG(f,A,b) attempts to solve the linear programming problem:
%
% min f'*x subject to: A*x <= b
% x
%
% X = LINPROG(f,A,b,Aeq,beq) solves the problem above while additionally
% satisfying the equality constraints Aeq*x = beq. (Set A=[] and B=[] if
% no inequalities exist.)
%
% X = LINPROG(f,A,b,Aeq,beq,LB,UB) defines a set of lower and upper
% bounds on the design variables, X, so that the solution is in
% the range LB <= X <= UB. Use empty matrices for LB and UB
% if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below;
% set UB(i) = Inf if X(i) is unbounded above.
%
% X = LINPROG(f,A,b,Aeq,beq,LB,UB,X0) sets the starting point to X0. This
% option is only available with the active-set algorithm. The default
% interior point algorithm will ignore any non-empty starting point.
%
% X = LINPROG(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
% structure with the vector 'f' in PROBLEM.f, the linear inequality
% constraints in PROBLEM.Aineq and PROBLEM.bineq, the linear equality
% constraints in PROBLEM.Aeq and PROBLEM.beq, the lower bounds in
% PROBLEM.lb, the upper bounds in PROBLEM.ub, the start point
% in PROBLEM.x0, the options structure in PROBLEM.options, and solver
% name 'linprog' in PROBLEM.solver. Use this syntax to solve at the
% command line a problem exported from OPTIMTOOL.
%
% [X,FVAL] = LINPROG(f,A,b) returns the value of the objective function
% at X: FVAL = f'*X.
%
% [X,FVAL,EXITFLAG] = LINPROG(f,A,b) returns an EXITFLAG that describes
% the exit condition. Possible values of EXITFLAG and the corresponding
% exit conditions are
%
% 3 LINPROG converged to a solution X with poor constraint feasibility.
% 1 LINPROG converged to a solution X.
% 0 Maximum number of iterations reached.
% -2