====== Math 445: 3D graphics ======
Matlab vocabulary
* meshgrid
* pcolor
* contour, contourf
* surf, surfc
* quiver
* shading (flat, faceted, or interp)
* colorbar
* axis (equal, tight)
===== meshgrid: =====
The ''meshgrid'' function is essential for Matlab's 3D graphics. Meshgrid creates 2D arrays of x,y data covering the x,y, plane, over which a function can be evaluated and graphed. Example:
>> x = linspace(-2,2,5)
x =
-2 -1 0 1 2
>> y = linspace(-3,3,7)
y =
-3 -2 -1 0 1 2 3
>> [X,Y] = meshgrid(x,y)
X =
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
Y =
-3 -3 -3 -3 -3
-2 -2 -2 -2 -2
-1 -1 -1 -1 -1
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
It is conventional to use capital letters (X,Y) for the matrix output of meshgrid from small letter (x,y) vector inputs. Observe how the output matrices X varies left to right, along the x axis, and Y varies up and down, along the y axis. Together they provide x,y, coordinates for a grid of points on the x,y plane in the region $-2 \leq x \leq 2$, $-3 \leq y \leq 3$.
We can then evaluate a function $z = f(x,y)$ over that 2D array via elementwise matrix operations. For example, this Matlab code would evaluate $z = f(x,y) = x^2 + y^2$
Z = X.^2 + Y.^2
===== pcolor: pseudocolor plot =====
The ''pcolor'' function produces a pseudocolor or checkerboard plot of Z as a function of x,y.
pcolor(X,Y,Z)
colorbar
axis equal
axis tight
xlabel('x'); ylabel('y'); title('z = x^2 + y^2')
You can modify the appearance of the pseudocolor with the ''shading'' command. Try
''shading flat'', ''shading interp'', and ''shading faceted''.
===== contour, contourf =====
The ''contour'' function plots contours or level curves of z=f(x,y). That is, it plots curves
on which f(x,y) is constant.
[X,Y] = meshgrid(x,y);
Z = X.^2 + Y.^2;
contour(X,Y,Z)
xlabel('x'); ylabel('y'); title('z= x^2 + y^2')
colorbar
axis equal
axis tight
As you can see, the level curves of $z = f(x,y) = x^2 + y^2$ are circles $x^2 + y^2 = c$.
''contourf'' is the same as ''contour'', except that the regions between contour lines are filled with color.
===== surf, surfc =====
The previous plots were all looking straight down at the (x,y) plane, with the value of z = f(x,y) encoded as a color. The ''surf'' function will plot z = f(x,y) in 3D, as a surface of height z over the (x,y) plane.
surf(X,Y,Z) % draw z=f(x,y) as a surface over x,y
xlabel('x'); ylabel('y'); zlabel('z')
axis equal; axis tight
It's also possible to draw more complicated surfaces (surfaces that are not simple graphs of the form
$z = f(x,y)$). Here's an example of how to draw a sphere by parameterizing its surface in terms of angles $\phi$ and $\theta$.
% make mesh over theta, phi
theta = linspace(0,2*pi,50); % angle between x and y
phi = linspace(0,pi,25); % angle down from z axis
[Theta, Phi] = meshgrid(theta, phi); % form 2D mesh in theta, phi
% Parameterize surface of sphere in terms of theta, phi
% (note that x^2 + y^2 + z^2 = 1)
X = cos(Theta).*sin(Phi);
Y = sin(Theta).*sin(Phi);
Z = cos(Phi);
% Draw parametrized surface of sphere with surf
surf(X,Y,Z); axis equal
xlabel('x'); ylabel('y'); zlabel('z')
axis equal