Plotting Multivariable x, y, and z for a data set

I have made a 3d multivatiable gird that is, x, y, and z
grid_x = 1:1:10;
grid_y = 1:1:10;
grid_z = 1:1:10;
then in a 3 loop (3d multivariable) I calculate values for a give equation and save is it in a variable called values(:,:). the multivariable values has points between 1:10 random numbers
Now I have used
[X,Y] = meshgrid(grid_x,grid_y);
Z = griddata(grid_x,grid_y,grid_z,X,Y);
however Z is not where the values are, when I call
surf(X,Y,Z)
I get a empty plot in 3d, here is where I am not understand how to call in the data points which is called values, even when I did
surf(X,Y,Z,values(:,:))
I get a empty plot in 3d.
Please tell me to clarify if needed.

 Respuesta aceptada

Star Strider
Star Strider el 5 de Oct. de 2020
The problem is with your choice of coordinates.
When I ran your code, it threw:
Warning: The underlying triangulation is empty - the points may be collinear.
> In griddata>useScatteredInterp (line 183)
and ‘Z’ was empty.
Creating random values instead resulted in the sort of result you want:
grid_x = rand(1,50);
grid_y = rand(1,50);
grid_z = randn(size(grid_x));
[X,Y] = meshgrid(0:0.01:1, 0:0.01:1);
Z = griddata(grid_x,grid_y,grid_z,X,Y);
figure
surf(X,Y,Z)
grid on
.

13 comentarios

