MATLAB's anonymous functions provide an easy way to specify a function. An anonymous function is a function defined without using a separate function file. It is a MATLAB feature that lets you define a mathematical expression of one or more inputs and either assign that expression to a function. This method is good for relatively simple functions that will not be used that often and that can be written in a single expression.
The inline command lets you create a function of any number of variables by giving a string containing the function followed by a series of strings denoting the order of the input variables. It is similar to an Anonymous Function
2. • MATLAB's anonymous functions provide an easy way to
specify a function.
• An anonymous function is a function defined without
using a separate function file
• It is a MATLAB feature that lets you define a
mathematical expression of one or more inputs and
either assign that expression to a function.
• This method is good for relatively simple functions that
will not be used that often and that can be written in a
single expression.
http://electricalenggtutorial.blogspot.com 2
3. • An anonymous function of any number of variables can
be created by giving the @ symbol, followed by a
variable list, followed by the MATLAB expression.
• Anonymous function can be written in Command
Window, script file, or inside user-defined function.
• It is similar to an Inline Function with some significant
differences.
• Anonymous functions can only have one expression and
can only return a single variable
http://electricalenggtutorial.blogspot.com 3
4. • To give the anonymous function a name, simply put the
function's name on the left side of an equal sign and the
anonymous function on the right.
• NAME = @(ARGLIST)EXPRESSION
• NAME is name of the function. (using rules for names of user-
defined functions)
• @ - a function handle, an object that has information
about the function
• ARGLIST is the input arguments (a comma-separated
list).
• The body of the function, to the right of the parentheses,
is a single line MATLAB expression.
• Execute the function by calling it by means of the given
name same way as user-defined functions.
http://electricalenggtutorial.blogspot.com 4
5. >> z = @(x, y) x.^3-y.^3
z =
@(x, y) x.^3-y.^3
>> z( 3, 2)
ans = 19
>> triarea = @(a, h) 1/2*a*h
triarea =
@(a, h) 1/2*a*h
>> triarea(2,2)
ans = 2
>> x = 5;
>> y = 4;
>> area = triarea(x, y)
area = 10
http://electricalenggtutorial.blogspot.com 5
6. • An anonymous function expression can use a variable
that have been defined before the function is defined.
• Value of the variable when the function is defined will be
used for evaluating the expression.
• The changes made after the expression is defined will
not be reflected in evaluating the function.
• >> f = 5o;
• >> sinewave = @(t) sin( 2*pi*f.*t );
• >> x=[0:0.001:0.2];
• >> y=sinewave(x);
• >> plot(x,y)
http://electricalenggtutorial.blogspot.com 6
7. • Anonymous function will be unaffected by any variable change
after the function is defined
>> a = 2;
>> b = 3;
>> sample = @(x,y) a*x.^2 + b*y^2;
>> c = sample( 2, 1 )
c = 11
>> a = 5
a = 5
>> b = 0
b = 0
>> c = sample( 2, 1 )
c = 11
http://electricalenggtutorial.blogspot.com 7
8. • The inline command lets you create a function of any
number of variables by giving a string containing the
function followed by a series of strings denoting the order
of the input variables.
• This method is good for relatively simple functions that
will not be used that often and that can be written in a
single expression.
• It is similar to an Anonymous Function
http://electricalenggtutorial.blogspot.com 8
9. • Name = inline(‘expression‘)creates an inline
function object from the expression.
• The input arguments are automatically determined by searching the
expression for variable names.
• If no variable exists, 'x' is used.
• The expression to be evaluated is defined in single
quotes, followed in order by the variables of the function
also surrounded by single quotes.
• Name = inline(‘expression', 'arg1', ‘arg2')
http://electricalenggtutorial.blogspot.com 9