% Plot the function z(x,y) = e^-r cos r^2 where r = sqrt(x^2 + y^2), i.e.,
% a series of decreasing ripples around the origin. This script plots the
% function for x and y values ranging from -2 pi to 2 pi. This is a
% prototype for a (small) CSci 120 programming project.

%% Set up the x and y values.
low = -2 * pi;
high = 2 * pi;
steps = 51;     % Number of grid lines in graph
x = linspace( low, high, steps );
y = linspace( low, high, steps );

%% Calculate a matrix of z values. To avoid explicit loops, I base this
% calculation on a matrix whose rows are repetitions of each x value,
% and another whose columns are repetitions of each y value. Any
% calculation involving the i-th x value and the j-th y value can thus
% be done by using element (i,j) of these matrices--in particular of the
% x matrix for x_i and of the y matrix for y_j.
[ x2d, y2d ]= meshgrid( x, y );
r2 = x2d .^ 2 + y2d .^ 2;
z = exp( -sqrt(r2) ) .* cos( r2 );

%% Plot the function.
figure;
surf( y, x, z );
axis( [ low high low high -0.5 1.0 ] );