logo

Solving Linear Programming Problems with MATLAB 📂Optimization

Solving Linear Programming Problems with MATLAB

Overview

You can use the Optimization Toolbox1. Insert the $A, \mathbf{b}, \mathbf{c}$ in matrix form for the linear programming problem.

Code

$$ \begin{matrix} \text{Maximize} & & x_{1} & + & x_{2} \\ \text{subject to} &-& x_{1} & + & x_{2} & \le & 1 \\ & & x_{1} & & & \le & 3 \\ & & & & x_{2} & \le & 2 \end{matrix} $$

As a simple example, let’s solve the following maximization problem from $x_{1} , x_{2} \ge 0$. At the shrimp sushi restaurant, this problem was solved manually using the simplex method, and the answer $\left( x_{1}^{\ast}, x_{2}^{\ast} \right) = (3,2)$ was known. This linear programming problem is

$$ \begin{matrix} \text{Optimize} & \mathbf{c}^{T} \mathbf{x} \\ \text{subject to} & A \mathbf{x} \le \mathbf{b} \end{matrix} $$

In such a form, since $\mathbf{c} = (1,1)$, $A = \begin{bmatrix} -1 & 1 \\ 1 & 0 \\ 1 & 0 \end{bmatrix}$, $\mathbf{b} = (1,3,2)$, it can be transcribed and solved as follows.

>> A = [-1 1
    1 0
    0 1];

b = [1 3 2];
c = [-1 -1];

x = linprog(f,A,b,[],[], [0,0])

Here, the reason for using c = [-1,-1] instead of c = [1,1] is because the basic optimization direction of linprog() is for minimization. Just reversing that direction is tantamount to maximization, and the result is $\left( x_{1}, x_{2} \right) = \left( 3,2 \right) =$[3 2], as we already knew.

최적해를 구했습니다.

x =

     3
     2

Environment

  • OS: Windows
  • MATLAB: v9.9.0.1592791 (R2020b)
  • Optimization Toolbox: v9.0 (R2020b)

See Also