Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Student Reviews


For Business / Universities

Corporate Training

Hire from US

Academic Up-skilling



All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Electrical

Uploaded on

02 Dec 2022

Understanding Anonymous functions in MATLAB

logo

Skill-Lync

Anonymous functions are very much similar to the regular user-defined functions in MATLAB, but with some distinct individual traits, Anonymous functions are not stored as function files or codes, instead it is just a data type which makes use of ‘function handle’ data type. To understand more about anonymous functions, let us dive deep into function handles

Function handles are simple function codes that can be created without using the regular function definition using keywords ‘function’ and ‘end’. They do not require any separate ‘.m’ files or not needed to be declared separately below/above the main code. Function handles are directly declared in the main code itself and also accessible from any point in that particular code, i.e they are localized to a particular conditional. 

A function handle declaration is quite similar to a function declaration. By placing a "@" symbol before your value or expression allocation, it can be identified,

Func_handle=@(input_argument) func_definition

here,

Func_handle is the name of the particular handle

input argument is the one which will be fed into the function

func_definition will have the equation/expression which will use the input to give us the output

Syntax for calling the function handle in Function Matlab

Variable_name=Func_handle(input_argument)

here,

The output from the handle will be stored under ’Variable_name’

Let us try to execute this syntax with an example snippet in MATLAB

func = @(x)  x+x

A = 10;

B = 20.6;

res1 = func(A)

res2 = func(B)

Syntax for calling the function

 

The above image represents the output for the particular snippet we entered in the MATLAB command window and workspace.

Multiple Arguments Anonymous Function:

The same can be done for functions with multiple input arguments and multiple output arguments and other different input data types

 

func= @(a,b)  strcat(a,b)

str1=’MAT’

str2=’LAB’

strout = func(str1,str2)

Multiple Arguments Anonymous Function

 

This particular feature comes in handy when you don’t want to create a function script for simple repetitive operation

In conclusion we are ideally defining a function to a variable so that it saves us the time and space for creating a separate function script.

Anonymous functions can store the values required for particular equations apart from the input arguments. For an instance consider this particular snippet

 

a=4;

b=6;

c=3;

func = @(x)  a*x^2+b*x+c

input=5

Res=func(input)

function handle matlab

This particular method works only if you declare the required variables before the function handle definition

Anonymous Functions Without Input Arguments

You can also execute an anonymous function without any input arguments. Let us execute the particular snippet in MATLAB

func = @()  8*5+5

Res=func()

Anonymous Functions Without Input Arguments

Arrays of Anonymous Functions:

One more trick you can do is that you can create an array of anonymous functions with different definitions. This results in a cell array of different anonymous functions. You can call a function easily by using the cell index in this case.

Consider this following snippet as an example

x=5;

y=10;

func={@(a,b) a+b;

@(a,b) a-b;

@(b) b^3;

@(a) a+a}

Result1= func{1}(x,y)

Result2=func{2}(x,y)

Result3=func{4}(x+y)

 Arrays of Anonymous Functions

Thus anonymous functions can be used at times when the function definition is very simple and to save program space and computational time. Also some specific solvers like 'ga' and other ode solvers require anonymous functions to execute.


Author

author

Anup KumarH S


Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Control System Designer basics

The controlSystemDesigner tool is primarily used for designing single-input, single-output (SISO) controllers with feedback systems.

Electrical

18 May 2023


Filters Used In Electronics Circuits

A filter is a circuit capable of passing certain frequencies while attenuating (to reduce or block) other frequencies. Thus, a filter can extract important frequencies from signals that also contain undesirable or irrelevant frequencies.

Electrical

17 May 2023


What is a lookup table?

A lookup table is an array of data that maps input values to output values, thereby resembling a mathematical function.

Electrical

13 May 2023


Overview of Stateflow and building of models

Control logic is a paramount part of a software program that controls the operations of the program. The control logic answers to commands from the user, and it also acts on its own to execute automated tasks that have been structured into the program.

Electrical

11 May 2023


Ultracapacitors and Fuel Cell Hybridization

Continuing our series of articles on fuel cells and ultracapacitors for EV, this short article gives a quick rundown on the basics of ultracapacitors. You will also learn about fuel cell hybridization and the high-level components of software needed to develop fuel cell systems.

Electrical

10 May 2023



Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Control System Designer basics

The controlSystemDesigner tool is primarily used for designing single-input, single-output (SISO) controllers with feedback systems.

Electrical

18 May 2023


Filters Used In Electronics Circuits

A filter is a circuit capable of passing certain frequencies while attenuating (to reduce or block) other frequencies. Thus, a filter can extract important frequencies from signals that also contain undesirable or irrelevant frequencies.

Electrical

17 May 2023


What is a lookup table?

A lookup table is an array of data that maps input values to output values, thereby resembling a mathematical function.

Electrical

13 May 2023


Overview of Stateflow and building of models

Control logic is a paramount part of a software program that controls the operations of the program. The control logic answers to commands from the user, and it also acts on its own to execute automated tasks that have been structured into the program.

Electrical

11 May 2023


Ultracapacitors and Fuel Cell Hybridization

Continuing our series of articles on fuel cells and ultracapacitors for EV, this short article gives a quick rundown on the basics of ultracapacitors. You will also learn about fuel cell hybridization and the high-level components of software needed to develop fuel cell systems.

Electrical

10 May 2023


Book a Free Demo, now!

Related Courses

https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/introduction-hev-matlab-simulink_1612262875.jpg
Introduction to Hybrid Electric Vehicle using MATLAB and Simulink
4.8
23 Hours of content
Electrical Domain
Know more
https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/vehicle-dynamics-matlab_1636606203.png
4.8
37 Hours of content
Cae Domain
Showing 1 of 4 courses
Try our top engineering courses, projects & workshops today!Book a Live Demo