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

  • MATLAB
  • Thread starter member 428835
  • Start date
  • Tags
    3d Plot
This solved the problem of the 2D plot and it now shows a 3D plot as intended. Thanks for all your help!In summary, the conversation discussed plotting in 3D using parameters and a separable function. The suggested method was to create a meshgrid for the parameters, compute the Cartesian coordinates, and then use the surf command to plot the surface. It was also mentioned to avoid using for loops and to correct the direction of the A values for accurate plotting.
  • #1
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
  • #2
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.
 
  • #3
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

  • surface.pdf
    45.2 KB · Views: 337
  • #4
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
  • #5
Perfect! Thanks so much!
 
  • #6
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
  • #7
I actually ended up redefining R as A' instead of AA, omitting that variable altogether.
 

1. What are polar coordinates and how are they different from Cartesian coordinates?

Polar coordinates are a way of representing points in a two-dimensional plane using a distance from the origin (r) and an angle from a fixed reference line (θ). This is different from Cartesian coordinates, which use x and y coordinates to pinpoint a location on a grid.

2. What is a separable function and how is it different from a non-separable function?

A separable function is a mathematical function that can be expressed as the product of two or more simpler functions. This means that the function can be broken down into smaller parts that are easier to work with. In contrast, a non-separable function cannot be expressed as the product of simpler functions.

3. How do I convert a function from Cartesian coordinates to polar coordinates?

The conversion from Cartesian coordinates (x,y) to polar coordinates (r,θ) can be done using the following formulas:
r = √(x² + y²) and θ = tan⁻¹(y/x). These formulas allow you to map the Cartesian coordinates onto a polar coordinate system, making it easier to plot a function in polar coordinates.

4. Can any function be plotted in polar coordinates?

Yes, any function can be plotted in polar coordinates as long as it can be expressed as a combination of simpler functions. However, some functions may be more difficult to plot in polar coordinates compared to Cartesian coordinates.

5. How can I visualize a separable function in 3D using polar coordinates?

To plot a separable function in 3D using polar coordinates, you can use a 3D graphing tool or software to plot the function in a polar coordinate system. You can also manually plot the function by converting it to polar coordinates and then plotting it on a polar coordinate grid.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
767
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
758
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
265
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
227
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top