Search code examples
matlab3drotationcurvesurface

Extracting Z values after 3D plot is generated by 2D curve revolution with repmat


I am stuck with an apparently simple problem. I have to revolve of 360° a 2D curve around an axis, to obtain a 3D plot. Say, I want to do it with this sine function:

z = sin(r);
theta = 0:pi/20:2*pi;
xx = bsxfun(@times,r',cos(theta));
yy = bsxfun(@times,r',sin(theta));
zz = repmat(z',1,length(theta));
surf(xx,yy,zz)
axis equal

I now want to visualize the numerical values of the Z plane, stored in a matrix. I would normally do it this way:

ch=get(gca,'children')
X=get(ch,'Xdata')
Y=get(ch,'Ydata')
Z=get(ch,'Zdata')
 

If I visualize Z with

imagesc(Z)

I don't obtain the actual values of Z of the plot, but the "un-revolved" projection. I suspect that this is related to the way I generate the curve, and from the fact I don't have a function of the type

zz = f(xx,yy)

Is there any way I can obtain the grid values of xx and yy, as well as the values of zz at each gridpoint?

Thank you for your help.


Solution

  • Instead of bsxfun you can use meshgrid:

    % The two parameters needed for the parametric equation
    h  = linspace(0,2) ;
    th = 0:pi/20:2*pi ;
    [R,T] = meshgrid(h,th) ;
    % The parametric equation
    %    f(x)    Rotation along Z
    %     ↓           ↓
    X = sin(R)  .*  cos(T) ;
    Y = sin(R)  .*  sin(T) ;
    % Z = h
    Z = R ;
    surf(X,Y,Z,'EdgeColor',"none")
    xlabel('X')
    ylabel('Y')
    zlabel('Z')
    

    Which produce:

    enter image description here

    And if you want to extract the contour on the X plane (X = 0) you can use contour:

    contour(Y,Z,X,[0,0])
    

    Which produce:

    enter image description here