Error : Value must be a vector or 2D array of numeric type
47 次查看(过去 30 天)
显示 更早的评论
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 个评论
KSSV
2016-7-6
what is that mFunction? It seems the output of mFunction is not double. Post mFunction here.
采纳的回答
Stephen23
2016-7-6
编辑:Stephen23
2016-7-6
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 个评论
Stephen23
2016-7-6
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 ?
更多回答(1 个)
Hamid Reza N.D
2020-12-5
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!