2021-03-22 14:31:56 +00:00
|
|
|
%% Homework 1 - Optimization Methods
|
|
|
|
% Author: Claudio Maggioni
|
|
|
|
%
|
2021-03-21 21:54:15 +00:00
|
|
|
% Sources:
|
2021-03-22 14:31:56 +00:00
|
|
|
% - https://www.youtube.com/watch?v=91RZYO1cv_o
|
|
|
|
|
|
|
|
clear
|
|
|
|
clc
|
|
|
|
close all
|
|
|
|
format short
|
2021-03-21 21:54:15 +00:00
|
|
|
|
2021-03-22 15:22:10 +00:00
|
|
|
colw = 3;
|
|
|
|
colh = 4;
|
|
|
|
|
2021-03-21 14:05:22 +00:00
|
|
|
%% Exercise 3.1
|
|
|
|
|
|
|
|
% f(x1, x2) = x1^2 + u * x2^2;
|
|
|
|
|
2021-03-21 21:54:15 +00:00
|
|
|
% 1/2 * [x1 x2] [2 0] [x1] + [0][x1]
|
|
|
|
% [0 2u] [x2] + [0][x2]
|
2021-03-21 14:05:22 +00:00
|
|
|
|
|
|
|
% A = [1 0; 0 u]; b = [0; 0]
|
|
|
|
|
|
|
|
%% Exercise 3.2
|
2021-03-22 14:31:56 +00:00
|
|
|
xaxis = -10:1:10;
|
|
|
|
yaxis = xaxis;
|
|
|
|
Zn = zeros(size(xaxis, 2), size(yaxis, 2));
|
|
|
|
Zs = {Zn,Zn,Zn,Zn,Zn,Zn,Zn,Zn,Zn,Zn};
|
2021-03-21 14:05:22 +00:00
|
|
|
|
|
|
|
for u = 1:10
|
|
|
|
A = [1 0; 0 u];
|
|
|
|
|
2021-03-22 14:31:56 +00:00
|
|
|
for i = 1:size(xaxis, 2)
|
|
|
|
for j = 1:size(yaxis, 2)
|
|
|
|
vec = [xaxis(i); yaxis(j)];
|
|
|
|
Zs{u}(i, j) = vec' * A * vec;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for u = 1:10
|
2021-03-22 15:22:10 +00:00
|
|
|
subplot(colh, colw, u);
|
2021-03-22 14:31:56 +00:00
|
|
|
h = surf(xaxis, yaxis, Zs{u});
|
|
|
|
set(h,'LineStyle','none');
|
2021-03-22 15:22:10 +00:00
|
|
|
title(sprintf("\nu=%d", u));
|
2021-03-22 14:31:56 +00:00
|
|
|
end
|
|
|
|
sgtitle("Surf plots");
|
2021-03-21 14:05:22 +00:00
|
|
|
|
2021-03-22 14:31:56 +00:00
|
|
|
% comment these lines on submission
|
|
|
|
addpath /home/claudio/git/matlab2tikz/src
|
|
|
|
matlab2tikz('showInfo', false, './surf.tex')
|
2021-03-21 14:05:22 +00:00
|
|
|
|
2021-03-22 14:31:56 +00:00
|
|
|
figure
|
|
|
|
|
|
|
|
yi = zeros(30, 25);
|
|
|
|
ni = zeros(30, 25);
|
|
|
|
its = zeros(30, 1);
|
|
|
|
|
|
|
|
for u = 1:10
|
2021-03-22 15:22:10 +00:00
|
|
|
subplot(colh, colw, u);
|
2021-03-22 14:31:56 +00:00
|
|
|
contour(xaxis, yaxis, Zs{u}, 10);
|
2021-03-22 15:22:10 +00:00
|
|
|
title(sprintf("\nu=%d", u));
|
2021-03-22 14:31:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
%% Exercise 3.3
|
|
|
|
|
|
|
|
A = [2 0; 0 2*u];
|
|
|
|
b = [0; 0];
|
|
|
|
xs = [[0; 10] [10; 0] [10; 10]];
|
|
|
|
|
|
|
|
syms sx sy
|
|
|
|
f = 1/2 * [sx sy] * A * [sx; sy];
|
|
|
|
|
|
|
|
g = gradient(f, [sx; sy]);
|
|
|
|
|
|
|
|
hold on
|
|
|
|
j = 1;
|
|
|
|
for x0 = xs
|
|
|
|
ri = u * 3 - 3 + j;
|
|
|
|
x = x0;
|
|
|
|
i = 1;
|
|
|
|
|
|
|
|
xi = zeros(2, 25);
|
|
|
|
xi(:, 1) = x0;
|
|
|
|
|
|
|
|
yi(ri, 1) = subs(f, [sx sy], x0');
|
|
|
|
|
|
|
|
while true
|
|
|
|
p = -1 * double(subs(g, [sx sy], x'));
|
|
|
|
|
|
|
|
ni(ri, i) = log10(norm(p, 2));
|
|
|
|
if norm(p, 2) == 0 || ni(ri, i) <= -8
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
alpha = dot(b - A * x, p) / dot(A * p, p);
|
|
|
|
|
|
|
|
x = x + alpha * p;
|
|
|
|
i = i + 1;
|
|
|
|
|
|
|
|
xi(:, i) = x;
|
|
|
|
yi(ri, i) = subs(f, [sx sy], x');
|
2021-03-21 14:05:22 +00:00
|
|
|
end
|
2021-03-22 14:31:56 +00:00
|
|
|
|
|
|
|
xi = xi(:, 1:i);
|
|
|
|
|
|
|
|
plot(xi(1, :), xi(2, :), '-');
|
|
|
|
|
|
|
|
fprintf("u=%2d x0=[%2d,%2d] it=%2d x=[%d,%d]\n", u, ...
|
|
|
|
x0(1), x0(2), i, x(1), x(2));
|
|
|
|
|
|
|
|
its(ri) = i;
|
|
|
|
|
|
|
|
j = j + 1;
|
2021-03-21 14:05:22 +00:00
|
|
|
end
|
2021-03-22 14:31:56 +00:00
|
|
|
hold off
|
|
|
|
end
|
|
|
|
sgtitle("Contour plots and iteration steps");
|
|
|
|
|
|
|
|
% comment these lines on submission
|
|
|
|
addpath /home/claudio/git/matlab2tikz/src
|
|
|
|
matlab2tikz('showInfo', false, './contour.tex')
|
|
|
|
|
|
|
|
figure
|
2021-03-21 14:05:22 +00:00
|
|
|
|
2021-03-22 14:31:56 +00:00
|
|
|
for u = 1:10
|
2021-03-22 15:22:10 +00:00
|
|
|
subplot(colh, colw, u);
|
|
|
|
title(sprintf("\nu=%d", u));
|
2021-03-22 14:31:56 +00:00
|
|
|
hold on
|
|
|
|
for j = 1:3
|
|
|
|
ri = u * 3 - 3 + j;
|
|
|
|
vec = yi(ri, :);
|
|
|
|
vec = vec(1:its(ri));
|
|
|
|
|
|
|
|
plot(1:its(ri), vec);
|
2021-03-21 14:05:22 +00:00
|
|
|
end
|
2021-03-22 14:31:56 +00:00
|
|
|
hold off
|
2021-03-21 21:54:15 +00:00
|
|
|
end
|
2021-03-22 14:31:56 +00:00
|
|
|
sgtitle("Iterations over values of objective function");
|
|
|
|
|
|
|
|
% comment these lines on submission
|
|
|
|
addpath /home/claudio/git/matlab2tikz/src
|
|
|
|
matlab2tikz('showInfo', false, './yseries.tex')
|
|
|
|
|
|
|
|
figure
|
|
|
|
|
|
|
|
for u = 1:10
|
2021-03-22 15:22:10 +00:00
|
|
|
subplot(colh, colw, u);
|
2021-03-22 14:31:56 +00:00
|
|
|
hold on
|
|
|
|
for j = 1:3
|
|
|
|
ri = u * 3 - 3 + j;
|
|
|
|
vec = ni(ri, :);
|
|
|
|
vec = vec(1:its(ri));
|
|
|
|
|
|
|
|
plot(1:its(ri), vec);
|
|
|
|
end
|
|
|
|
hold off
|
2021-03-22 15:22:10 +00:00
|
|
|
title(sprintf("\nu=%d", u));
|
2021-03-22 14:31:56 +00:00
|
|
|
end
|
|
|
|
sgtitle("Iterations over log10 of gradient norms");
|
|
|
|
|
|
|
|
% comment these lines on submission
|
|
|
|
addpath /home/claudio/git/matlab2tikz/src
|
|
|
|
matlab2tikz('showInfo', false, './norms.tex')
|
|
|
|
|