MATLAB: I keep getting the error “Error using mesh (line 75) Z must be a matrix, not a scalar or vector.”

error using mesh (line 75) z must be a matrixnot a scalar or vector.

if true
% code
end
disp('Question #1');
x = 5:0.1:5;
y = 5:0.1:5;
[X,Y] = meshgrid(x,y);
R = sqrt(X.^2 + Y.^2);
z = (-1*cos(2*R))./(exp(0.2*R));
mesh(X,Y,z)
xlabel('x')
ylabel('y')
zlabel('z')
title('Mesh Plot')
grid on

Best Answer

You probably intended to define ‘x’ and ‘y’ this way:
x = -5:0.1:5;
y = -5:0.1:5;
Make that change and your code works!