Borrar filtros
Borrar filtros

Error : Value must be a vector or 2D array of numeric type

47 visualizaciones (últimos 30 días)
amine&&
amine&& el 6 de Jul. de 2016
Respondida: Hamid Reza N.D el 5 de Dic. de 2020
I want to draw my function of two variable so that the graph gives me Z values between -5 and 5. I used this code
[X,Y,Z] = meshgrid(0:.2:1, 0:.2:1, -5:.2:5);
Z = arrayfun(@mFunction, X, Y);
surf(X,Y,Z)
but i get the following error :
Error using surf (line 57)
While setting the 'XData' property of Surface:
Value must be a vector or 2D array of numeric type
Thanks!
  1 comentario
KSSV
KSSV el 6 de Jul. de 2016
what is that mFunction? It seems the output of mFunction is not double. Post mFunction here.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 6 de Jul. de 2016
Editada: Stephen23 el 6 de Jul. de 2016
The problem is how you are calling meshgrid, because calling it with three input arguments causes the outputs to be 3D arrays, not 2D arrays. So the solution is to simply call it like this:
[X,Y] = meshgrid(0:.2:1, 0:.2:1);
and the rest of your code will work fine.
Debugging for Beginners
This bug is easy to find: The error message tells you that the inputs to surf must be 2D arrays, but that they are not. So lets start by having a look at those arrays:
>> [X,Y,Z] = meshgrid(0:.2:1, 0:.2:1, -5:.2:5);
>> size(X)
ans =
6 6 51
Does 6x6x51 look like a 2D array to you? No, clearly the 51 on the third dimensions makes it a 3D array. Why is it a 3D array? Lets actually read the documentation for meshgrid: it clearly states that for three inputs " [X,Y,Z] = meshgrid(xgv,ygv,zgv) produces three-dimensional coordinate arrays." Now we know why the arrays are 3D: because you called meshgrid with three inputs. The documentation also tells us how to make 2D arrays, but I am sure you can figure this out yourself.
  6 comentarios
Stephen23
Stephen23 el 6 de Jul. de 2016
I do not have your mFunction, so I had to invent some data to test my code on. That is what that line does. If I did not invent some fake data, how would you expect me to test my code ?
amine&&
amine&& el 6 de Jul. de 2016
Editada: amine&& el 6 de Jul. de 2016
Ok i understand. Thank you Stephen.

Iniciar sesión para comentar.

Más respuestas (1)

Hamid Reza N.D
Hamid Reza N.D el 5 de Dic. de 2020
Hi. i wana draw my function of three variable. this is my code and i got those Errors. what shloud i do?
>> Q=[1 2 3;4 5 6;7 8 9];
>> S=[0;1;2];
>> u1=linspace(-10,10,20);
>> u2=linspace(-10,10,20);
>> u3=linspace(-10,10,20);
>> [U1,U2,U3]=meshgrid(u1,u2,u3);
>> L=1/2*(Q(1,1)*U1.^2+(Q(1,2)+Q(1,3)+Q(2,1)+Q(2,3)+Q(3,1)+Q(3,2))*U1.*U2.*U3+Q(2,2)*U2.^2+Q(3,3)*U3.^2)+S(1)*U1+S(2)*U2+S(3)*U3;
>> meshc(U1,U2,U3,L)
Error using matlab.graphics.chart.primitive.Surface/set
Value must be a vector or 2D array of numeric type
Error in matlab.graphics.chart.internal.ctorHelper (line 8)
set(obj, pvpairs{:});
Error in matlab.graphics.chart.primitive.Surface
Error in mesh (line 143)
hh = matlab.graphics.chart.primitive.Surface('XData',x,'YData',y,'ZData',z,'CData',c,...
Error in meshc (line 58)
hm = mesh(cax, x, y, z, c);

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by