I need to plot the following functions but I receive this error "Invalid indexing or function definition. When defining a function ensure that the arguments are symbolic variables ...."

조회 수: 3 (최근 30일)
x = 2:30;
y = 40:60;
u = @(x,y) 10*sin(200*x)+0.1*cos(10*y)+(100*x)./y;
v = @(x,y) 10*cos(200*x)+0.1*sin(10*y)-100*(x./y);
syms x y
ex = diff(u,x);
ey = diff(v,y);
Gxy = diff(v,x)+diff(u,y);
[X,Y] = meshgrid(x,y);
Z = ex(X,Y);
figure(1)
mesh(X, Y, Z)
[X,Y] = meshgrid(x,y);
Z = ey(X,Y);
figure(2)
mesh(X, Y, Z)
[X,Y] = meshgrid(x,y);
Z = Gxy(X,Y);
figure(3)
mesh(X, Y, Z)

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 8일
You have defined
x = 2:30;
y = 40:60;
but then you have
syms x y
That is equivalent to
x = sym('x');
y = sym('y');
which is to say that it overwrites x and y, and afterwards they are no longer numeric. But afterwards you sometimes treat them as symbols and sometimes treat them as numeric. In particular,
[X,Y] = meshgrid(x,y);
attempts to treat them as numeric.
You also have
u = @(x,y) 10*sin(200*x)+0.1*cos(10*y)+(100*x)./y;
v = @(x,y) 10*cos(200*x)+0.1*sin(10*y)-100*(x./y);
ex = diff(u,x);
ey = diff(v,y);
The two diff() lines attempt to take the symbolic differentiation of a MATLAB function handle. You probably cannot do that. You can differentiate a symbolic expression, and you might be able to differentiate a symbolic function, but probably not a function handle.
ex = diff(u(x,y), x);
ey = diff(v(x,y), y)
would invoke the function handles on the symbolic parameters x and y, giving a symbolic expression as a result, and that symbolic expression can be differentiated.
  댓글 수: 2
mohsen
mohsen 2016년 3월 8일
I removed the syms x y
and replaced the diff with
ex = diff(u(x,y), x);
ey = diff(v(x,y), y);
but I still get an error what else I should change?
Walter Roberson
Walter Roberson 2016년 3월 8일
With x and y numeric and u a function handle, u(x,y) would be numeric, so diff(u(x,y),x) would be a numeric difference rather than a symbolic difference. When you request a numeric difference, the second parameter must be the number of times the difference is to be taken, not a variable indicating a direction. That is not going to be what you want. You need to be using symbolic expressions and to have x and y being symbolic. What you should be doing is renaming your numeric uses of x and y.
Note: you do not need to define x or y before defining a function handle that uses those names as parameters in @(x,y) . Your first need for your numeric x and y is in your meshgrid(). You also do not need to recalculate the meshgrid() since you are not changing X or Y or the numeric x or y between your meshgrid() calls. So you could just eliminate your use of x and y as numeric and directly call
[X, Y] = meshgrid(2:30, 40:60);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by