0
votes

I know this seems like a basic question—and it probably is. I am having trouble learning how to plot this function using meshgrid and surfc. I keep getting the error saying that "The surface Z must contain more than one row or column." Why am I getting this error? Here's what I have to do:

enter image description here

x = 0:.01:1;
y = 0:.01:1;

f = @(x, y) sin(4*pi.*x).*sin(y).*(1-y) + (1./sqrt(1+10.*(x.^2)+ 100.*(y-0.5).^2))
Z = f(x, y);

[X, Y] = meshgrid(x, y)
surfc(X, Y, Z)
1
Well, sounds like surfc is complaining about the size of Z. Check the sizes of X, Y, and Z using size(). Are they what you expect? And does that match the description of Z in help surfc? - Andrew Janke
X and Y are both 101 by 101. These make sense b/c I am using a meshgrid. However, Z is 1 by 101. This doesn't make sense since Z is suppose to be a 3d surface evaluated at 101 by 101 points. If this is correct, how do I figure out a way to get Z to be 101 by 101? - Jack L.
Take a close look at the x and y you're passing to the f() call. Are they also what you're expecting to be passing there, especially with respect to the meshgrid call? (Matlab is case sensitive.) - Andrew Janke
Ok thanks, got it. it should be f(X, Y) and the line Z = f(X, Y) should be moved below the next line. - Jack L.

1 Answers

0
votes

Got it:

x = 0:.01:1;
y = 0:.01:1;

f = @(x, y) sin(4*pi.*x).*sin(y).*(1-y) + (1./sqrt(1+10.*(x.^2)+ 100.*(y-0.5).^2))


[X, Y] = meshgrid(x, y)
Z = f(X, Y);

size(x)
size(Y)
size(Z)
surfc(X, Y, Z)