MATLAB Lesson 4 - Functions

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;

Create an anonymous function called myfun1 to evaluate f(x) = sin(x)/x.

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.

>>  myfun1 = @(x) sin(x)/x
>>  myfun1(2)

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.

Create an anonymous function called myfun2 to evaluate f(x, k) = sin(k π x)/x and find f(1,2).

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.

>>  myfun2 = @(x, k) sin(k*pi*x)/x
>>  myfun2(1, 2)

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.

Call myfun2 with x = 1/6 and k = 3.

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

>>  x = 1/6, k = 3
>>  myfun2(x, k)
>>  myfun2(k, x)

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.

Write a function npdf to evaluate the standard normal probability density function p(x) = (1/sqrt(2 π)) e-x2/2.

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.

>>  npdf = @(x) (1/sqrt(2*pi))*exp(-0.5*x.^2)
>>  y = npdf([-1 0 1])

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.

Write an anonymous function func to evaluate f(x) = sin(4x) - log(x). Then use the MATLAB function fzero to find a zero of f(x) (that is solve f(x) = 0) near x = 2.

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.

>>  func = @(z) sin(4*z) - log(z)

>>  xs = fzero(func, 2)

Remember how to get more information about a MATLAB function.

Find out more information about the MATLAB function fzero.

Use help to display a summary in the command window.

Alternatively use the help browser or you could use the help menu.

>>  help fzero
>>  doc fzero


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.


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.