% demo_2d_max_min
% File 62
% Stavros Toumpis, 2008
% Freshman Calculus
%
% This demo plots various 2d functions and studies their maxima and minima

% Initialization
clear;
figure(1); set(gcf,'Position',[50 50 700 670]); colormap('default');
figure(2); set(gcf,'Position',[50 50 700 670]); colormap('default');

% A minimum at the boundary
% =========================

disp('In next two plots, there are extrema at the boundary of the domain');

figure(1);  hold off; clf; hold on;
ezsurf('exp(-x.^2-y.^2-x*y)',[1 2 -2 2]);
get(gca,'Title');
axis([0.5 2.5 -2 2 0 0.5]);
view(3);
pause;

figure(2);  hold off; clf; hold on;
ezplot('exp(-x.^2)',[1 2]);
get(gca,'Title');
axis([0.5 2.5 0 0.5]);
pause;

% A minimum at a point with no gradient
% =====================================

disp('In next two plots, there are extrema inside the domain, at ');
disp('   locations where the function is not differentiable');

figure(1);  hold off; clf; hold on;
h=ezsurf('sqrt(x.^2+y.^2)',[-2 2 -2 2],200);
set(h,'edgealpha',0);
get(gca,'Title');
axis([-2 2 -2 2 0 3]);
view(3);
pause;

figure(2);  hold off; clf; hold on;
ezplot('abs(x)',[-2 2]);
get(gca,'Title');
axis([-2 2 -0.5 2]);
grid on;
pause;

% An interior point with non-zero gradient 
% ========================================

disp('In next two plots, we identify points where the (partial)');
disp('   deriviative is not zero');

% we plot the function
figure(1);  hold off; clf; hold on;
ezsurf('-x.^2-y.^2+10',[-2 2 -2 2]);
axis([-2 2 -2 2 2 10]);
view(3);
pause;

% we plot f(x,-0.5) and the relevant plane
h=ezplot3('t','-0.5','10-t.^2-0.5.^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[-2 2 2 -2]; Y=[-1 -1 -1 -1]/2; Z=[2 2 10 10];
h=patch(X,Y,Z,'b');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
view(3);
pause;

% we plot f(-1,y) and the relevant plane
h=ezplot3('-0.5','t','10-0.5.^2-t.^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[-1 -1 -1 -1]/2; Y=[-2 2 2 -2]; Z=[2 2 10 10];
h=patch(X,Y,Z,'b');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
view(3);
pause;

% we plot the tangent plane at (1,1)
h=ezsurf('9.5+1*(x+0.5)+1*(y+0.5)',[-2 1 -2 1]);
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
set(h,'EdgeColor','r');
view(3);
axis([-2 2 -2 2 2 10]);
disp('This is the tangent plane at the point (-0.5,-0.5,9.5)');
pause;

figure(2);  hold off; clf; hold on;
ezplot('-x.^2+10',[-2 2]);
ezplot('11-2*x',[-2 2]);
axis([-2 2 7 11]);
scatter(1,9,'filled');
h=get(gca,'Title');
set(h,'String','');
grid on;
pause;

% An interior point with zero gradient which is a local maximum 
% =============================================================

disp('In next two plots, we identify points where the (partial)');
disp('   derivatives are zero and we have a maximum');

% we plot the function
figure(1);  hold off; clf; hold on;
ezsurf('-x.^2-y.^2+10',[-2 2 -2 2]);
axis([-2 2 -2 2 2 10]);
view(3);
pause;

% we plot f(x,0) and the relevant plane
h=ezplot3('t','0','10-t.^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[-2 2 2 -2]; Y=[0 0 0 0]; Z=[2 2 10 10];
h=patch(X,Y,Z,'b');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
view(3);
pause;

% we plot f(0,y) and the relevant plane
h=ezplot3('0','t','10-t.^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[0 0 0 0]; Y=[-2 2 2 -2]; Z=[2 2 10 10];
h=patch(X,Y,Z,'b');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
view(3);
pause;

% we plot the tangent plane at (1,1)
h=ezsurf('10',[-2 1 -2 1]);
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
set(h,'EdgeColor','r');
view(3);
axis([-2 2 -2 2 2 10]);
disp('This is the tangent plane at the point (0,0,10)');
pause;

figure(2);  hold off; clf; hold on;
ezplot('-x.^2+10',[-2 2]);
ezplot('10',[-2 2]);
axis([-2 2 7 11]);
scatter(0,10,'filled');
h=get(gca,'Title');
set(h,'String','');
grid on;
pause;


% An interior point with zero gradient which is not a local maximum 
% =================================================================

disp('In next two plots, we identify points where the (partial)');
disp('   derivatives are zero but we have no maximum');

% we plot the function
figure(1);  hold off; clf; hold on;
ezsurf('x.^2-y.^2',[-2 2 -2 2]);
axis([-2 2 -2 2 -3 3]);
view(3);
pause;

% we plot f(x,0) and the relevant plane
h=ezplot3('t','0','t.^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[-2 2 2 -2]; Y=[0 0 0 0]; Z=[-4 -4 4 4];
h=patch(X,Y,Z,'b');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
h=get(gca,'Title');
set(h,'String','');
view(3);
pause;

% we plot f(0,y) and the relevant plane
h=ezplot3('0','t','-t.^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[0 0 0 0]; Y=[-2 2 2 -2]; Z=[-4 -4 4 4];
h=patch(X,Y,Z,'b');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
h=get(gca,'Title');
set(h,'String','');
view(3);
pause;

% we plot the tangent plane at (0,0)
X=[-2 -2 2 2]; Y=[-2 2 2 -2]; Z=[0 0 0 0];
h=patch(X,Y,Z,'r');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
view(3);
axis([-2 2 -2 2 -4 4]);
disp('This is the tangent plane at the point (0,0,0)');
pause;

figure(2);  hold off; clf; hold on;
ezplot('x.^3',[-2 2]);
ezplot('0',[-2 2]);
axis([-1 1 -1 1]);
h=get(gca,'Title');
set(h,'String','');
scatter(0,0,'filled');
grid on;
pause;



