Anonymous Functions: Lab Session 11
Anonymous Functions: Lab Session 11
Anonymous Functions
User-defined functions written in function files can be used for simple mathematical
functions, for large and complicated math functions that require extensive programming, and as
subprograms in large computer programs. In cases when the value of a relatively simple
mathematical expression has to be calculated many times within a program, MATLAB provides
the option of using anonymous functions. An anonymous function is a user-defined function that
is defined and written within the computer code (not in a separate function file) and is then used
in the code. Anonymous functions can be defined in any part of MATLAB (in the Command
Window, in script files, and inside regular user-defined functions).
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 constructed in the
Command Window, within a script file, or inside a regular user-defined function. An anonymous
function is created by typing the following command:
A simple example which calculates the cube of the input argument is:
• The command creates the anonymous function and assigns a handle for the function to the
variable name on the left-hand side of the = sign. (Function handles provide means for
using the function and passing it to other functions)
• The expr consists of a single valid mathematical MATLAB expression.
• The mathematical expression can have one or several independent variables. The
independent variable(s) is (are) entered in the (arglist). Multiple independent variables are
separated with commas. An example of an anonymous function that has two independent
variables is: circle = @ (x, y) 16*x^2+9*y^2
• The mathematical expression can include any built-in or user-defined functions.
• The expression must be written according to the dimensions of the arguments (element-
byelement or linear algebra calculations).
1
• The expression can include variables that are already defined when the anonymous function
is defined. For example, if three variables a, b, and c are defined (have assigned numerical
values), then they can be used in the expression of the anonymous function parabola= @
(x) a*x^2+b*x+c.
MATLAB captures the values of the predefined variables when the anonymous function is
defined. This means that if new values are subsequently assigned to the predefined variables, the
anonymous function is not changed. The anonymous function has to be redefined in order for the
new values of the predefined variables to be used in the expression.
Using an Anonymous Function
• Once an anonymous function is defined, it can be used by typing its name and a value for
the argument (or arguments) in parentheses.
• Anonymous functions can also be used as arguments in other functions
Sub-Functions
A function file can contain more than one user-defined function. The functions are typed
one after the other. Each function begins with a function definition line. The first function is called
the primary function and the rest of the functions are called sub-functions. The sub-functions can
be typed in any order. The name of the function file that is saved should correspond to the name
of the primary function. Each of the functions in the file can call any of the other functions in the
file. Outside functions, or programs (script files), can call only the primary function. Each of the
functions in the file has its own workspace, which means that in each the variables are local. In
other words, the primary function and the sub-functions cannot access each other's variables
(unless variables are declared to be global).
Sub-functions can help in writing user-defined functions in an organized manner. The
program in the primary function can be divided into smaller tasks, each of which is carried out in
a sub-function.
Nested Functions
A nested function is a user-defined function that is written inside another user-defined
function. The portion of the code that corresponds to the nested function starts with a function
definition line and ends with an end statement. An end statement must also be entered at the end
of the function that contains the nested function. (Normally, a user-defined function does not
2
require a terminating end statement. However, an end statement is required if the function contains
one or more nested functions.) Nested functions can also contain nested functions. Obviously,
having many levels of nested functions can be confusing. This section considers only two levels
of nested functions.
One Nested Function
The format of a user-defined function A (called the primary function) that contains one
nested function B is:
The format of a user-defined function A (called the primary function) that contains two
nested functions B and Cat the same level is :
3
Two Levels of Nested Functions
Two levels of nested functions are created when nested functions are written inside nested
functions. The following shows an example for the format of a user-defined function with four
nested functions in two levels.
Practice Exercise
1. The surface area S of a ring in shape of a torus with an inner radius r and a diameter d is
given by:
𝑆 =𝜋2(2𝑟+𝑑)𝑑
4
The ring is to be plated with a thin layer of coating. The weight of the coating W can be
calculated approximately as W = γSt , where γ is the specific weight of the coating material
and t is its thickness. Write an anonymous function that calculates the weight of the coating.
The function should have four input arguments, r, d, t, and γ. Use the anonymous function
to calculate the weight of a gold coating (γ = 0.696 lb/in3) of a ring with r = 0.35 in, d =
0.12 in, and t = 0.002 in.