% Matlab Self Paced example: File = plt3d.m
% Contour and surface plots

% Plot interval for x variable
xl = [-3 3]; 
% Number of plot points in x variable
%nx = 7;
nx = 61;
% Plot interval for y variable
yl = [-4 4];
% Number of plot poitns in y variable
%ny = 9;
ny = 81;

x = linspace(xl(1), xl(2), nx);
y = linspace(yl(1), yl(2), ny);

% Note that X, Y (and F below) are ny by nx arrays
% Even though x has nx elements and y has ny elements
[X, Y] = meshgrid(x, y);

% Function from Matlab Self Paved tutorial
F = (X.^3-3*X) + Y.^2;
% Another function to try
%R = X.^2 + Y.^2;
%F = exp(-R/4) .* cos(X.^2 + 6*Y + 1);

figure(1)
%[c, h] = contour(x, y, F, 15); clabel(c,h); str0 = 'Contour';
mesh(x, y, F); str0 = 'Mesh';
%surf(x, y, F); str0 = 'Surf';
%surfc(x, y, F); str0 = 'Surfc';

% Interpolated shading with no grid lines when more grid points
%shading interp

str1 = 'plot of f(x,y) = x^3 - 3 x + y^2';
str = sprintf('%s %s using nx = %d, ny = %d', str0, str1, nx, ny);
title(str)
xlabel('x')
ylabel('y')
grid on