There are several problems in your code:
- You are already converting spherical coordinates to cartesian coordinates with the
sin(theta)*cos(phi)
andsin(theta)*sin(phi)
bit. Why are you callingpol2cart
on this (moreover, we're not working in polar coordinates!)? - As natan points out, there is no third dimension (i.e.
z
) in your plot. For unity radius,r
can be omitted in the spherical domain, where it is completely defined bytheta
andphi
, but in the cartesian domain, you have all threex
,y
andz
. The formula forz
isz = cos(theta)
(for unit radius). You didn't read the documentation for
surf
, which says:surf(Z,C)
plots the height ofZ
, a single-valued function defined over a geometrically rectangular grid, and uses matrixC
, assumed to be the same size asZ
, to color the surface.In other words, your
surf(x,y)
line merely plots the matrixx
and colors it usingy
as a colormap.
Here's the above code with the mistakes fixed and plotted correctly:
[f,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
x = sin(t)*cos(f);
y = sin(t)*sin(f);
z = cos(t);
surf(x,y,z)