48 lines
885 B
Matlab
48 lines
885 B
Matlab
% Sources:
|
|
% https://www.youtube.com/watch?v=91RZYO1cv_o
|
|
|
|
%% Exercise 3.1
|
|
|
|
% f(x1, x2) = x1^2 + u * x2^2;
|
|
|
|
% 1/2 * [x1 x2] [2 0] [x1] + [0][x1]
|
|
% [0 2u] [x2] + [0][x2]
|
|
|
|
% A = [1 0; 0 u]; b = [0; 0]
|
|
|
|
%% Exercise 3.2
|
|
|
|
|
|
for u = 1:10
|
|
A = [1 0; 0 u];
|
|
|
|
xs = -10:1:10;
|
|
ys = xs;
|
|
|
|
Z = zeros(size(xs, 2), size(ys, 2));
|
|
|
|
for i = 1:size(xs, 2)
|
|
for j = 1:size(ys, 2)
|
|
vec = [xs(i); ys(j)];
|
|
Z(i, j) = vec' * A * vec;
|
|
end
|
|
end
|
|
|
|
if u <= 5
|
|
fig = u;
|
|
con = 5 + u;
|
|
else
|
|
fig = 10 + u - 5;
|
|
con = 15 + u - 5;
|
|
end
|
|
|
|
subplot(4, 5, fig);
|
|
h = surf(xs, ys, Z);
|
|
set(h,'LineStyle','none');
|
|
title(sprintf("Surf for u=%d", u));
|
|
subplot(4, 5, con);
|
|
contour(xs, ys, Z, 20);
|
|
title(sprintf("Contour for u=%d", u));
|
|
|
|
end
|
|
matlab2tikz('showInfo', false, './ex32.tex')
|