Drawing upside down cone using spherical coordinates.
7 views (last 30 days)
Show older comments
Viktor Karlsson
on 30 Apr 2021
Commented: Viktor Karlsson
on 3 May 2021
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.
hh = matlab.graphics.chart.primitive.Surface(allargs{:});
0 Comments
Accepted Answer
Benjamin Kraus
on 30 Apr 2021
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
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!