This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
OM/hw1/ex3.m

43 lines
762 B
Mathematica
Raw Normal View History

2021-03-21 14:05:22 +00:00
%% 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