Skip to content

(MatLab) Problem is unbounded when doing the linear programming in matlab

An answer to this question on Stack Overflow.

Question

x + y  44,
2x + y  64,
9,000x + 5,000y  300,000

objective function: 30,000x + 20,000y

I would like to find the Optimal solution in Matlab

But there are error message Problem is unbounded.

Here is my code

A = [1 1;2 1;9 5];
b = [44 64 300];
f = [3 2];
x = linprog(f,A,b)

suppose ans: x=20,y=24

Reference https://www.mathworks.com/help/optim/ug/linprog.html

Answer

It's not a direct answer to your question, but one reason you're having difficulties is that you're using Matlab. Other languages would offer more expressive and easier-to-understand ways of expressing your problem.

Python for instance, coupled with cvxpy gives us:

#!/usr/bin/env python3
import cvxpy as cp
x = cp.Variable()
y = cp.Variable()
constraints = [
  x + y <= 44,
  2 * x + y <= 64
]
objective = cp.Maximize(9000*x + 5000*y)
prob = cp.Problem(objective, constraints)
N
optimum_value = prob.solve()
print("optimum value", optimum_value)
print("x", x.value)
print("y", y.value)

The code is very readable and the model is easily extended even to non-linear regimes such as quadratic programs and second-order cones.

This returns

optimum value 300000.00000076543
x 20.00000000075882
y 23.999999998787203