Plotting a 3-D Function with Interpolated Shading

I am having issues with the meshgrid ( http://www.mathworks.com/help/matlab/ref/meshgrid.html ) function. I need X and Y to be a 21x25 matrix but this function is only generating a 21x26 matrix when I use the following command:
[X,Y] = meshgrid(0:.04:1, 0:.024:0.5);
Z = cos(2*pi*(3.*X - 4.*Y))
surf(X,Y,Z)
X and Y are bounded by the following:
0<= X <= 1 (Need 25 data points)
&
0<= Y <= 0.5 (Need 21 data points)
I figure that 1 / 25 should give me 25 total data points for the X bounary and 0.5 / 21 should give me 21 total data points for the Y boundary.
However, meshgrid is giving me a 21x26 matrix. Is the parameter argument incorrect for the function?

 采纳的回答

[X,Y] = meshgrid(linspace(0,1,25), 0:.024:0.5);
Z = cos(2*pi*(3.*X - 4.*Y))
surf(X,Y,Z)
The linspace command will gave you the same result as 0:1/24:1, as I believe increments of 1/24 would give you the number of data points you desire.
It's a bit easier to use linspace instead of the colon operator when your main concern is the length of the array you're creating, not the spacing.

4 个评论

That did it for me! Although, why wouldn't manually incrementing by 0.04 work? For instance I used the command:
[X,Y] = meshgrid(0:.04:1, 0:.024:0.5);
This should be the same as saying,
[X,Y] = meshgrid(0:1/24:1, 0:.024:0.5);
or
[X,Y] = meshgrid(linspace(0,1,25), 0:.024:0.5);
Either of the three should work the same, correct?
In decimal form, 1/24 is 0.0416666667. While it does round to 0.04, 0.04 is equal to 1/25 which will give you 26 elements.
Sounds great. That was my mistake. I was assuming the correct expression would be 1/25 because I need 25 data points so (1-0)/25, but I was neglecting that were are starting at 0 and therefore I didn't account for it.
Thanks for your help Evan. I greatly appreciate it!
I make that same mistake quite often. And no problem! Glad you have it working.

请先登录,再进行评论。

更多回答(0 个)

类别

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by