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

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;
[dbdX,dbdY] = gradient(b)
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?

 Accepted Answer

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);
[dbdX,dbdY] = gradient(b(X,Y));
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

4 Comments

Note that symbolic gradient gives you back a formula for the gradient, which could then be evaluated at multiple points.
Note that numeric gradient can only be an approximation of the gradient around a particular point.
It turns out that there is a file exchange contribution numjac that can approximate the Jacobian of a function handle around a point. In the case of a function with one output, that would be the same as gradient.
Ok I see indeed all combinations of x and y (Meshgrid)
So if I understand it good:
dbdX is the derivative of x for every combination of x and y
dbdY is the derivative of y for every combination of x and y
and if for the given function b=@(x,y) (x-1).^2+(x.^2+y).^2, I would like to have the gradient in x=1 and y=0
manually I would just say:
dbdX=@(x,y) 2*x-4*x*(-x.^2+y)-2;
dbdY=@(x,y) -2*x.^2+2*y;
and by substitution in that specific point (1,2)
How does this matrix system work?
Hi Clarisha Nijman,
you should check your analytical solution.
Chosen resolution has an influence on the numerically obtained result.
% define point to evaluate
xToEval = 1;
yToEval = 0;
% base function
b=@(x,y) (x-1).^2+(x.^2+y).^2;
% analytical solution
dbdXf=@(x,y) 2.*(x-1) + 4.*(x.^2+y).*x;
dbdYf=@(x,y) 2.*(x.^2 + y);
dbdXRef = dbdXf(xToEval,yToEval);
dbdYRef = dbdYf(xToEval,yToEval);
% resolution of grid
resGrid = 0.1;
% define grid
[X,Y] = meshgrid(xToEval-1:resGrid:xToEval+1,yToEval-1:resGrid:yToEval+1);
% gradient on grid
[dbdX,dbdY] = gradient(b(X,Y),resGrid,resGrid);
% point of interest is in the center of the result matrix
dbdX = dbdX((size(X,1)+1)/2,(size(X,1)+1)/2);
dbdY = dbdY((size(Y,1)+1)/2,(size(Y,1)+1)/2);
Kind regards,
Robert
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

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!