Open In App

Inline and Anonymous Functions in MATLAB

Last Updated : 31 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Methods are also popularly known as functions. The main aim of the methods is to reuse the code. A method is a block of code that is invoked and executed when it is called by the user. 

Inline Function

Inline functions are the kind of functions which is defined in one line. that’s why this type of function is called the one-linear function. Inline functions are classified into two categories:

  • In-Built Inline function 
  • User Defined Inline function

So let’s take some examples to understand the inline functions. For the inline function we have to create a new script file then write the code inside that file and run that code.

Example 1:

Matlab




% MATLAB program for inline function.
clc;
close all;
Inline_function = @(x,y) (x^2 + y^2);
result = Inline_function(2,4)


Output: 

 

In this example, we will represent the graphical(plot), figure. using the inline function. so here we create a function and take some variables that help to make a plot in MATLAB.

Example 2:

Matlab




% MATLAB program to draw the
% plot using inline function.
clc;
close all;
 
Anonymous_Function = @(d,t,w) (exp(d*t).* sin(w*t));
time = linspace(0,2,201);
frequency = 100;
omega = 2 * pi * frequency;
digit = -1;
P = Anonymous_Function(digit, time, omega);
 
plot(time,P)


Output:

 

Anonymous Function

An Anonymous function is a kind of MATLAB function that is not stored program file location. or an Anonymous function is a single line of executable expression function that can accept multiple inputs and return one output.

An anonymous function can contain a single-line executable statement. these functions are not stored in program files, but it is connected with a data type function_handle (@)

Use of Anonymous Function 

Example 1:

  • Suppose we have an equation  
  •  y = x2 – 1

So we could write this equation as an anonymous function in MATLAB with the following line of code. But remember the Anonymous function don’t need any new script file, directly we can write and execute in the command window. 

Matlab




% MATLAB program for perform
% the Anonymous function.
 y = @(x)  x^2-1;
 % get the x value.
 y(2)


In the above code, we y=@(x), where the @ is identified as a function handle what it does In MATLAB it’s a data type that stores an association to a function.

Output:

As we know that for the Anonymous function we don’t need any program file we can execute this program in Command Window in MATLAB, as you can see in the below image.

 

If you will change just one line in the above code you will get a different output.

Matlab




% MATLAB program for perform
% the Anonymous function.
 y = @(x)  x^2-1;
 % get the x value.
 y(0)


Output:

 

Let’s look into another Example –

Example 2: 

Suppose in this example I want to perform the Anonymous function that takes just a single line of command for execution. So, here we are defining the function name as cube and defining the x variable.

Matlab




% MATLAB program for perform the Anonymous function.
Cube = @(x) x.^3;
Cube(15)


Using the above command line we will find the cube of any number just using the one-line Anonymous function.

Output:

 

And also if we want to find the cube of others’ numbers Simultaneously then we have to give the command as following and then hit enter.

Matlab




% MATLAB program for perform the Anonymous function.
Cube = @(x) x.^3;
x = [3 5 7]
 
x =
 
    3     5     7
 
Cube(x)


Output:

 

Example 3:

Anonymous function with multiple input and output-

Matlab




% MATLAB program for perform the Anonymous function.
Anonymous_Function = @(a,b) (a^2 + b^2 + a*b);
a = 2;
b = 20;
x = Anonymous_Function(a,b)


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads