Function Handles

Introduction

MATLAB® functions are written to named M-files or are produced at the command line as anonymous functions. In either case, a function handle is used to pass the function as an input argument to a function function.

M-File Functions

Consider the example function:

An M-file for the function is humps.m. Its contents look like this:

function y = humps(x)
y = 1./((x - 0.3).^2 + 0.01) ...
  + 1./((x - 0.9).^2 + 0.04) - 6;

To evaluate humps at 2.0, first use @ to obtain a function handle. Use the function handle in the same way you would use a function name to call a function:

fh = @humps;
fh(2.0)
ans =
   -4.8552

Anonymous Functions

Consider again the example function:

A second way to represent the function in the MATLAB is to create an anonymous function at the command line, as follows:

fh = @(x)(1./((x-0.3).^2 + 0.01) ...
          + 1./((x-0.9).^2 + 0.04)-6);

You evaluate fh at 2.0 the same way you do with a function handle for an M-File function:

fh(2.0)
ans =
   -4.8552

Anonymous functions can have any number of arguments. The following anonymous function has two input arguments x and y:

fh = @(x,y)(y*sin(x)+x*cos(y));
fh(pi,2*pi)
ans =
    3.1416

Example: Function Plotting Function

The fplot function plots a mathematical function between a given set of axes limits. You can control the x-axis limits only, or both the x- and y-axis limits. For example, to plot the humps.m function over the x-axis range [-5 5], use

fplot(@h,[-5 5])
grid on

You can zoom in on the function by selecting y-axis limits of -10 and 25, using

fplot(@humps,[-5 5 -10 25])
grid on

zooming in on the function by selecting y-axis limits of -10 and 25

You can also pass the function handle for an anonymous function for fplot to graph, as in

fplot(@(x)2*sin(x+3),[-1 1]);

You can plot more than one function on the same graph with one call to fplot. If you use this with a function, then the function must take a column vector x and return a matrix where each column corresponds to each function, evaluated at each value of x.

If you pass an anonymous function consisting of several functions to fplot, the anonymous function also must return a matrix where each column corresponds to each function evaluated at each value of x, as in

fplot(@(x)[2*sin(x+3),humps(x)],[-5 5])

which plots the first and second functions on the same graph.

plot of the first and second functions on the same graph

Note that the anonymous function

fh = @(x)[2*sin(x+3),humps(x)];

evaluates to a matrix of two columns, one for each function, when x is a column vector.

fh([1;2;3])

returns

   -1.5136   16.0000
   -1.9178   -4.8552
   -0.5588   -5.6383

Parametrizing Functions

Using Nested Functions

One way to provide additional parameters to a functional argument of a function function is to write a M-file that

The following example illustrates how to find a zero of the cubic polynomial x3 + bx + c, for different values of the coefficients b and c, using this method. To do so, write an M-file with the following code.

function y = findzero(b, c, x0)

options = optimset('Display', 'off'); % Turn off Display
y = fzero(@poly, x0, options);

	function y = poly(x) % Compute the polynomial.
	y = x^3 + b*x + c;
	end
end

The main function, findzero, does two things:

You can call findzero with any values of the coefficients b and c, which are seen by poly because it is a nested function.

As an example, to find a zero of the polynomial with b = 2 and c = 3.5, using the starting point x0 = 0, call findzero as follows.

x = findzero(2, 3.5, 0)

This returns the zero

x =

   -1.0945

Using Anonymous Functions

Suppose you have already written a standalone M-file for the function poly containing the following code, which computes the polynomial for any coefficients b and c,

function y = poly(x, b, c) % Compute the polynomial.
y = x^3 + b*x + c;

You then want to find a zero for the coefficient values b = 2 and c = 3.5. You cannot simply apply fzero to poly, which has three input arguments, because fzero only accepts functions with a single input argument.

As an alternative to rewriting poly as a nested function (see Using Nested Functions) you can pass poly to fzero as a function handle to an anonymous function that has the form @(x) poly(x, b, c). The function handle has just one input argument x, so fzero accepts it.

b = 2;
c = 3.5;
x = fzero(@(x) poly(x, b, c), 0)

This returns the zero

x =

   -1.0945

Anonymous Functions explains how to create anonymous functions.

If you later decide to find a zero for different values of b and c, you must redefine the anonymous function using the new values. For example,

b = 4;
c = -1;
fzero(@(x) poly(x, b, c), 0)

ans =

    0.2463

For more complicated objective functions, it is usually preferable to write the function as a nested function.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS