% demo_2d_functions
% File 61
% Stavros Toumpis, 2008
% Freshman Calculus
%
% This demo plots various 2d functions
%
% PARTS
% =====
% PART A: We explain the concept of a 2-var function with examples
% PART B: We explain the concept of contour lines with a detailed example
% PART C: A few examples with contours

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART A: We explain the concept of a 2-var function with examples
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('********************************************');
disp('Part A: We plot various 2 variable functions');

% f(x,y)=x;
figure(1); clf;
ezsurf('x',[-9 9 -9 9]);
h=get(gca,'Title');
set(h,'String','');
axis([-10 10 -10 10 -10 10]);
disp('Plotted function is f(x,y)=x');
pause;

% f(x,y)=y;
figure(2); clf;
ezsurf('y',[-9 9 -9 9]);
h=get(gca,'Title');
set(h,'String','');
axis([-10 10 -10 10 -10 10]);
disp('Plotted function is f(x,y)=y');
pause;

% f(x,y)=x+y;
figure(3); clf;
ezsurf('x+y',[-9 9 -9 9]);
h=get(gca,'Title');
set(h,'String','');
axis([-10 10 -10 10 -20 20]);
disp('Plotted function is f(x,y)=x+y');
pause;

% f(x,y)=r;
figure(1); clf;
ezsurf('sqrt(x.^2+y.^2)',[-2 2 -2 2],60);
h=get(gca,'Title');
set(h,'String','');
axis([-2 2 -2 2 0 4]);
disp('Plotted function is f(x,y)=r=sqrt(x^2+y^2)');
pause;

% f(x,y)=r^2=(x^2+y^2);
figure(2); clf;
ezsurf('x.^2+y.^2',[-9 9 -9 9]);
h=get(gca,'Title');
set(h,'String','');
axis([-10 10 -10 10 0 180]);
disp('Plotted function is f(x,y)=r^2=x^2+y^2');
pause;

% f(x,y)=exp(-x^2-y^2);
figure(3); clf;
ezsurf('exp(-x.^2-y.^2)',[-2 2 -2 2]);
h=get(gca,'Title');
set(h,'String','');
axis([-2 2 -2 2 0 1]);
disp('Plotted function is f(x,y)=exp(-x^2-y^2)');
pause;

% f(x,y)=exp(-x^2-y^2-x*y);
figure(1); clf;
ezsurf('exp(-x.^2-y.^2-x*y)',[-2 2 -2 2]);
h=get(gca,'Title');
set(h,'String','');
axis([-2 2 -2 2 0 1]);
disp('Plotted function is f(x,y)=exp(-x^2-y^2-xy)');
pause;

% f(x,y)=x/(x^2+y^2);
figure(2); clf;
ezsurf('x/(x.^2+y.^2)',[-2 2 -2 2]);
h=get(gca,'Title');
set(h,'String','Going to +/- Infinity at (0,0)');
axis([-2 2 -2 2 -10 10]);
disp('Plotted function is f(x,y)=x/(x^2+y^2)');
pause;

% f(x,y)=(x^2-y^2)/(x^2+y^2);
figure(3); clf;
h=ezsurf('(x^2-y^2)/(x.^2+y.^2)',[-2 2 -2 2],1000);
set(h,'Edgealpha',0);
h=get(gca,'Title');
set(h,'String','A function with a discontinuity at 0');
axis([-2 2 -2 2 -2 2]);
disp('Plotted function is f(x,y)=(x^2-y^2)/(x^2+y^2)');
disp('  The limit at (0,0) depends on angle of approach!');
pause;

% f(x,y)=x^3-3*x*y.^2
figure(1); clf;
ezsurf('x.^3-3*x*y.^2',[-2 2 -2 2],60);
h=get(gca,'Title');
set(h,'String','Monkey saddle');
axis([-2 2 -2 2 -20 20]);
disp('Plotted function is f(x,y)=x^3-3*x*y.^2 (monkey saddle)');
pause;

% f(x,y)=sin(r)/r
figure(2); clf;
ezsurf('sin(x.^2+y.^2)/(x.^2+y.^2)',[-4 4 -4 4],60);
h=get(gca,'Title');
set(h,'String','Sombrero');
axis([-4 4 -4 4 -0.5 1]);
disp('Plotted function f(x,y)=sin(r)/r=sin(x^2+y^2)/(x^2+y^2) (Sombrero)');
pause;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART B: We explain the concept of contour lines with a detailed example
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('***********************************************');
disp('Part B: We explain the concept of contour lines');

% f(x,y)=x^2+y^2;
figure(1); clf;
ezsurf('x.^2+y.^2',[-9 9 -9 9]);
h=get(gca,'Title');
set(h,'String','');
axis([-10 10 -10 10 0 180]);
pause;

cycle=[cos((1:1:100)*2*pi/100)' sin((1:1:100)*2*pi/100)'];
hold on; 
axis([-10 10 -10 10 0 200]);

h=ezsurf('20',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5);
plot(sqrt(20)*cycle(:,1),sqrt(20)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;
h=ezsurf('40',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5); 
plot(sqrt(40)*cycle(:,1),sqrt(40)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;
h=ezsurf('60',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5);
plot(sqrt(60)*cycle(:,1),sqrt(60)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;
h=ezsurf('80',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5); 
plot(sqrt(80)*cycle(:,1),sqrt(80)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;
h=ezsurf('100',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5);
plot(sqrt(100)*cycle(:,1),sqrt(100)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;
h=ezsurf('120',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5); 
plot(sqrt(120)*cycle(:,1),sqrt(120)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;
h=ezsurf('140',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5); 
plot(sqrt(140)*cycle(:,1),sqrt(140)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;
h=ezsurf('160',[-10 10 -10 10]); set(h,'Edgealpha',0); set(h,'Facealpha',0.5); 
plot(sqrt(160)*cycle(:,1),sqrt(160)*cycle(:,2));
axis([-10 10 -10 10 0 200]); pause;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART C: A few examples with contours
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('********************************************');
disp('Part C: We show a few examples with contours');

% f(x,y)=x+y;
figure(2); hold off; clf;
h=ezsurfc('x+y',[-9 9 -9 9]);
set(h,'Linewidth',2);
h=get(gca,'Title');
set(h,'String','');
axis([-10 10 -10 10 -20 20]);
pause;

% f(x,y)=exp(-x^2-y^2-x*y);
figure(3);  hold off; clf; hold on;
ezsurf('exp(-x.^2-y.^2-x*y)+1',[-2 2 -2 2]);
h=ezcontour('exp(-x.^2-y.^2-x*y)',[-2 2 -2 2]);
set(h,'Linewidth',2);
h=get(gca,'Title');
set(h,'String','');
axis([-2 2 -2 2 0 2]);
view(3);
pause;

% f(x,y)=x^3-3*x*y.^2
figure(1);  hold off; clf;
h=ezsurfc('x.^3-3*x*y.^2',[-2 2 -2 2],60);
set(h,'Linewidth',2);
h=get(gca,'Title');
set(h,'String','');
axis([-2 2 -2 2 -20 20]);
pause;
