Open In App

Anonymous Functions in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

A block of code that is organized in such a way that is reusable for the entire program. Functions are used for reducing efforts made by writing code and making the program short, and easily understandable. 

There are different syntaxes for declaring a function in different programming languages. In this article, we will specifically revolve around the syntax of declaring a function in MATLAB.

Function Declaration in MATLAB:

Matlab possesses different types of functions. Some functions are defined in a few lines, whereas some take the entire program named over the function for its declaration. 

Syntax:

function [i1,i2,...,in] = func_dec(o1,o2,...,om)

Now we see the MATLAB program. In this program the name of the function is funx_dec and hence you have to save the file with the same name, i.e., func_dec.m. Here, the input parameters are i1, i2, i3,…, in, and the output parameters are o1, o2, o3, …, om. Let’s understand this syntax via practical implementation.  

For this, we create a function to find the maximum among three numbers.

Example 1: 

Matlab

% MATLAB program for function
function [ans] = greatest_no(x,y,z)
    if x > y
        if x>z
            ans = x;
        else
            ans = z;
        end
    else
        if y>z
            ans= y; 
        else 
            ans=z;
        end
    end
end

                    

Output:

Anonymous Functions:

The anonymous function is one of the most powerful tools of MATLAB, as it could define a function without an M-file. Anonymous functions in MATLAB, unlike normal functions, are associated in a variable, not in files. When you declare a variable for an anonymous function then the data_type of that particular variable is function_handle. 

As it is an anonymous function whose value is to be stored in a variable, and thus there could only be one output, however, there could be many inputs for it. As the function is written in a single line and thus it contains only one executable statement. However, for executing more than one statement you can use a temporary function inside the anonymous function.

Syntax:

func_name = @(inp_argu)math_exp
// The @ operator is used to initialize anonymous
 functions and handles function .

Let’s understand the syntax of anonymous function using an example of finding cube function:

Example 2:

Matlab

% MATLAB code for anonymous function
cub = @(x) x.^3;

                    

Output:

Here, the syntax is the initializing of the anonymous function is done with ‘@’, followed by input parameter x, written in between the parentheses ‘( )’, and then the statement to be executed is written. 

You can also use the variables, that were declared earlier in your anonymous function. The values persist even if you clear the variables, after the declaration of the anonymous function. Let’s understand this by the following parabola equation.

Example 3:

Matlab

% MATLAB code for function
% variable declaration
a = 3
b = 1
c = 43
para = @(x) a*(x*x) + b*x +c
% parabola equation
clear a b c

                    

Output:

We can use the anonymous function both in the command line window, as well as could be saved to load later by calling it while running other programs or through the command line. If a function is going to be used a limited number of times, then anonymous functions are great to go with. It is also used to construct higher functions. 

Now’s let’s understand Matlab anonymous functions different usages.

Anonymous functions of no variable:

We can use the Matlab anonymous function without even passing a single variable. For such anonymous functions leave the parentheses () blank. 

Example 4:

Matlab

% MATLAB code for no variable anonymous function
print = @() disp("This is an anonymous function.");
print();

                    

Output:

ans: This is an anonymous function.

Here, we have not used any variable in the input or even in the function. In this function, we have not even used any variable in the function, however, we could assign any value by evaluating it also.

Anonymous functions of one variable:

Using Matlab function without variable is not convenient at all. We don’t need the same thing to happen again and again, we actually need to perform some calculations over time. For these, we need to put some variables under the parentheses. We had our previous cub example of variable, let’s have another example of finding  x\sqrt{x}    using an anonymous function.

Example 5:

Matlab

% MATLAB code with variable
expression = @(x) x*sqrt(x);
expression(4)

                    

Output:

ans: 8

Anonymous function with more than one variable:

In certain cases, more than one variable needs to be passed and evaluated. For such cases, we can pass more than one variable in the Anonymous function to evaluate. Let’s evaluate the expression {x^3+ y^2 – z} using an anonymous function.

Example 6:

Matlab

% MATLAB Code anonymous function with more than one variable
expression = @(x,y,z) x.*3+y.*2-z;
expression(12,5,19)

                    

Output:

ans = 27

Nested anonymous function:

Sometimes we need to perform more than one line of code evaluation, in such cases, we need to use the nested anonymous function. The nested anonymous function evaluates the inner function first and then the outer one gives the output. Let’s understand such king of function with an example of solving a definite integral \int_{1}^{6} (12x^3 -9x^2+2) dx.

Example 7:

Matlab

% MATLAB Code for Nested anonymous function
expression = @() integral(@(x) (12*x.^3 - 9*x.*2 + 2),1,6)
  
expression =
  
  function_handle with value:
  
    @()integral(@(x)(12*x.^3-9*x.*2+2),1,6)

                    

Output:

ans = 3580

Passing an anonymous function as a function handle to other functions:

For the constructions of higher functions, we can pass an anonymous function as the function handle to other functions. For example, create a function handle with input parameter as func(output of the anonymous function), and x.

Example 8:

Matlab

function y = functionHandle(fun,x)
    fun = fun(123);
    y = fun * x;
end

                    

Output:

>> functionHandle(@(x) sqrt(x), 12) 
   ans = 133.0864


Last Updated : 15 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads