Wrong graph when trying to build z = x + y with MATLAB

I am trying to build 3D surfaces in MATLAB, and I used meshgrid, similar to what MATLAB textbooks said: http://www.mathworks.com/help/matlab/ref/meshgrid.html

I wrote a very simple three lines of script that I thought would create a surface z = x + y, and it looks like this:

[x , y] = meshgrid( linspace( 0 , 10 , 10 ) , linspace( 0 , 10 , 10 ) );
z = x + y;
surf( [ x , y , z] );

From what I understand, line 1 produces all combinations (x, y) of coordinates evenly spaced from 0 to 10. Then line 2 just applies the formula z = x + yto this exhaustive list of combinations. Then line 3 just displays all the dots (x, y, z).

But I got the following β€œthing” as output:

incorrect plot of z = x + y

I am sure that the graph in the figure above is not z = x + y, and I do not know why there are no two axes reaching a maximum value of 10.

However, I find the script too simple and see nothing wrong with that. Can someone point out where I forgot something? Thank.

+4
source share
1 answer

Your syntax for creating 3D coordinates is correct. Your call is surfincorrect. What you really need to do is to share it x, yand zon three separate parameters:

surf(x,y,z);

, . , MATLAB R2013a, parula, R2014b , , :

enter image description here

... x, y z, ? , [x,y,z] , x, y z 2D-, , 2D-, 10 30. surf 2D- , x 1 30, y 1 10, 2D- , surf z, z . , , , x 1 30, , .

x, y z.

+6

All Articles