

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART D: We explain partial derivatives
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('********************************************');
disp('Part D: We explain partial derivatives');

% f(x,y)=x^3-3*x*y.^2
figure(2);  hold off; clf; hold on;
h=ezsurfc('x.^3-3*x*y.^2',[-2 2 -2 2],200);
axis([-2 2 -2 2 -10 10]);
h=get(gca,'Title');
set(h,'String','');
view(3);
pause;

h=ezplot3('t','-1','t.^3-3*t*(-1).^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[-2 2 2 -2]; Y=[-1 -1 -1 -1]; Z=[-10 -10 10 10];
h=patch(X,Y,Z,'r');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
axis([-2 2 -2 2 -10 10]);
h=get(gca,'Title');
set(h,'String','');
view(3);
pause;

h=ezplot3('1','t','1.^3-3*1*t.^2',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
X=[1 1 1 1]; Y=[-2 2 2 -2]; Z=[-10 -10 10 10];
h=patch(X,Y,Z,'b');
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
axis([-2 2 -2 2 -10 10]);
h=get(gca,'Title');
set(h,'String','');
view(3);

disp('Each intersecting plane specifies a 1D function, whose derivative...');
disp('  ... is the partial derivative');
pause;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PART E: We explain differentiability and tangent planes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

disp('*******************************************************');
disp('Part D: We explain tangent planes and differentiability');

% we plot the function -x.^2-y.^2+10
% ==================================
figure(3);  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');
h=get(gca,'Title');
set(h,'String','');
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');
h=get(gca,'Title');
set(h,'String','');
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('The plotted function -x^2-y^2+10 is differentiable.');
disp('This is the tangent plane at the point (-0.5,-0.5,9.5)');
pause;

% we plot the function -sqrt(abs(x*y))
% ====================================
% 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('The plotted function is f(x,y)=r=sqrt(x^2+y^2)');
disp('The function r=sqrt(x^2+y^2) is not differentiable.');
pause;

% we plot the function -sqrt(abs(x*y))
% ====================================
figure(2);  hold off; clf; hold on;
h=ezsurf('-sqrt(abs(x*y))',[-2 2 -2 2],1000);
set(h,'Linestyle','none');
axis([-2 2 -2 2 -3 0.5]);
view(3);
pause;

% we plot f(x,0) and the relevant plane
h=ezplot3('t','0','-0.03',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
h=get(gca,'Title');
set(h,'String','');
X=[-2 2 2 -2]; Y=[0 0 0 0]/2; Z=[-3 -3 0.5 0.5];
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','-0.02',[-2 2]);
set(h,'Linewidth',3);
set(h,'Color','k');
h=get(gca,'Title');
set(h,'String','');
X=[0 0 0 0]; Y=[-2 2 2 -2]; Z=[-3 -3 0.5 0.5];
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('0',[-2 1 -2 1]);
set(h,'Facealpha',0.5);
set(h,'Linestyle','none');
set(h,'EdgeColor','r');
view(3);
axis([-2 2 -2 2 -3 0.5]);
disp('The plotted function -sqrt(abs(xy)) is NOT differentiable');
disp('This is NOT the tangent plane at the point (0,0)');
pause;
