Linear programming: a production plan Open ↗
Code
% A workshop makes tables (profit 30) and chairs (profit 20).
% Hours available: carpentry 120 (tables 4 h, chairs 2 h), finishing 90 (tables 2 h, chairs 3 h).
% At least 5 chairs must be made. linprog minimises, so we minimise minus the profit.
f = -[30; 20];
A = [4 2; 2 3; 0 -1]; b = [120; 90; -5];
[x, fval] = linprog(f, A, b, [], [], [0; 0]);
[T, Cc] = meshgrid(0:0.25:35, 0:0.25:35);
ok = 4*T + 2*Cc <= 120 & 2*T + 3*Cc <= 90 & Cc >= 5;
contourf(T, Cc, 30*T + 20*Cc .* ok - 1e3*~ok, [0 200:200:1200]), colormap(gray), hold on
t = 0:35;
plot(t, (120 - 4*t)/2, t, (90 - 2*t)/3, 'LineWidth', 1.8), yline(5, '--')
plot(x(1), x(2), 'rp', 'MarkerSize', 14), hold off
xlim([0 35]), ylim([0 35]), xlabel('tables'), ylabel('chairs')
legend('profit (feasible area)', 'carpentry hours', 'finishing hours', 'minimum chairs', 'optimum')
title(sprintf('Best plan: %.1f tables, %.1f chairs, profit %.0f', x(1), x(2), -fval))