Anonymous Functions and Inline Functions

In today’s lesson, we will be covering the following topics:

1. Anonymous Functions

2. Inline Functions

video coming soon

Anonymous Functions

Anonymous functions aren’t technically “functions” in the sense that they need a special function file, but can be written within a script (check out the lesson on User Defined Functions here). They can accept multiple inputs but return only one output – they only have one line of code, whereas User Defined Functions can have multiple outputs.

To create an anonymous function, use an @ symbol, followed by the variables that the equation will accept. This is what it will look like (highlighted in yellow below). I have created an anonymous function that will accept an input variable x (the @x portion), and will use it in the equation sin(x). I set the anonymous function equal to the variable equation. You must set it equal to a variable because it will come in useful in the next step.

To get the output of this anonymous function, you will need to define the x values that the function will accept. To do this, you need to refer to the name of the function, and in the example above I called the anonymous function by the variable equation. You will type the name of the anonymous function followed by the number you want to use as x (or y, or whatever variable you have). In this case, I want to use x = 5. So I typed equation(5). This “calls” the anonymous function, and substitutes in a value of x = 5. You cannot call this in the Command Window like you can with User Defined Functions.

Now let’s execute this code! When I hit the Run button, this is what comes up in the Command Window. It outputs the answer as -0.9589.

Let’s do an example where we have two inputs to an equation. The equation x + 2*y – 5*x*y uses the variables x and y. This anonymous function is equal to the variable equation, so when I call the function to input the x and y variables, I type equation(1,2). This means x = 1 and y = 2. The order of the numbers entered (eg. (1,2)) works in the order you entered the variables in the anonymous function (eg. @(x,y)):

When I hit the Run button, this is what comes up in the Command Window. The answer is -5 when x = 1 and y = 2 is substituted into the equation x + 2*y – 5*x*y:

Note that you don’t only have to enter one number at a time into the equations. For example, it doesn’t have to be x = some number and y = some number (eg. x = 1 and y = 2). x and y can be equal to a range of numbers – a vector. To do this, you need to make sure any multiplication, division, or exponentation in the equation has a dot (.) before the operators, so that MATLAB will do element-by-element operations (click here if you missed that lesson).

Notice the difference in these two anonymous function examples below versus the anonymous functions I had made in the examples above. The difference now is that I have used dots before the multiplication signs in the equation, making it x+2.*y-5.*x.*y rather than x+2*y-5*x*y. Now you can use vectors rather than just one number at a time to substitute for the variables in the anonymous function. I have used x = [1 2 3] rather than one number, and y = 8 in Example 1. You could use a vector for variable y well, such as [3 5 7] or something else. This is what I’ve done in Example 2.

Example 1

In this example, because both x and y are vectors, MATLAB pairs together each position in the vectors together. This means x = 1 and y = 8 are substituted into the equation first, then x = 2 and y = 6, then x = 3 and y = 8, to give the answers -23, -46, and -101.

Example 2

Inline Functions

Inline functions are similar to anonymous functions – they aren’t technically “functions” – as in, they don’t need a special function file, but can run from within a script.

The syntax is inline(‘equation’,’variable 1′,’variable 2′). equation will be replaced with the equation you want to use, and variable 1, variable 2 will be replaced with the variables that the equation has (called the arguments of the function). The argument can be one variable or more – I have just used two variables as an example.

Example – Suppose I have the equation c = 5*x + 2*z, and want to create an inline function to calculate the value of y when x = 2 and z = 3.

The arguments to this equation (the variables) are x and z, because these are values that need to be defined as numbers in order for the equation to give an output value. Let’s create this equation within an inline function below:

Now to substitute in x = 2 and z = 3, I can do this one of two ways. I will have to call the function using the variable it’s equal to, which is c. This will be c(x,z), where the desired values of x and z will be substituted in. It is in this order because I had defined ‘x’ before ‘z’ in the order of the inline function code.

(1) Type c(2,3) into the Command Window

(2) Type c(2,3) into the script.

Running this will also give the same answer as typing it into the Command Window directly.