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:

\[\min f^Tx\]

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:

  • f is the objective coefficient vector
  • A,b describe inequality constraints
  • Aeq,beq describe equality constraints
  • lb,ub describe lower and upper bounds
  • x is the solution vector
  • fval is 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.

25

25
Ready to start
Matlab linprog
Session: 1 | Break: Short
Today: 0 sessions
Total: 0 sessions