[X,Y] =
meshgrid(x,y) returns
2-D grid coordinates based on the coordinates contained in vectors x and y. X is
a matrix where each row is a copy of x, and Y is
a matrix where each column is a copy of y. The
grid represented by the coordinates X and Y has length(y) rows
and length(x) columns.
[X,Y,Z]
= meshgrid(x,y,z) returns
3-D grid coordinates defined by the vectors x, y,
and z. The grid represented by X, Y,
and Z has size length(y)-by-length(x)-by-length(z).
Create a 2-D grid with uniformly spaced x-coordinates and y-coordinates in the interval [-2,2].
x = -2:0.25:2;
y = x;
[X,Y] = meshgrid(x);
Evaluate and plot the function over the 2-D grid.
F = X.*exp(-X.^2-Y.^2);
surf(X,Y,F)
Starting in R2016b, it is not always necessary to create the grid before operating over it. For example, computing the expression implicitly expands the vectors x and y. For more information on implicit expansion, see Array vs. Matrix Operations.