Thank you for the correction, it helps to construct the plot to display the values in. Now how do get the values to be used in the plot as opposed the Z values.
here is what I am not understanding after the gird is made then I run through the loop, which saves all the values in a variable called values, using the mulivariable plot grid that was made to plot the values in that plot?
SugerCodeCude
SugerCodeCude el 5 de Oct. de 2020
Editada: SugerCodeCude el 5 de Oct. de 2020
Ok here is what I did
is it correct?
grid_x = rand(1,50);
grid_y = rand(1,50);
grid_z = rand(1,50);
[X,Y,Z] = meshgrid(0:0.01:1, 0:0.01:1, 0:0.01:1);
val = griddata(grid_x,grid_y,grid_z,values ,X,Y,Z);
figure
surf(X,Y,Z,val )
grid on
Star Strider
Star Strider el 5 de Oct. de 2020
Now how do get the values to be used in the plot as opposed the Z values.
I have no idea what you are asking. The ‘Z’ values created by griddata are the ‘Z’ values used in the plot. The whole purpose of using greddata is to interpolate the vectors to create the surface.
here is what I am not understanding after the gird is made then I run through the loop, which saves all the values in a variable called values, using the mulivariable plot grid that was made to plot the values in that plot?
I am lost. There is no explicit loop in the code you posted.
SugerCodeCude
SugerCodeCude el 5 de Oct. de 2020
Editada: SugerCodeCude el 5 de Oct. de 2020
I understand I will be more clear,
grid_x = rand(1,50);
grid_y = rand(1,50);
grid_z = randn(size(grid_x));
[X,Y] = meshgrid(0:0.01:1, 0:0.01:1);
Z = griddata(grid_x,grid_y,grid_z,X,Y);
row = 1;
col= 1;
d = 1;
values = zeros(0,0,0);
for z = grid_z
for x = grid_x
for y = grid_y
%in the grid I like for values to be displayed
% in the sqrt; values are made any numbers between the give grid
values(row, col, d) = sqrt(mean(rand(1,50)));
row=row+1;
end
row=1;
col=col+1;
end
col=1;
d=d+1;
end
figure
surf(X,Y,Z)
grid on
Star Strider
Star Strider el 5 de Oct. de 2020
For many reasons, (1) that code does not make sense, and (2) will likely not do what you believe it should do. I do not understand the reason you go through the trouble of calculating ‘Z’ then completely ignore it in the loop.
Please describe what you want to do. How do the vectors and the resulting matrices fit into it?
Star Strider
Star Strider el 5 de Oct. de 2020
SugerCodeCude posted as an Answer —
Ok bear with me I think I am understanding what you are showing me in the code and what you are tell me. I will now explain and tell me where I am going wrong
Star Strider
Star Strider el 5 de Oct. de 2020
Please do so, however in a Comment rather than an Answer!
3 Dimensions, x- and y- axis having all the same range between 1-10, and z has 10 dimension, and in dimension 1 for z it has a range between 1-10.
grid_x = 1:1:10;
grid_y = 1:1:10;
% here will be the grid for the 10 dimensions
grid_z = 1:1:10;
% make the meshgrid
[X,Y] = meshgrid(grid_x, grid_y);
% Here in the loop; is where you are saying I am not using the Z variable
% to put my values in,
% rather I'm putting it into another variable called values
for z = grid_z
for x = grid_x
for y = grid_y
% Here is where I am not using Z to store the value I get
% please tell me do you see where I am making the mistake
% I think this is used here.
% I can't understand where this comes into play?
% pseudo code
Z = griddata(grid_x,grid_y,grid_z,rand(),X,Y);
end
end
end
figure
surf(X,Y,Z)
grid on
You are telling me to use Z, I believe I am having a problem in how to implemented it in? I do understand what you are telling me.
For Z in dimension 1 it will go through the loop and get the values, then next dimension 2 it will have all the values for it... so on. I guess that is what I need help on.
and for simplicity sake I am making the grid 10x10x10 (x, y, z);
Please tell me if I'm in the ballpark
Star Strider
Star Strider el 5 de Oct. de 2020
There is no need to use a loop (or any sort) to store the values of ‘Z’. It exists as a matrix, and you can easily address any part of it, either using its indices or using griddedinterpolant (with a bit of extra coding) to get any values you want from it, within its boundaries. If you want to suqare its elements (or do several other calculations with them), that is easily done as well (in most instances, depending on what you want to do).
Simply put, the loop is not necessary at all. MATLAB has a number of straightforward ways to do what you want with ‘Z’ (within limits, since not everything is possible). You simply have to tell it what you want to do. (It would also help to tell me what you want to do, if you want my help with it.)
It is starting to make sence, here is why I am using the loop and tell me how and where to implement the Z
grid_x = 1:1:10;
grid_y = 1:1:10;
grid_z = 1:1:10;
% make the meshgrid
[X,Y] = meshgrid(grid_x, grid_y);
% Here in the loop; Why the loop?
for z = grid_z
for x = grid_x
for y = grid_y
% the loop variable z, x, and y are passed into a function to give values
% for each of the grid points
% [values_1]=function(z,x,y)
% a for loop then I compares the values_1 to another values_2
% (values_2 are data points I have)
% bestFit(i)= min(abs(values_1-values_2)) by using a for loop between a small range
% i++;
% end the for loop
%then I do the sqrt and the mean, how can I use the Z to put in the values I get
% weather I use griddata or griddedinterpolant I think I need help here.
Z = griddata(grid_x,grid_y,sqrt(mean(bestFit(:))),X,Y);
end
end
end
Yes, I do agree no need to use the loop, as I am very fond of Matlabs power. but this is why I use the loop, and you are right I can use the sqrt and mean in the Z, and not use the loop as Matlab will take care of it.
however, I get it Z is 10 by 10 ok, for each sqrt and mean of the bestFit values I like it to go into it matrix place (x, and y position)
By the way THANK YOU!! SO MUCH for bearing with me
I still do not understand what you are doing, however if you want to calculate the squared difference between your ‘values_2’ matrix and ‘Z’ (that both must be the same sizes), just do this:
squared_difference = (Z - values_2).^2;
and be done with it without the explicit loop. Putting the griddata call inside the loop does not make sense.
Thank you so mch, this back and forth help me understand the issues I was having. Thank you again!
Star Strider
Star Strider el 6 de Oct. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Oct. de 2020

Comentada:

el 6 de Oct. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by