尝试使用MATLAB绘制z = x + y时图形不正确 [英] Incorrect graph when trying to plot z = x + y with MATLAB

查看:225
本文介绍了尝试使用MATLAB绘制z = x + y时图形不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MATLAB中绘制3D曲面,并且我使用了meshgrid,类似于MATLAB教程在这里说的内容:

的错误图

我很确定上图中的图形不是z = x + y,而且我不知道为什么没有两个轴达到最大值10.

不过,我发现脚本太简单了,看不到任何错误.谁能指出我忽略了什么地方?谢谢.

解决方案

您生成3D坐标的语法是正确的.您对surf的呼叫不正确.您实际需要做的是将xyz分为三个单独的参数:

surf(x,y,z);

执行此操作时,您会获得此表面.请注意,生成的图形是使用MATLAB R2013a绘制的,因此显示的颜色图不是R2014b及更高版本中可用的parula颜色图,但是表面将是您要查找的正确的颜色图:

...现在为什么需要分开xyz点来创建曲面?原因是因为执行[x,y,z]意味着您正在级联 xyz坐标为单个2D信号,所以这是什么事实是您正在创建一个10 x 30的2D信号.使用此单个2D数组调用surf会自动假定x值的范围是1到30,而y值的范围是1到10,并且是跨越surf图的轴的2D值网格以及所显示的z值,其中z值源自之前创建的级联矩阵.如果查看生成的图,可以看到x值从1到30,显然不是您想要的.

您需要将xyz值分开以达到所需的平面.

I am attempting to plot 3D surfaces in MATLAB, and I made use of meshgrid, similar to what the MATLAB tutorials said here: http://www.mathworks.com/help/matlab/ref/meshgrid.html

I wrote a very simple three line script that I believed would produce the surface z = x + y and it is as follows:

[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 of (x,y) coordinates evenly spaced from 0 to 10. Then line 2 just applies the formula z = x + y to that exhaustive list of combinations. Then line 3 just plots all the (x, y, z) points.

But I got the following "thing" as output:

I'm pretty sure the graph in the above picture is not z = x + y, and I have no clue why there aren't two axes going up to maximum value 10.

Still, I find the script too simple and couldn't see anything wrong with it. Could anyone point out where I overlooked something? Thank you.

解决方案

Your syntax for generating the 3D coordinates is right. Your call to surf is incorrect. What you actually need to do is separate x, y and z into three separate parameters:

surf(x,y,z);

When you do that, you get this surface. Take note that the figure generated was using MATLAB R2013a, so the colour map shown is not the parula colour map that is available as of R2014b and up, but the surface will be the right one that is what you're looking for:

... now why do you need to separate your x, y and z points to create the surface? The reason why is because doing [x,y,z] means that you are concatenating the x, y and z coordinates into a single 2D signal, and so what's happening is that you are creating a 2D signal that is 10 x 30. Calling surf with this single 2D array automatically assumes that the x values span from 1 to 30 and the y values span from 1 to 10 and those are the 2D grid of values that span the axis of your surf plot in conjunction with the z values shown, where the z values originate from the concatenated matrix created earlier. If you look at the plot you generated, you can see the x values are spanning from 1 to 30, and that's obviously not what you want.

You need to separate the x, y and z values to achieve the desired plane.

这篇关于尝试使用MATLAB绘制z = x + y时图形不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