clear; tic
% Title: 3D plotting of x*exp(-x^2-y^2)
% Question: What is necessary to represent a surface in three dimensions?
% Step 1) Create the matrix you would like to visualize.
Nx = 18;
Ny = 20;
x = linspace(-3,3,Nx);
y = linspace(-2,2,Ny);
% Method 1 of creating surface matrix
Z1 = zeros(Nx,Ny);
for ii = 1:length(x)
        for jj = 1:Ny
               Z1(ii,jj) = x(ii)*exp(-x(ii)^2-y(jj)^2);
        end
end
% Method 2 of creating surface matrix
[X1, Y1] = ndgrid(x,y); % We will discuss 'ndgrid' and 'meshgrid' in the future
Z2 = X1.*exp(-X1.^2-Y1.^2); %Question: What are the '.' for? See note 1 below.
% Question: What do you think of method 1 and method 2 to create matrix Z?
% What are the merits of each method? What are the downsides of each
% method?
% Note 1:
% MATLAB stands for Matrix Labratory so basic functions like *, / and ^ assume
% that they are operating on matricies. Above we don't want a matrix
% multiplication but a point by point multiplication. Same with the square
% function, we want to do that point by point.
%
% I typed 'doc *' and it brought me the list of arithmetic operators.
% plus - Plus +
% uplus - Unary plus +
% minus - Minus -
% uminus - Unary minus -
% mtimes - Matrix multiply *
% times - Array multiply .*
% mpower - Matrix power ^
% power - Array power .^
% mldivide - Backslash or left matrix divide \
% mrdivide - Slash or right matrix divide /
% ldivide - Left array divide .\
% rdivide - Right array divide ./
zdiff = Z1-Z2;
maxZdiff = max(zdiff(:)) % We will discuss the max functions in a future lesson.
% Plot the surfaces
% Here is how you get a quick view of a surface but you aren't always sure
% of the axis.
figure(22),
        surf(Z1)
        zlabel('z')
        title('x*e^{-x^2-y^2}')
%Make sure you play with all the figure window buttons. Zoom, pan, rotate, etc
% Below are equilvalent ways of plotting the surface:
figure(23),
surf(y,x,Z1)
xlabel('y'),ylabel('x'),zlabel('z')
title('x*e^{-x^2-y^2}')
figure(24),
        surf(x,y,Z1.')
        xlabel('x'),ylabel('y'),zlabel('z')
        title('x*e^{-x^2-y^2}')
figure(25),
        surf(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surf plot with matrix inputs x*e^{-x^2-y^2}')
% Below are some other plotting options which we will go into more details
% in the future.
figure(26)
        surfc(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surf plot with contours x*e^{-x^2-y^2}')
figure(27)
        surf(X1,Y1,Z2,'edgecolor','none')
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surf plot with no mesh x*e^{-x^2-y^2}')
figure(28)
        mesh(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('mesh plot of x*e^{-x^2-y^2}')
figure(29)
        surfl(X1,Y1,Z2)
        xlabel('x'),ylabel('y'),zlabel('z')
        title('surfl plot of x*e^{-x^2-y^2}')
        shading interp
        colormap('copper')
% Question: What did you like about this lesson? What would you like to
% learn more about?
toc