1.简述
linprog函数主要用来求线型规划中的最小值问题(最大值的镜像问题,求最大值只需要加个“-”)
2. 算法结构及使用方法
针对约束条件为Ax=b或Ax≤b的问题
2.1 linprog函数
x=linprog(f,A,b)
x=linprog(f,A,b,Aeq,beq)
x=linprog(f,A,b,Aeq,beq,lb,ub)
x=linprog(f,A,b,Aeq,beq,lb,ub,x0)
2.2 参数简介
f:目标函数
A:不等式约束条件矩阵
b:对应不等式右侧的矩阵
Aeq:等式约束条件矩阵
beq:不等式右侧的矩阵
Aeq:等式约束条件矩阵
beq:对应等式右侧的矩阵
lb:x的下界
ub:x的上界
x0:设置初始点x0,这个选择项只是对medium-scale算法有效。默认的large-scale算法和简单的算法忽略任何初始点。(一般用不到)
2.3 常用linprog函数及用法举例
linprog函数常用形式为:
x=linprog(f,A,b,Aep,beq,lb,ub);
例子: 学习目标:有约束条件多元变量函数最小值
适合 计划生产盈利最大 的模式求解,
最大值解法可转化为求解最小值算法,非常容易
求最大值转化为求最小值 f=70*x1+120*x2 的最大值,当然x1,x2是有约束的。
转化为求 f=-(70*x1+120*x2) 的最小值。
约束条件:9*x1+4*x2<=3600;4*x1+5*x2<=2000;3*x1+10*x2<=3000;-x1,-x2<
2.代码
主函数:
clc
clear
f=[-70 -120];
A=[9 4;4 5;3 10];
B=[3600;2000;3000];
Aeq=[]; Beq=[];
lb=[0 0];ub=[inf inf];
x0=[1 1];
options=optimset('display','iter','Tolx',1e-8);
[x,f,exitflag]=linprog(f,A,B,Aeq,Beq,lb,ub,x0,options)
%[xmincon,fval,exitflag,output] = fmincon(@(x)-(70*x(1)+120*x(2)),x0,A,B,Aeq,Beq,lb,ub,[],options)
子函数:
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.