Trenera
Trenera

Reputation: 1505

MATLAB - Surf Plot data structure

I have done calculations with 2 different methods. For those calculations I have varied 2 parameters: x and y

In the end, I have calculated the % ERROR between both methods, for each variation. Now I want to create a 3D surface plot from the results:

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?

P.S. - I am currently trying to write some sample code with my first attempts. I will post it as soon as I get to somewhere.

Upvotes: 2

Views: 722

Answers (2)

hbaderts
hbaderts

Reputation: 14371

The surf function needs a grid of X,Y-values as input. 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. Use the unique function to select all unique values in x and y and create matrices of all possible combinations:

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

To create a Z matrix which fits the [X,Y] grid, the griddata function is helpful:

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

Now you can call surf with the grid matrices as input:

surf(X,Y,Z);

Upvotes: 7

naman arora
naman arora

Reputation: 51

create the grid for the first and second column and calculate Z using your formula. help meshgrid in MATLAB

Upvotes: 0

Related Questions