2021-06-01 14:19:05 +00:00
|
|
|
clc
|
|
|
|
clear
|
|
|
|
close all
|
|
|
|
|
|
|
|
%% Exercise 2.1
|
|
|
|
|
|
|
|
syms x1 x2
|
|
|
|
c1 = 2 * x1 + 3 * x2 - 6;
|
|
|
|
c2 = -3 * x1 + 2 * x2 - 3;
|
|
|
|
c3 = 2 * x2 - 5;
|
|
|
|
c4 = 2 * x1 + x2 - 4;
|
|
|
|
|
|
|
|
c1 = solve(c1 == 0, x2, 'Real', true);
|
|
|
|
c2 = solve(c2 == 0, x2, 'Real', true);
|
|
|
|
c3 = solve(c3 == 0, x2, 'Real', true);
|
|
|
|
c4 = solve(c4 == 0, x2, 'Real', true);
|
|
|
|
|
|
|
|
i1 = solve(c1 == c2, x1, 'Real', true);
|
|
|
|
i2 = solve(c1 == c4, x1, 'Real', true);
|
|
|
|
i3 = solve(c4 == 0, x1, 'Real', true);
|
|
|
|
|
|
|
|
px = double([0 0 i1 i2 i3]);
|
2021-06-01 20:38:06 +00:00
|
|
|
pysym = [0 subs(c2, x1, 0) subs(c1, x1, i1) subs(c4, x1, i2) subs(c4, x1, i3)];
|
|
|
|
py = double(pysym);
|
2021-06-01 14:19:05 +00:00
|
|
|
|
|
|
|
xl = -0.05;
|
|
|
|
xh = 2.05;
|
|
|
|
|
2021-06-01 20:38:06 +00:00
|
|
|
orange = [232/255 128/255 18/255];
|
|
|
|
grey = [0.5 0.5 0.5];
|
|
|
|
colors = [orange; 0 0 1; 1 0 0; 0 1 0];
|
|
|
|
dirs = [30 30 30 30];
|
2021-06-01 14:19:05 +00:00
|
|
|
|
|
|
|
axis([-0.05 2.05 -0.15 5.15])
|
|
|
|
i = 1;
|
|
|
|
hold on
|
|
|
|
for c = [c1 c2 c3 c4]
|
2021-06-01 20:38:06 +00:00
|
|
|
pl = patch([xl, xl, xh, xh], ...
|
|
|
|
double([-0.15, subs(c, x1, xl), subs(c, x1, xh), -0.15]), ...
|
|
|
|
colors(i, :));
|
|
|
|
pl.EdgeColor = colors(i, :);
|
|
|
|
pl.FaceAlpha = .2;
|
|
|
|
pl.EdgeAlpha = .2;
|
2021-06-01 14:19:05 +00:00
|
|
|
i = i + 1;
|
|
|
|
end
|
2021-06-01 20:38:06 +00:00
|
|
|
|
|
|
|
pl = patch([0, 0, xh, xh], ...
|
2021-06-01 14:19:05 +00:00
|
|
|
double([0, 5.15, 5.15, 0]), ...
|
2021-06-01 20:38:06 +00:00
|
|
|
grey);
|
|
|
|
pl.EdgeColor = grey;
|
|
|
|
pl.FaceAlpha = .2;
|
|
|
|
pl.EdgeAlpha = .2;
|
|
|
|
|
|
|
|
pl = patch(px, py, 'green');
|
|
|
|
pl.EdgeColor = 'green';
|
|
|
|
|
|
|
|
legend('2x1 + 3x2 <= 6', '-3x1 + 2x2 <= 3', '2x2 <= 5', ...
|
|
|
|
'2x1 + x2 <= 4', 'x1 > 0 and x2 > 0', 'feasible region');
|
2021-06-01 14:19:05 +00:00
|
|
|
hold off
|
|
|
|
|
2021-06-03 13:23:37 +00:00
|
|
|
%% Exercise 3.1
|
2021-06-01 14:19:05 +00:00
|
|
|
|
|
|
|
G = [6 2 1; 2 5 2; 1 2 4];
|
|
|
|
c = [-8; -3; -3];
|
|
|
|
A = [1 0 1; 0 1 1];
|
|
|
|
b = [3; 0];
|
|
|
|
|
2021-06-03 13:23:37 +00:00
|
|
|
K = [G A'; A zeros(2)];
|
|
|
|
eig(K)
|
|
|
|
|
|
|
|
%% Exercise 3.2
|
|
|
|
|
2021-06-01 14:19:05 +00:00
|
|
|
[x, lambda] = uzawa(G, c, A, b, [0;0;0], [0;0], 1e-8, 100);
|
|
|
|
display(x);
|
|
|
|
display(lambda);
|
|
|
|
|