0% found this document useful (0 votes)
50 views

Anonymous Functions: Lab Session 11

The document discusses anonymous functions in MATLAB. It explains that anonymous functions are user-defined functions defined within code rather than in separate files. Anonymous functions can be created using the @ symbol followed by a mathematical expression and variables. They capture predefined variable values and must be redefined to use new values. Anonymous functions can be used, passed to other functions, and nested within other functions. The practice problems demonstrate creating an anonymous function to calculate coating weight and a function to calculate triangle circumference using a sub-function.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Anonymous Functions: Lab Session 11

The document discusses anonymous functions in MATLAB. It explains that anonymous functions are user-defined functions defined within code rather than in separate files. Anonymous functions can be created using the @ symbol followed by a mathematical expression and variables. They capture predefined variable values and must be redefined to use new values. Anonymous functions can be used, passed to other functions, and nested within other functions. The practice problems demonstrate creating an anonymous function to calculate coating weight and a function to calculate triangle circumference using a sub-function.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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:

• Note the end statements at the ends of functions B and A.


• The nested function B can access the workspace of the primary function A, and the primary
function A can access the workspace of the function B. This means that a variable defined
in the primary function A can be read and redefined in nested function B and vice versa.
• Function A can call function B, and function B can call function A. Two (or more) Nested
Functions at the Same Level

The format of a user-defined function A (called the primary function) that contains two
nested functions B and Cat the same level is :

• The three functions can access the workspace of each other.


• The three functions can call each other.

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.

The following rules apply to nested functions:


• A nested function can be called from a level above it. (In the preceding example, function
A can call B or D, but not C or E.)
• A nested function can be called from a nested function at the same level within the primary
function. (In the preceding example, function B can call D, and D can call B.)  A nested
function can be called from a nested function at any lower level.
• A variable defined in the primary function is recognized and can be redefined by a function
that is nested at any level within the primary function.
• A variable defined in a nested function is recognized and can be redefined by any of the
functions that contain the nested function.

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.

2. Write a user-defined MATLAB function that determines the circumference of a triangle


when the coordinates of the vertices are given. For the function name and arguments, use
[cr] = cirtriangle (A, B, C). The input arguments A, B, C are vectors with the coordinates
of the vertices, and the output variable cr is the circumference. The function should work
for a triangle in the x-y plane (each vertex is defined by two coordinates) or for a triangle
in space (each vertex is defined by three coordinates). Write the code of cirtriangle such
that it has a sub-function or an anonymous function for calculating the distance between
two points. Use the function to determine the circumference of triangles with the following
vertices:
5
a. A(1,2), B(10,3), C(6,11)

b. A(-1.5, -4.2, -3), B(-5.1 ,6.3 ,2), C(12.1, 0, -0.5)

You might also like