Uploaded on
02 Dec 2022
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
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)
The above image represents the output for the particular snippet we entered in the MATLAB command window and workspace.
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)
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)
This particular method works only if you declare the required variables before the function handle definition
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()
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)
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
Anup KumarH S
Author
Skill-Lync
Continue Reading
Related Blogs
The controlSystemDesigner tool is primarily used for designing single-input, single-output (SISO) controllers with feedback systems.
18 May 2023
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.
17 May 2023
A lookup table is an array of data that maps input values to output values, thereby resembling a mathematical function.
13 May 2023
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.
11 May 2023
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.
10 May 2023
Author
Skill-Lync
Continue Reading
Related Blogs
The controlSystemDesigner tool is primarily used for designing single-input, single-output (SISO) controllers with feedback systems.
18 May 2023
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.
17 May 2023
A lookup table is an array of data that maps input values to output values, thereby resembling a mathematical function.
13 May 2023
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.
11 May 2023
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.
10 May 2023
Related Courses