Matlab linprog
MATLAB linprog
MATLAB linprog solves linear programming problems in matrix form. It is especially useful after the model has been written as vectors and matrices.
MATLAB Form
linprog solves problems of the form:
subject to:
\[Ax\le b\] \[A_{eq}x=b_{eq}\] \[lb\le x\le ub.\]Basic Syntax
The common syntax is:
[x, fval, exitflag, output] = linprog(f, A, b, Aeq, beq, lb, ub);
where:
-
fis the objective coefficient vector -
A,bdescribe inequality constraints -
Aeq,beqdescribe equality constraints -
lb,ubdescribe lower and upper bounds -
xis the solution vector -
fvalis the minimized objective value
Maximization
If the original problem is:
\[\max c^Tx,\]solve instead:
\[\min -c^Tx.\]In MATLAB:
f = -c;
[x, fval] = linprog(f, A, b, Aeq, beq, lb, ub);
max_value = -fval;
Greater-Than Constraints
linprog accepts $Ax\le b$ inequalities. Convert a $\ge$ constraint by multiplying both sides by $-1$.
If:
\[a^Tx\ge b,\]then use:
\[-a^Tx\le -b.\]Bounds
For nonnegative variables:
lb = zeros(n,1);
If a variable has no upper bound, use:
ub = [];
or put Inf for that variable.
Output Checks
Check:
A*x <= b
Aeq*x == beq
x >= lb
Use a numerical tolerance, because floating-point results may have tiny errors such as $10^{-9}$.
See Also
Exam checkpoint
For solver questions, the solver result is not enough. Also report the LP model, variable meanings, objective value, active constraints, and whether the result matches algebraic expectations.