OM/hw1/ex3.m

43 lines
762 B
Matlab

%% Exercise 3.1
% f(x1, x2) = x1^2 + u * x2^2;
% [x1 x2] [1 0] [x1] + [0][x1]
% [0 u] [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