Drawing upside down cone using spherical coordinates.

조회 수: 6 (최근 30일)
Viktor Karlsson
Viktor Karlsson 2021년 4월 30일
댓글: Viktor Karlsson 2021년 5월 3일
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
  댓글 수: 1
Viktor Karlsson
Viktor Karlsson 2021년 5월 3일
Thank you so much for taking the time to help, works like a charm!

댓글을 달려면 로그인하십시오.

추가 답변 (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