# Undefined unary operator '.'' for input arguments of type 'function_handle'.

8 views (last 30 days)
Clarisha Nijman on 20 Nov 2019
Commented: Clarisha Nijman on 21 Nov 2019
Hi,
I'm using matlab 2017b and want to find the gradient of a function with two arguments as defined in the mathwork site:
[FX,FY] = gradient(F) returns the x and y components of the two-dimensional numerical gradient of matrix F. The additional output FY corresponds to ∂F/∂y, which are the differences in the y (vertical) direction.
This is my code:
b=@(x,y) (x-1).^2+(x.^2+y).^2;
The error I got is:
Undefined unary operator '.'' for input arguments of type 'function_handle'.
How do I solve this? Can somebody help me or give me a hint?

Robert U on 20 Nov 2019
Edited: Robert U on 20 Nov 2019
Hi Clarisha Nijman,
your b is a function handle to the anonymous function with input arguments x and y. Gradient itself is - as stated in the documentation - the 2-D gradient of a matrix. In order to calculate the matrix you would have to create it.
b=@(x,y) (x-1).^2+(x.^2+y).^2;
[X,Y] = meshgrid(-100:1:100,-100:1:100);
Please, pay attention that gradient needs the grid having equidistant distribution and if spacing is not "one" you have to scale the result.
Using symbolic toolbox you can apply gradient to scalar functions, which would need you to change your function definition.
Kind regards,
Robert
Clarisha Nijman on 21 Nov 2019
Okee,
I see, you are zooming in on that area by adjusting the meshGrid such that the point of interest in the middle. I was really wondering how to pick the right answer out of the grid.
Now it is clear to me.
Thank you very much,
kind regards,
Clarisha