Search code examples
matlabmatlab-figure

Gap in MATLAB surface plot


I'm trying to plot a cone in MATLAB using the following code. However, when MATLAB generates the plot, there is a gap in the surface as shown in the image below. Would anyone be able to suggest a way to close it?

clearvars; close all; clc;

[theta, r] = meshgrid(-pi:0.1:pi, -4:0.1:6);
x = (r-1).*cos(theta);
y = (r-1).*sin(theta);
z = r;

% 3-D plot
figure
surf(x, y, z);
xlabel("x"); ylabel("y"); zlabel("z");
zlim([0 8]);
axis square

enter image description here


Solution

  • The problem is that the list of theta stops before reaching pi because the increments of 0.1 do not reach the upper bound.

    For example, you may use the line

    [theta, r] = meshgrid(-pi:(2*pi/20):pi, -4:0.1:6);
    

    to complete the circle in 20 steps.