MATLAB: Error: The size of X must match the size of U or the number of columns of U.

quiver

What's the problem with this code? when I run it the following Error occurs:
Error using quiver
The size of X must match the size of U or the number of columns of U.
syms x y;
f=input('enter function: ','s');
f = symfun(eval(f), [x y]);
u=-diff(f,x);
v=-diff(f,y);
[X,Y]=meshgrid(-10:.5:10);
quiver(X,Y,u,v);

Best Answer

There is no need to make the code slower and more complicated by using syms and eval. Exactly as I wrote in my answer to your related question, simple numeric calculations will do everything you need:
% str = input('enter a function in x and y: ','s');
str = 'log(x^2+y^2)';
fun = str2func(['@(x,y)',str]);
[X,Y] = meshgrid(-10:.51:10);
[DX,DY] = gradient(arrayfun(fun,X,Y));
quiver(X,Y,DX,DY,5);