Drawing upside down cone using spherical coordinates.

5 次查看(过去 30 天)
I wrote the following code:
phi=pi/6;
theta=linspace(0,2*pi,63); %Theta goes from 0 to 2*pi.
a=linspace(0,1,21);
[PHI,THETA,A]=meshgrid(phi,theta,a);
X=A.*sin(PHI).*cos(THETA); % Spherical coordinates
Y=A.*sin(PHI).*sin(THETA);
Z=A.*cos(PHI);
surf(X,Y,Z)
axis equal
It won't draw, but instead gives me an empty 2D graph.
The error message is:
Error using matlab.graphics.chart.primitive.Surface
Value must be a vector or 2D array of numeric type.
Error in surf (line 145)
hh = matlab.graphics.chart.primitive.Surface(allargs{:});

采纳的回答

Benjamin Kraus
Benjamin Kraus 2021-4-30
The issue you are facing is that when you call meshgrid you are specifying three input vectors, so the output matrices are going to be 3D matrices, but surf only accepts 2D matrices.
My recommendation is to remove phi from the call to meshgrid:
phi=pi/6;
theta=linspace(0,2*pi,63);
a=linspace(0,1,21);
[THETA,A]=meshgrid(theta,a);
X=A.*sin(phi).*cos(THETA);
Y=A.*sin(phi).*sin(THETA);
Z=A.*cos(phi);
surf(X,Y,Z)
axis equal

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by