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