Anonymous Functions

Another way to perform the circle calculations is to use anonymous functions.

This can be done directly at the command prompt (or in our current file).

We type

circumference = @(r) 2*pi*r
circumference = 

    @(r)2*pi*r

and

area = @(r) pi*r.^2
area = 

    @(r)pi*r.^2

Anonymous functions are evaluated like standard math functions.

To get the circumference of a circle with radius 5 we use

circumference(5)
ans =

   31.4159

To get both values we use, e.g.,

circledata = [circumference(5); area(5)]
circledata =

   31.4159
   78.5398

Vectorized usage:

circledata = [circumference([2 4 6]); area([2 4 6])]
circledata =

   12.5664   25.1327   37.6991
   12.5664   50.2655  113.0973