Mathematics Previous page   Next Page

Representing Functions in MATLAB

MATLAB can represent mathematical functions by expressing them as MATLAB functions in M-files or as anonymous functions. For example, consider the function

This function can be used as input to any of the function functions.

MATLAB Functions

You can find the function above in the M-file named humps.m.

To evaluate the function humps at 2.0, use @ to obtain a function handle for humps, and then use the function handle in the same way you would use a function name to call the function:

Anonymous Functions

A second way to represent a mathematical function at the command line is by creating an anonymous function from a string expression. For example, you can create an anonymous function of the humps function. The value returned, fh, is a function handle:

You can then evaluate fh at 2.0 in the same way that you can with a function handle for a MATLAB function:

You can also create anonymous functions of more than one argument. The following function has two input arguments x and y.


Previous page  Function Summary Plotting Mathematical Functions Next page

© 1994-2005 The MathWorks, Inc.