Using surf or equivalent with spherical coordinates

122 次查看(过去 30 天)
I'm working on a model which develops some surfaces in spherical coordinates which I would like to plot. But surf is designed for Cartesian coordinates and the dimensions don't work out correctly to simply do surf(sph2cart(theta, phi, r)).
Is there a nice simple way to get 3D plots in spherical coordinates? I was kind of hoping this would be straightforward since I'm just using it to visually check my model results.

回答(2 个)

Fangjun Jiang
Fangjun Jiang 2011-11-5
Use function sph2cart() to calculate the data needed by surf().
Let's say you want to draw a sphere with radius of 10, Phi from -pi/3 to pi/3, Theta from 0 to pi. You can do the following.
R=10;
Phi=linspace(-pi/3,pi/3);
Theta=linspace(0,pi);
[Phi,Theta]=meshgrid(Phi,Theta);
[X,Y,Z]=sph2cart(Theta,Phi,R);
% Z=R*sin(Phi);
% X=R*cos(Phi).*cos(Theta);
% Y=R*cos(Phi).*sin(Theta);
surf(X,Y,Z);
  2 个评论
B K
B K 2011-11-5
There's a dimension mismatch because surf wants a nx1 x vector, mx1 y vector, then nxm z vector. That doesn't translate into sph2cart which wants dedicated sets of theta, phi, r to convert to x y z. Since the Cartesian coordinates are dependent on all three spherical coordinates I can't get them in the form for surf.
Fangjun Jiang
Fangjun Jiang 2011-11-6
What is your spherical coordinates data? I am making something up in the answer.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2011-11-5
No, there is no simple way to do 3D plots in spherical coordinates to display within any device which is inherently regularly-spaced cartesian coordinates. This is a mathematical issue, not a MATLAB problem.
To get the best approximation, you should first calculate the cartesian coordinates of each pixel to be rendered to, and back-project those to spherical coordinates, and do the function calculations at each of those spherical coordinates. This is the "pull" technique of rendering, whereas what you are using is the "push" technique (calculating destination coordinates from source coordinates), which is known to necessarily fail in some cases.
A problem, though, is that when you calculate the cartesian coordinates, you should take in to account the view projection from 3D to 2D that the rendering will necessarily undergo. Calculating proper source Z coordinates back from a flat image can be a bit tricky. And if you rotate the view, you need to recalculate destination and source locations and then recalculate the function values.
If this sounds like I am saying that you can either have accurate rendering or ease (and number of times) of function calculation, but not both, then Yes, that would be correct. "push" from spherical to cartesian will always give poor rendering, "pull" will require repeated recalculations as your view changes.

类别

Help CenterFile Exchange 中查找有关 Polar Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by