matlabmatlab-figure

How to rotate a 2-D plot around the y-axis to turn it into a radial symmetry 3-D plot in matlab?


I have a plot of 3 traveling waves at time 200(I use the drawnow function) that looks like below. enter image description here In fact, this is a cross-section of radial symmetric traveling waves in 3 dimension(just like a circle that is expanding). So the 3-D plot is just this 2-D plot rotating 360 degrees around the y-axis. I want to draw this 3-D plot of the traveling waves. How can I do this in matlab?

I tried to use the polarplot function in matlab. But it does not support 3-D plot.


Solution

  • Since you didn't provide us with demo data I had to create my own. Let's consider the following y1 and y2 curves:

    %% Demo curves
    npt = 101 ; maxX = 20 ;
    Xdelay = 8 ;
    x = linspace(0,maxX,npt);
    y1 = exp(x-Xdelay) ./ (1+exp(x-Xdelay)) ;
    y2 = fliplr(exp(x-(maxX-Xdelay)) ./ (1+exp(x-(maxX-Xdelay))))*.9 ;
    
    plot(x,y1);hold on;
    plot(x,y2);
    grid on ; ylim([0 1.1])
    

    This will give something looking like some of your curves: original curve

    The important part for the next step is that you have a y vector defined for each of the curve you want to rotate around the Y axis.

    The idea is to copy this y curve into every theta steps of a cylindrical coordinate referential grid. Then convert back everything to cartesian for use with surf().

    %% place curves into 3D cylindrical coordinates
    r   = x ;                           % The [r] dimension is our old [x] dimension
    ntt = 50 ;                          % define how many angular division for the plot
    theta   = linspace(0,2*pi,ntt) ;    % create all the angular divisions
    [rr,tt] = meshgrid(r,theta) ;       % generate a grid 
    
    zz1 = repmat( y1 , ntt , 1 ) ;      % replicate our "y1" vector for all theta steps
    
    [X,Z1,Y] = pol2cart(tt,rr,zz1) ;    % convert everything to cartesian coordinates
    
    % display surface
    hs1 = surf(X,Y,Z1,'FaceAlpha',0.3,'EdgeAlpha',0.2)
    xlabel('X');ylabel('Y');zlabel('Z'); hold on
    
    % overlay original curve for verification
    z = zeros(size(x)) ;
    hp1 = plot3(x,y1,z,'b','LineWidth',3)
    

    This gives you the curve y1 rotated around the Oy axis. Note how the output of pol2cart are ordered in order to respect the rotation around Oy: rotated y1

    Now to repeat this on any other curve is trivially the same method:

    %% Now do the same with `y2`
    zz2 = repmat( y2 , ntt , 1 ) ;      % replicate our "y2" vector to match the grid
    [X,Z2,Y2] = pol2cart(tt,rr,zz2) ;   % convert everything to cartesian coordinates (X will stay the same)
    
    hs2 = surf(X,Y2,Z2,'FaceAlpha',0.3,'EdgeAlpha',0.2)
    hp2 = plot3(x,y2,z,'r','LineWidth',3)
    

    Which yields: enter image description here