Here is an example: Suppose that A and B are points in the plane whose distance we wish to find. The points are given to us in polar coordinates \((r_a,\theta_a)\) and \((r_b,\theta_b)\) as you can see in this picture:
>> pdist = @(ra,ta,rb,tb) sqrt(ra.^2 + rb.^2 – 2*ra.*rb.*cos(tb-ta));
>> dAtoB = pdist(2,pi/6, 5,3*pi/4) dAtoB = 5.8461
A word of caution. The expression that defines the anonymous function can include pre-assigned MATLAB variables.
a = 2; b = 3; ellipse = @ (x, y) a*x^2 + b*y^2
The values are captured when the function is defined, i.e. the function is NOT changed when new values are assigned to a and b .