MATLAB: How to generate a contour map with rand on the axes

axescontourmeshgridplotrandom

I am not sure if this is what I'm supposed to be doing.
rng(1776);
x = (rand(300,1)-0.5)*6;
y = (rand(300,1)-0.5)*6;
[X,Y] = meshgrid(x, y);
Z = some function
contour(X,Y,Z)
How should my code be to make this correct?

Best Answer

In contour function, X and Y should be increasing or decreasing along one dimension. To do so, please sort x and y before meshgrid, like:
rng(1776);
x = (rand(300,1)-0.5)*6;
y = (rand(300,1)-0.5)*6;
x = sort(x);
y = sort(y);
[X,Y] = meshgrid(x, y);
Z = ...(some function)
contour(X,Y,Z)