Anonymous Functions
MATLAB's anonymous functions provide an easy way to specify a function. This is essential for problems that include solving a nonlinear equation, integrating or differentiating a function, minimizing a function or a solving differential equation.
The basic syntax is function_name = @(variable_name) matlab_expression;
The name of the function is important - it must be as specified.
Your function can use MATLAB's built-in functions, for example sin(x).
The function is called like any other function.
Function arguments
A function may have one or more input arguments, separated by commas.
When calling a function with more than one input argument, it is the order of the arguments that determines which input value corresponds to which argument. The first first input value corresponds to the first argument, the second input value to the second argument, etc.
The two input arguments are specified after the @ symbol.
The first first value 1 corresponds to the first argument x, while the second value 2 corresponds to the second argument k.
Variables as function arguments
An input argument may be a variable. In this case, the function does not care about the name of the variable, only its position in the list of input arguments.
Assign the given values to x and k.
Produces the correct answer sin(3π(1/6))/(1/6) = 6 since x is first and k is second
Produces the wrong answer sin((1/6)π3)/3 = 1/3 since k is first and x is second
Functions applied to a vector
Your function is much more versatile if it will work not only with scalar inputs but also vector inputs. This can be simply achieved by using element by element operations like .* for multiplication, ./ for division and .^ for powers. In this case the function produces a vector of the same size as the input vector.
If the input x is a vector, x.^2 calculates the square of each element. The MATLAB function exp calculates the exponential of each element when the input is a vector. Multiplying a vector by a scalar is done element by element.
Several output arguments
More complicated functions may produce more than one result, which is most conveniently stored in different variables. This is achieved with the syntax
[out1, out2, out3, ...] = function_name(in1, in2, in3, ...).
Anonymous functions generally only have one output argument, which may be a vector.
Functions as input arguments
Some functions require a function as input. The quantity created using the @ operator is actually called a function handle, and this many be passed to a function as an argument to specify the function.
Define the anonymous function func. The name of the input argument is not important, as long as your are consistent within the definition of the anonymous function.
Call the function fzero: the first argument is the name of the function whose zero is to be found and the second argument is the initial guess.
Remember how to get more information about a MATLAB function.
Use help to display a summary in the command window.
Alternatively use the help browser or you could use the help menu.
More complicated functions
If the function is to be used in several different problems or the function requires more than one MATLAB command, then you need to create a function m-file. This is a plain text file, where the name of the file is the same as the name of the function, and the first line contains the MATLAB keyword function. Function m-files are covered in Lesson 6.
Self-test Exercise
Create an anonymous function decay to evaluate f(t) = t e-2 t, which will work both with a scalar value of the time t and a vector of times t.Answer: decay = @(t) t.*exp(-2*t)
Use the mouse to select the text between the word "Answer" and here to see the answer.
Summary
Anonymous functions are created using the @ operator.
Anonymous functions are particularly powerful if they can accept a vector as input, evaluating the function at each element of the input vector.