MATLAB: How to use gradient

quiver

what is the problem with the following code?
f=input('enter function: ','s')
[DX,DY] = -gradient(f);
f = str2func(['@(x,y)' vectorize(f)]);
[DX,DY] = -gradient(f);
hold on
quiver(X,Y,DX,DY)
hold off
I get the following errors:
Error using zeros CLASSNAME input must be a valid numeric or logical class name.
Error in gradient (line 56) g = zeros(size(f),class(f)); % case of singleton dimension

Best Answer

You have to evaluate ‘f’ to calculate the gradient.
The Code
f = 'log(x^2+y^2)';
f = str2func(['@(x,y)' vectorize(f)]);
x = -10:10;
y = x';
[DX,DY] = gradient(-f(x,y));
quiver(x,y,DX,DY,0); % The ‘0’ Argument Turns Off Arrow Scaling
The Plot
EDIT Added plot.