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
Click For Summary

Discussion Overview

The discussion focuses on plotting a separable function in 3D using polar coordinates, specifically the function defined as ##r = h(\theta)g(z)##. Participants explore methods for visualizing this function in a 3D space, addressing challenges and providing code snippets primarily in MATLAB.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant describes their initial success plotting ##r = f(\theta,z=0)## but seeks guidance on extending this to ##r = f(\theta,z)##.
  • Another participant suggests creating a meshgrid for ##z## and ##\theta##, followed by computing Cartesian coordinates and plotting using the surf command.
  • A participant expresses difficulty in achieving a satisfactory plot, noting that their output appears black and may be a 2D plot instead of 3D.
  • One response indicates that the appearance of only mesh lines could be due to the mesh being too close, recommending turning off the mesh lines to better visualize the surface.
  • Another participant advises on optimizing the creation of the AA matrix to improve performance by reducing the use of for loops.
  • A later reply mentions redefining the variable R to simplify the code, omitting the AA variable altogether.

Areas of Agreement / Disagreement

Participants provide various suggestions and code adjustments, but there is no consensus on a single method or solution, as different approaches are discussed and refined throughout the thread.

Contextual Notes

Some participants note potential issues with the size and orientation of matrices, as well as the performance implications of using for loops versus matrix operations. Specific assumptions about the values of variables and their dimensions are not fully resolved.

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   Reactions: 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   Reactions: member 428835
I actually ended up redefining R as A' instead of AA, omitting that variable altogether.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K