Example 1: Maximizing Profit
Problem Statement: A company produces two products, A and B. Each unit of
product A requires 3 hours of labor and 2 units of material, while each unit of
product B requires 2 hours of labor and 1 unit of material. The company has 150
hours of labor and 100 units of material available. The profit from each unit of
product A is $40, and the profit from each unit of product B is $30. Determine the
number of units of each product to produce to maximize profit.
Formulation:
Objective Function: Maximize profit z=40x1+30x2z = 40x_1 +
30x_2z=40x1+30x2
Constraints:
o Labor constraint: 3x1+2x2≤1503x_1 + 2x_2 \leq 1503x1+2x2≤150
o Material constraint: 2x1+x2≤1002x_1 + x_2 \leq 1002x1+x2≤100
o Non-negativity: x1≥0x_1 \geq 0x1≥0, x2≥0x_2 \geq 0x2≥0
Solution in Octave:
octave
Copy code
pkg load optim % Load the optim package
% Coefficients for the objective function (negative for maximization)
c = [-40; -30];
% Coefficients for inequality constraints
A = [3, 2; 2, 1];
% Right-hand side of inequality constraints
b = [150; 100];
% Lower bounds (non-negativity constraints)
lb = [0; 0];
% Solve the linear programming problem
[x, fval] = linprog(c, A, b, [], [], lb);
% Display results
disp('Optimal number of units for product A and B:');
disp(x);
disp('Maximum profit:');
disp(-fval); % fval is negative because we minimized; negate it to get profit
Output Interpretation:
x will contain the optimal number of units of products A and B.
-fval will give the maximum profit.
Example 2: Resource Allocation
Problem Statement: A factory produces two types of widgets. Widget 1
requires 4 hours of machine time and 3 hours of labor. Widget 2 requires 2 hours
of machine time and 5 hours of labor. The factory has 120 hours of machine time
and 150 hours of labor available. The profit per unit of Widget 1 is $50, and per
unit of Widget 2 is $60. Determine how many units of each widget to produce to
maximize profit.
Formulation:
Objective Function: Maximize profit z=50x1+60x2z = 50x_1 +
60x_2z=50x1+60x2
Constraints:
o Machine time: 4x1+2x2≤1204x_1 + 2x_2 \leq 1204x1+2x2≤120
o Labor: 3x1+5x2≤1503x_1 + 5x_2 \leq 1503x1+5x2≤150
o Non-negativity: x1≥0x_1 \geq 0x1≥0, x2≥0x_2 \geq 0x2≥0
Solution in Octave:
octave
Copy code
pkg load optim % Load the optim package
% Coefficients for the objective function (negative for maximization)
c = [-50; -60];
% Coefficients for inequality constraints
A = [4, 2; 3, 5];
% Right-hand side of inequality constraints
b = [120; 150];
% Lower bounds (non-negativity constraints)
lb = [0; 0];
% Solve the linear programming problem
[x, fval] = linprog(c, A, b, [], [], lb);
% Display results
disp('Optimal number of units for Widget 1 and Widget 2:');
disp(x);
disp('Maximum profit:');
disp(-fval); % fval is negative because we minimized; negate it to get profit
Output Interpretation:
x will show the optimal number of units of Widget 1 and Widget 2.
-fval will be the maximum profit obtained.