TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

MATLAB Anonymous Function

MATLAB Anonymous Function with MATLAB Tutorial, MATLAB, MATLAB Introduction, MATLAB Installation, MATLAB Platform, MATLAB Syntax, MATLAB Data Types, MATLAB Variables, MATLAB Operators, MATLAB Commands, MATLAB Loops, MATLAB Strings, MATLAB Numbers, MATLAB Vectors, MATLAB Downloading etc.

<< Back to MATLAB

Anonymous Function

An anonymous function is a simple (one-line) user-defined function that is defined without creating a separate function file (M-file). Anonymous functions can be defined in the Command Window, within a script file, or inside a user-defined function.

An anonymous function is generated by typing the following command:

Anonymous Function

Where f is the function handle. The input list can contain a single variable or several variables separated by commas. After creating the function, we can use it with its handle to evaluate the function or pass it as an argument to other functions.

For example, create an anonymous function that evaluates and return the area of a circle:

>> cirarea = @ (radius) pi * radius .ˆ 2;

The function handles variable name is cirarea. There is a one input argument, radius. The body of a function is the expression pi * radius .ˆ 2. The .ˆ array function is used so that the vector of radii can be passed to the functions.

The functions are then called using the handle and passing arguments(s) to it. The function call using a function handle looks just like a function call using a function name.

>> cirarea(4)
ans =
       50.2655
>> cirarea(1:4)
ans =
         3.1416   12.5664   28.2743   50.2655

The type of cirarea can be found using class functions:

>> class(cirarea)
ans =
         function_handle

Unlike functions saved in code files, if no parameter is passed to an anonymous function, the parentheses must be in the function definitions and in the function calls. For example, the following is anonymous function that print a random real number with two decimal places, as well as call to this function:

>> prtran = @ () fprintf('%.2f\n', rand);
 >> prtran()
 0.95

Typing the name of the function handle will show its content, which is the function definitions.

>> prtran
prtran = 
@ () fprintf('%.2f\n',rand)

An anonymous function can be stored to a MAT-file and can then be loaded when needed.

>> cirarea = @ (radius) pi * radius .ˆ 2;
 >> save anonfns cirarea
>> clear
>> load anonfns
>> who
Your variables are:
cirarea 
>> cirarea
cirarea = 
@ (radius) pi * radius .ˆ 2

Custom anonymous functions

Example of an anonymous functions with one independent variable:

The function: Anonymous Function can be defined in the command window as an anonymous function for x as a scalar by:

>> FA= @ (x) exp(x^2)/sqrt(x^2+5)
FA =
            @(x)exp(x^2)/sqrt(x^2+5)

If a semicolon is not typed at the end, MATLAB shows the function. The function can then be used for different values of x:

>> FA(2)
ans =
         18.1994
>> z = FA(3)
z =
        2.1656e+003

If x is expected to be an array, and the function calculated for each element, then the function must be modified for element-by-element calculations.

>> FA = @ (x) exp(x.^2)./sqrt(x.^2+5)
FA =
         @(x)exp(x.^2)./sqrt(x.^2+5)
> > FA ([ 1 0 . 5 2])    
ans =
           1.1097     0.5604    18.1994

Example of an anonymous function with various independent variables:

The function f (x, y) = 2x2-4xy + y2 can be defined as an anonymous function by:

>> HA = @ (x, y) 2*x^2 - 4*x*y + y^2
HA = 
              @ (x, y) 2*x^2 - 4*x*y + y^2

Then, the anonymous function can be used for particular values of x and y. For example, typing HA (2, 3) gives:

>> HA (2,3)
ans =
         -7

Parameterizing anonymous functions

Variables in the workspaces can be used within the definition of anonymous functions. This is called parameterizing. For example, to use a constant c = 2 in an anonymous function:

>> c = 2;
>> f = @(x) c*x
f = 
    @(x)c*x
>> f(3)
ans =
         6

f (3) used the variable c as an argument to multiply with the provided x. Note that if the value of c is set to something different at this point, then f (3) is called, the result would not be different. The value of c is the value at the time of the creation of the anonymous function:

>> c = 2;
>> f = @(x) c*x;
>> f(3)
ans =
     6
>> c = 3;
>> f(3)
ans =
        6   

Next TopicMATLAB Plotting




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf