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

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    3d Plot
AI Thread Summary
To plot a separable function in 3D using polar coordinates, start by creating a meshgrid for the parameters z and theta. Compute the Cartesian coordinates using the equations X = R*cos(theta) and Y = R*sin(theta), where R is derived from the separable function r = h(theta)g(z). If the plot appears too dark, it may be due to mesh lines obscuring the surface; turning off the mesh lines can help. Additionally, for better performance, minimize the use of for loops by employing matrix operations instead. This approach will enhance the clarity and efficiency of your 3D plot.
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:
Physics news 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

By default MATLAB plots the mesh lines, your mesh might be too close to show you the rest of the surface. If it is you will only see the mesh lines unless you turn the mesh lines off, i.e., set 'LineStyle' of the plot to 'none'.
 
  • Like
Likes member 428835
Perfect! Thanks so much!
 
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 member 428835
I actually ended up redefining R as A' instead of AA, omitting that variable altogether.
 

Similar threads

Replies
1
Views
2K
Replies
8
Views
2K
Replies
2
Views
3K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top