Categories

# Anonymous Function in Matlab

In Matlab, there are various inbuilt functions (various libraries are available in Matlab ). These inbuilt functions make Matlab easy and more powerful. But if we want to create our own function then there is one option in Matlab which is a function handle. By using a function handler we can create any mathematical function, these functions are called anonymous functions.

Let us consider one example y = – 2, in this there are two variables x and y. x is input and y is output. The value of y depends on the value of x.

So anonymous function representation will be y = @ ( x ) ^ 3 – 2

In the above equation, y is the function handler, x is the input variable and ‘ @ ‘ is a symbol used for anonymous function.

After writing this equation we can give any value of x . for example y ( 0 ) , y ( 1 ) , y ( 2 ) , y ( 3 ) , etc

If value of x is 0 then y = x ^ 3 – 2

Y = 0 – 2

Y = – 2

Similarly, if the value of x is 1 then y = 1 ^ 3 – 2

Y = 1 – 2

Y = -1

## Steps to Write Anonymous Function in Matlab

Here we have discussed the steps to write a function in Matlab.

Step 1: First define Matlab handle function by using ‘ @ ‘ symbol and input variable , y = @ ( x )

Step 2: Write the whole equation next to the function handler variable.

Step 3: Accept the input value inside the output variable, y ( 0).

Syntax:

Function definition;

Body of function;

Let us consider one equation p = q ^ 2 – 3

## Examples of Anonymous Functions in Matlab

Below are the different examples of anonymous function in matlab as follows:

## Example 1:

``Y = x3 + 2x``

In this example, we will see a simple method of anonymous function.

Here ,Y is output, x is variable input ,

If we put x = 0

Y = 0 + 2 ( 0 )

Y = 0

If we put x = 1

Y = x3 + 2x

Y = 1 + 2

Y = 3

If we put x = 2

Y = x3 + 2x

Y = 8 + 4

Y = 12

If we put x = 3

Y = x3 + 2x

Y = 27 + 6

Y = 33

## Matlab Code:

clc ;

y = @ ( x ) x. ^ 3 + (2 * x) ;

y ( 0 )

y ( 1 )

y ( 2 )

y ( 3 )

In the above code, y takes values of x at compile time. Screen 1 shows the implementation of example 1 in Matlab

y = x– 2*x + 3

if x = 1

y = 1 – 2 + 3

y = 2

if x = 2

y = 8 – 4 + 3

y = 7

if x = 3

y = x– 2*x + 3

y = 27 – 6 + 3

y = 24

## Matlab Code:

X = ( 1 : 10 )

y = @ ( x ) x . ^ 3 – 2 * x + 3

p = y ( x )

plot (y, x )

In above code, x ranges from 0 to 10, so there is no need to assign values of ‘ x ‘ at the time of compile and if discrete values of x and y are known then we can plot the response of variable x and y. Screen 2 shows the implementation of Example 2 and the response of x and y.

## Example 3:

``Y = x2 – log(x)``

In this example, it is difficult to find a logarithm for each value of x, Instead of mathematical calculations if we use direct Matlab commands we will get output with all values of y as well as the response of x vs y (by using plot command).

## Matlab Code:

clc ;

y = @ ( x ) x.^2 – log(x)

y = @(x)x.^2-log(x)

x (1: 10 )

p = y ( x )

In the above code, x ranges from 0 to 10, so there is no need to assign values of ‘ x ‘ at the time of compile and if discrete values of x and y are known then we can plot the response of variable x and y. Screen 2 shows the implementation of Example 2 and the response of x and y.

## Advantages of Anonymous Functions in Matlab

• In an anonymous function, we can create any function which is not predefined.
• It can be stored in a variable.
• Anonymous functions can be returned in function.
• It can be pass inside the function.
• These functions can not be stored in program files, therefore, we can save memory.
• We can store an anonymous function handle so that we can use it again and again whenever required.
• It is easy to represent and implement.