简体   繁体   English

MATLAB - Surf Plot数据结构

[英]MATLAB - Surf Plot data structure

I have done calculations with 2 different methods. 我用2种不同的方法进行了计算。 For those calculations I have varied 2 parameters: x and y 对于那些计算,我有两个参数:x和y

In the end, I have calculated the % ERROR between both methods, for each variation. 最后,我计算了两种方法之间的%ERROR,用于每种变化。 Now I want to create a 3D surface plot from the results: 现在我想根据结果创建一个3D表面图:

x -> on x axis
y -> on y axis
Error -> on z axis

Here is a sample of the data: 以下是数据示例:

A = [
                   -0.1111                     1.267          9.45680081826912
                   -0.1111                       2.6          212.361735695025
                     -0.25                     1.533          40.5729362609655
                     -0.25                     2.867          601.253624894196
                   -0.4286                         1          0.12116749607863
                   -0.4286                       3.4          79.6948438921078
                   -0.6667                     2.067          33.3495544017519
                   -0.6667                     3.667          141.774875517481
                        -1                       2.6       -0.0399171449531781
                   0.09091                     1.533            163.7083541414 ];

But, when I try to plot it with surf function: 但是,当我尝试用冲浪功能绘制它时:

x = A(:,1);
y = A(:,2);
z = A(:,3);

surf(x,y,z)

, I get an error: ,我收到一个错误:

Error using surf (line 75)
Z must be a matrix, not a scalar or vector
Error in ddd (line 27)
surf(x,y,z)

Can you help me with a code that can restructure the data in the format acceptable for the surf function? 你能帮我一个能够以冲浪功能可接受的格式重组数据的代码吗?

PS - I am currently trying to write some sample code with my first attempts. PS - 我正在尝试用我的第一次尝试编写一些示例代码。 I will post it as soon as I get to somewhere. 我一到某个地方就会发布它。

The surf function needs a grid of X,Y -values as input. surf函数需要一个X,Y的网格作为输入。 Your data however is simply three vectors with some combinations, not a full grid. 然而,您的数据只是三个向量,有一些组合,而不是一个完整的网格。 As described in the documentation, the meshgrid function is often helpful to create such grid matrices. 如文档中所述, meshgrid函数通常有助于创建此类网格矩阵。 Use the unique function to select all unique values in x and y and create matrices of all possible combinations: 使用unique函数选择xy所有唯一值,并创建所有可能组合的矩阵:

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

To create a Z matrix which fits the [X,Y] grid, the griddata function is helpful: 要创建适合[X,Y]网格的Z矩阵, griddata函数很有用:

Z = griddata(x,y,z,X,Y);

Now you can call surf with the grid matrices as input: 现在,您可以使用网格矩阵作为输入调用surf

surf(X,Y,Z);

create the grid for the first and second column and calculate Z using your formula. 为第一列和第二列创建网格,并使用您的公式计算Z. help meshgrid in MATLAB 在MATLAB中帮助meshgrid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM