"Undefined function or variable 'x'." for input of function

조회 수: 2 (최근 30일)
Curtnos
Curtnos 2016년 5월 21일
댓글: Curtnos 2016년 5월 21일
I had a function which was working perfectly for a while, however suddenly stopped working. The function is to plot a 2D vector field with inputs of the components in the x-direction (i) and y-direction (j) as well as mins and maxes.
function [ output ] = VecField(i,j,min,max)
% SCRIPT FOR PLOTTING VECTOR FIELDS
% Use defined range to create matrix of x,y values.
[x,y] = meshgrid(min:20:max);
% Plot vectors
quiver(x,y,i,j,'color','red','linewidth',1.2)
% Define axes based on given range
if min > 0
min_axes = 0.95*min;
else
min_axes = 1.05*min;
end
if max > 0
max_axes = 1.05*max;
else
max_axes = 0.95*max;
end
% Graph configuration
axis([min_axes max_axes min_axes max_axes])
title('Velocity vector field plot of over cylinder (inviscid)')
xlabel('x-axis')
ylabel('y-axis')
end
An example input which was previously working is: VecField(((-x-y)./((x.^2+y.^2).^0.5)),((x-y)./((x.^2+y.^2).^0.5)),-200,200)
While I can understand the error (Undefined function or variable 'x'.), I am unsure how to remedy it. If I enclose the functions in quotation marks, then the quiver function can no longer handle the inputs.
Are there any simple ways of getting around this?
  댓글 수: 3
dpb
dpb 2016년 5월 21일
Amplifying Stephen's comment, I don't see anything wrong w/ x,_y_ in your function but from
help quiver
...
quiver(X,Y,U,V) plots velocity vectors as arrows with components (u,v)
at the points (x,y). The matrices X,Y,U,V must all be the same size...
Your function creates a 2D grid for the positions, but passes in i and j which should be the velocity components. Looks peculiar; should they probably not also be output from meshgrid, perhaps???
Also, you have an [output] argument, but it is never defined???
Curtnos
Curtnos 2016년 5월 21일
As for the [output] argument, that was just the result of copying and pasting the shell of the code from another similar code I had haha. The function works well with the function aspect taken out:
min_val = -200;
max_val = 200;
[x,y] = meshgrid(min_val:20:max_val);
i_comp = ((-x-y)./((x.^2+y.^2).^0.5));
j_comp = (x-y)./((x.^2+y.^2).^0.5);
% Plot vectors
quiver(x,y,i_comp,j_comp,'color','red','linewidth',1.2)
% Define axes based on given range
if min_val > 0
min_axes = 0.95*min_val;
else
min_axes = 1.05*min_val;
end
if max_val > 0
max_axes = 1.05*max_val;
else
max_axes = 0.95*max_val;
end
% Graph configuration
axis([min_axes max_axes min_axes max_axes])
title('Velocity vector field plot of over cylinder (inviscid)')
xlabel('x-axis')
ylabel('y-axis')
grid on
grid minor

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 5월 21일
편집: Stephen23 2016년 5월 21일
1) When I run your code exactly as it is I get a totally different error:
>> VecField(1,2,0,100)
??? Error using ==> quiver at 58
The size of X must match the size of U or the number of columns of U.
So you need to clarify how you calling this function, and with what values.
2) Note that the quiver documentation clearly states that its input matrices must be of the same size: " quiver(x,y,u,v) plots vectors as arrows at the coordinates specified in each corresponding pair of elements in x and y. The matrices x, y, u, and v must all be the same size." There is absolutely nothing in your code to check or ensure that these matrices are the same size, so presumably it was only luck that allowed that function to work previously.
3) Also note that your variable names go from not-reccomended to extremely bad: you should never use the variable names i and j because these are the names of the inbuilt imaginary unit, and using the names min and max makes the inbuilt functions with the same names stop working: this could easily produce pointless bugs later. Change your variable names!
4) Get rid of the output variable: if you don't use, then don't define it.
  댓글 수: 1
Curtnos
Curtnos 2016년 5월 21일
That is a very good point with the variable names, I will change that immediately and make a note to avoid that in future scripts!
With the matrices sizes, I assumed that the matrices would always be of the same size in a script such as this (assuming that the input was a function) since the (previously called) i and j matrices are both created from scalar multiplications and matrix additions of the [x y] matrix and thus could not be of different sizes?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Vector Fields에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by