Skip to main content
Logo image

Section 8.6 Anonymous Functions with Single Variable

Some functions are really too short to merit creating an entire function file for them. For that reason, simple mathematical functions can be declared as anonymous functions.
An anonymous function is a simple one-line user-defined function that does not require its own m-file. Here is the syntax:
name = @ (arg1, arg2, ..., argn) expr
name is the name by which you wish to refer to the function. The @ operator creates a so-called function handle, which is the way in which MATLAB stores functions. The arguments of the function are listed in parenthesis and expr is the expression that defines the function (and involves the arguments).
Examples:
>> cube = @ (x) x^3;
>> ellipse = @ (x, y) 16*x^2 + 9*y^2;
This defines two functions; the first one cubes the input value, the second one depends on two input parameters. You can now evaluate these functions:
>> num = cube(2)
num =
    8
Your own functions can be used just like any other MATLAB function. In particular, you can use a function as an argument to another function:
>> num2 = ellipse(cube(2), 3)
num2 =
    1105

Activity 8.1.

Define a function
\begin{equation*} f(x) = \sin{(x)}\times \cos{(x)} \end{equation*}
and evaluate \(f\) at \(x = 0\text{,}\) \(\pi /4\) and \(\pi /2\text{.}\)
Here are some more examples:
>> superfunk = @(x) exp(-x^2*.01)*cos(x)^2
superfunk =
    @(x)exp(-x^2*.01)*cos(x)^2
>> superfunk(5)
ans =
    0.0626656
>> superfunk(1)
ans =
    0.289022
>> superfunk(superfunk(1))
ans =
    0.917999
Summing up, this is what you need to know about anonymous functions:
  • The expression can have several independent variables (arguments).
  • The arguments can be named anything; typically you might use just a single letter. Any letter can be used for the independent variables except you should avoid i and j since these stand for the imaginary unit.
  • The expression can include built-in or user-defined functions.
  • You can use element-by-element or linear algebra operations so that array inputs become possible.
  • The anonymous function can be used as arguments to other functions.
  • Function handles provide means for using the function and passing it to other functions.
  • Anonymous functions can be defined anywhere, including inside a script file.

Subsection 8.6.1 When and How to use Anonymous Functions

  • When the value of a relatively simple mathematical expression has to be determined many times in a program.
  • User-defined functions defined and written within the computer code, i.e. not in a separate file.
  • Can be defined in any part of MATLAB, i.e. Command Window, Script Files, Function Files.
It is possible to write your anonymous functions so that they can also operate on vector or array input, just like the other MATLAB functions do. Let’s take a look at an example. Recall our cube function above:
>> cube = @ (x) x^3;
If you were to try to enter a vector for x you’d encounter an error message:
>> x = [2 3];
>> cube(x)
>> 
Error using ^ (line 51).
\(\color{red}{\text{Error using } \hat{ } \text{ (line 51).}}\)
\(\color{red}{\text{Incorrect dimensions for raising a matrix to a power. Check that the}}\)
\(\color{red}{\text{matrix is square and the power is a scalar. To perform elementwise}}\)
\(\color{red}{\text{matrix powers, use `.}\hat{ }\text{ '.}}\)
\(\color{red}{\text{Error in @}(\text{x})\text{x }\hat{ } \text{ 3}}\)
What we really meant was for each element of x to be cubed individually. For this to happen we need to change our definition of the function so as to use dot operators:
>> cube = @ (x) x.^3;
Note the subtle difference in the definition: The cube operation is now using the dot operator. Take a look:
>> x = [2 3];
>> cube(x)
>> 
ans =
         8    27
Here is another example: if we wanted to redefine our above superfunk so as to allow array input would do so as follows:
>> superfunk = @(x)exp(-x.^2*.01).*cos(x).^2;
>> superfunk(0:0.1:0.4)
ans =
    1.0000    0.9899    0.9601    0.9118    0.8470

Activity 8.2.

Define a function
\begin{equation*} f(x,y) = x^2 - y^2 \end{equation*}
and evaluate it on a grid of \((x,y)\) values with
\begin{equation*} -1 \leq x \leq 1\ \text{ and} \ -2 \leq y \leq 2 \end{equation*}