Search code examples
matlabmathpolar-coordinates

Why not spherical plot? How to plot 3D-polar-plot in Matlab?


[r,t] = meshgrid(linspace(0,2*pi,361),linspace(0,pi,361));
[x,y]=pol2cart(sin(t)*cos(r),sin(t)*sin(r));
%[x,y]=pol2cart(r,t);
surf(x,y);

enter image description here

I played with this addon but trying to find an default function to for this. How can I do the 3D-polar-plot?

I am trying to help this guy to vizualise different integrals here.


Solution

  • There are several problems in your code:

    • You are already converting spherical coordinates to cartesian coordinates with the sin(theta)*cos(phi) and sin(theta)*sin(phi) bit. Why are you calling pol2cart 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 by theta and phi, but in the cartesian domain, you have all three x, y and z. The formula for z is z = cos(theta) (for unit radius).
    • You didn't read the documentation for surf, which says:

      surf(Z,C) plots the height of Z, a single-valued function defined over a geometrically rectangular grid, and uses matrix C, assumed to be the same size as Z, to color the surface.

      In other words, your surf(x,y) line merely plots the matrix x and colors it using y 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)
    

    enter image description here