MATLAB: 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 ….”

errorplot

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)

Best Answer

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.
Related Question