Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Student Reviews



More

Academic Training

Informative Articles

Find Jobs

We are Hiring!


All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Electrical

Modified on

02 Dec 2022 09:00 pm

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

Subscribe to Our Free Newsletter

img

Continue Reading

Related Blogs

Electric PowerTrain for Two Wheelers

The average vehicle body gives any vehicle its structural integrity, while the electric motor is concerned with the generation of torque or force. The electric motor can be a permanent magnet synchronous motor, brushless dc motor etc.

Electrical

25 Aug 2022


How To Tune PID Controllers for Converter Feedback

In industrial control applications, a PID controller is a device that regulates temperature, flow, pressure, speed, and other process variables. PID (proportional integral derivative) controllers, which use a control loop feedback mechanism to control process variables, are the most accurate and trustworthy controllers.

Electrical

27 Aug 2022


Pulse Charging of Lithium Ion Battery

A lithium-ion (Li-ion) battery is a complicated battery technology that uses lithium ions as a key component of its electrochemistry. During a discharge cycle, lithium atoms within the anode are ionized and separated from their electrons.

Electrical

29 Aug 2022


A Guide on the Dashboard Library Blockset

Dash in your car is your source of information. It basically functions as a control panel that sits in front of the driver and shows numerous controls and instrumentation required for your car to function.

Electrical

01 Sep 2022


All About Series Hybrid Vehicles

A hybrid vehicle has two power sources for motion within the same vehicle. Basically the hybrid vehicle is split into two types supported the source for propulsion - combustion engine type and Hydrogen power cell.

Electrical

07 Sep 2022



Author

blogdetails

Skill-Lync

Subscribe to Our Free Newsletter

img

Continue Reading

Related Blogs

Electric PowerTrain for Two Wheelers

The average vehicle body gives any vehicle its structural integrity, while the electric motor is concerned with the generation of torque or force. The electric motor can be a permanent magnet synchronous motor, brushless dc motor etc.

Electrical

25 Aug 2022


How To Tune PID Controllers for Converter Feedback

In industrial control applications, a PID controller is a device that regulates temperature, flow, pressure, speed, and other process variables. PID (proportional integral derivative) controllers, which use a control loop feedback mechanism to control process variables, are the most accurate and trustworthy controllers.

Electrical

27 Aug 2022


Pulse Charging of Lithium Ion Battery

A lithium-ion (Li-ion) battery is a complicated battery technology that uses lithium ions as a key component of its electrochemistry. During a discharge cycle, lithium atoms within the anode are ionized and separated from their electrons.

Electrical

29 Aug 2022


A Guide on the Dashboard Library Blockset

Dash in your car is your source of information. It basically functions as a control panel that sits in front of the driver and shows numerous controls and instrumentation required for your car to function.

Electrical

01 Sep 2022


All About Series Hybrid Vehicles

A hybrid vehicle has two power sources for motion within the same vehicle. Basically the hybrid vehicle is split into two types supported the source for propulsion - combustion engine type and Hydrogen power cell.

Electrical

07 Sep 2022


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