How can I plot a separable function in 3D using polar coordinates?

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    3d Plot
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
member 428835
Hi PF!

I'm trying to plot in 3D. I have ##r = f(\theta,z=0)## and it plots well. However, I would like to plot ##r = f(\theta,z)## in 3D. Any idea how to do this? The radius is actually a separable function ##r = h(\theta)g(z)##, so I have ##h## plotted for a given ##\theta## vector and ##g## plotted for a given ##z## vector.

I should say, I tried using the polar plot command to initially plot ##r = f(\theta,z=0)## but this was taking so long that parameterized ##x(\theta),y(\theta)## and plotted normally. In this air, I was thinking ##x = h(\theta)g(z)\cos\theta## and ##y=h(\theta)g(z)\sin\theta##, but I don't know how to plot this in 3D.

My question to you is, how would you plot this in 3D?

Thanks!
 
Last edited by a moderator:
on Phys.org
Since you have ##z## and ##\theta## as parameters, first create a meshgrid for them:
Code:
[Z,THETA] = meshgrid(zmin:dz:zmax,2*pi*(0:0.01:1));
(replace the zmin, dz, and zmax variables with whatever you need)

Then compute the Cartesian coordinates corresponding to this grid and plot:
Code:
R = h(THETA).*g(Z);
X = R.*cos(THETA);
Y = R.*sin(THETA);
surf(X,Y,Z);
Of course you could skip the separate lines for computing X and Y and just put those computations directly into the surf command. I put them outside for clarity. You could in principle also compute h and g before doing the meshgrid and then make a meshgrid in them to save some evaluations of h and g, but this is easier to overview.

Edit: Depending on your aim, you might also want to play with the color assignments. Otherwise MATLAB with color based on the Z-values.
 
So I'm not getting a good plot. This is what I have
Code:
dz = 0.01;
z = 0:dz:1;
AA = zeros(size(A,2),length(z));
for ii = 1:length(z)% change A from vector to matrix
    AA(:,ii) = A';
end

[Z,THETA] = meshgrid(z,(thetaS:dtheta:2*pi));
R = (1-eps*AA.*cos(k*pi*Z));
X = R.*cos(THETA);
Y = R.*sin(THETA);
surf(X,Y,Z);
where ##A## is a THETA -length vector and has already been determined for THETA earlier in the code. The size of ##AA,THETA,X,Y,Z## are all the same rectangular matrices. Attached is the plot I get. It looks okay but is all black, which makes me think it is only a 2D plot. Any idea what I'm doing wrong?
 

Attachments

I would also suggest that you create your AA matrix in a different way:
Code:
AA = ones(length(z),1)*A';
In general, MATLAB will run faster the less for loops you put. You should try to avoid for loops as much as possible. Do not do with a for loop what you can do with matrix multiplication.

Edit: You might have to correct this to get the A values in the correct direction, but you get the idea.
 
  • Like
Likes   Reactions: member 428835
I actually ended up redefining R as A' instead of AA, omitting that variable altogether.