HOW TO DO MATLAB SURFACE PLOTS.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here's how to use the parametric description of T&F14.6 to make a nice surface
plot in MATLAB. Pages 118-125 in the Pratap MATLAB book explain this some (but
use more limited versions of the SURFL and MESH commands).
Say you want to call your two parameters u and v. For example say u and v
represent phi and theta from spherical coordinates. First make u and v into
MATLAB vectors which have the desired range of values. The commands below give
u and v 50 evenly spaced values in the range needed to cover the points on a
sphere.
u = linspace(0, pi, 50);
v = linspace(0, 2*pi, 50);
Now comes the clever command MESHGRID
[U,V] = meshgrid(u,v);
Meshgrid makes two arrays U and V. Every row of U is u. Every column of V is v.
both U and V have as many columns as the number of elements of u and as many rows
as the number of elements of v. To get the idea of what MESHGRID does type this
command (or type HELP MESHGRID): [A, B] = meshgrid([1 2 3],[2 4]).
So now U is an array. Each element of U is going to correspond to a point
on the surface. The value of the parameter u is given by the value of that
element of the array. Similarly, the value of an element of V is the value
of v for the corresponding point on the surface.
Now, we use the parameters to describe the surface! We need to write x, y, and z
in terms of the parameters u and v, just like in mathland. Say for a sphere of radius
R = 7 we would write:
R = 7;
X = R* sin(U) .* cos(V);
Y = R* sin(U) .* sin(V);
Z = R* cos(U);
We have made three more arrays X, Y, and Z. The 2,3 element of these
arrays are the x,y, and z values on a sphere corresponding to the 2nd value of v
and the third value of u. So the array of values in X is all the X coordinates
of the points on the surface. Likewise for Y and Z. Note that you need to
use .* when multiplying so that you get element by element array multiplication.
Now we make the plot. There are a few different ways of doing this, depending
on what appearance you like. Any of the three commands below will work
mesh(X,Y,Z) or surf(X,Y,Z) or surfl(X,Y,Z)
So, putting the commands above together, here is the example just explained.
Cut and paste this into a script file or the command window and see what it does.
%SAMPLE SURFACE PLOT: HEMISPHERE (using spherical coordinates as parameters)
u = linspace(0, pi, 50);
v = linspace(0, 2*pi, 50);
[U,V] = meshgrid(u,v);
R = 7;
X = R* sin(U) .* cos(V);
Y = R* sin(U) .* sin(V);
Z = R* cos(U);
mesh(X,Y,Z)
Here are two more samples as free bonuses. They plot the hemisphere with
a cone. The example above could have a cone added to it as well, but it was
left off for simplicity. You can cut and paste the examples above and below into
a script file or the command window to see what they do. .
Here is how to put a sphere and a cone on top of each other
%HEMISPHERE and CONE on top of each other (using polar coordinates as parameters)
r = 0:.05 :1;
theta = 0:pi/20:2*pi;
[R, THETA] = meshgrid(r,theta);
X = R.*cos(THETA);
Y = R.*sin(THETA);
Zsphere = sqrt(1-R.^2);
mesh(X,Y,Zsphere) %makes the plot
hidden off % turns off hidden line removal so you can see through the surfaces
Zcone = R;
hold on
mesh(X,Y,Zcone)
hold off ; hidden on
Here is how to use x and y as parameters for a surface plot
%HEMISPHERE and CONE (using x and y as parameters)
x = -1.5:.2:1.5;
y = x;
[X,Y] = meshgrid(x,y);
one = ones(size(X));
Zsq = one - X.^2 - Y.^2;
positivesphere=sqrt((Zsq+abs(Zsq))/2); % clunky, but prevents sqrt(negatives)
surf(X,Y,positivesphere);
hold on;
cone = sqrt(X.^2 + Y.^2);
mesh(X,Y,cone)
hold off