%% Critical point example, Week 7 Lecture

% look at different domains
%x1 = linspace(-0.5, 2.5, 50);   y1 = linspace(-0.5, 2.5, 50);
%x1 = linspace(0, 2, 50);  y1 = linspace(0, 2, 50);
x1 = linspace(0, 3, 50);  y1 = linspace(0, 2, 50);

[x,y] = meshgrid(x1, y1);

f = @(x, y) (2*x - x.^2).*(2*y - y.^2);
ff = f(x, y);

% messy hack to graph it over a more complicated region
%I = (x >= 0) & (x <= 3) & (y >= 0) & (y <= -x/6+2) & ((x-1).^2 + y.^2 >= 1);
%ff(~I) = nan;

figure(1); clf;
contour(x, y, ff, 'linewidth', 2);
xlabel('x'); ylabel('y');
axis equal; axis tight
cp = [1 1; 0 0; 0 2; 2 0; 2 2];
hold on
plot(cp(:,1), cp(:,2), 'r*', 'linewidth', 5);

figure(2); clf;
surf(x, y, ff);
xlabel('x'); ylabel('y'); zlabel('z');
axis equal; axis tight
hold on
plot3(cp(:,1), cp(:,2), f(cp(:,1), cp(:,2)), 'r*', 'linewidth', 5);
colorbar
