%% Solve LP Using Problem-Based Approach for linprog
% This example shows how to set up a problem using the problem-based approach and then solve it using the solver-based approach. The problem is
% \max_x (x + y/3) {\rm \ subject\ to\ } \left\{ {\begin{array}{*{20}{l}} {x+y\le 2}\\
% {x+y/4\le 1}\\
% {x-y\le 2}\\
% {x/4+y\ge -1}\\
% {x+y\ge 1}\\
% {-x+y\le 2}\\
% {x+y/4 = 1/2}\\
% {-1\le x\le 1.5}\\
% {-1/2\le y\le 1.25}{\end{array}}\right .
% Create an OptimizationProblem object named prob to represent this problem.

x = optimvar('x','LowerBound',-1,'UpperBound',1.5);
y = optimvar('y','LowerBound',-1/2,'UpperBound',1.25);
prob = optimproblem('Objective',x + y/3,'ObjectiveSense','max');
prob.Constraints.c1 = x + y <= 2;
prob.Constraints.c2 = x + y/4 <= 1;
prob.Constraints.c3 = x - y <= 2;
prob.Constraints.c4 = x/4 + y >= -1;
prob.Constraints.c5 = x + y >= 1;
prob.Constraints.c6 = -x + y <= 2;
prob.Constraints.c7 = x + y/4 == 1/2;

% Convert the problem object to a problem structure.

problem = prob2struct(prob);

% Solve the resulting problem structure.

[sol,fval,exitflag,output] = linprog(problem)

% The returned fval is negative, even though the solution components are positive. Internally, prob2struct turns the maximization problem into a minimization problem of the negative of the objective function. See Maximizing an Objective.
% Which component of sol corresponds to which optimization variable? Examine the Variables property of prob.

prob.Variables

% As you might expect, sol(1) corresponds to x, and sol(2) corresponds to y. See Algorithms.
% Copyright 2018 The MathWorks, Inc.