Mathematics Previous page   Next Page

Providing Parameter Values to 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,

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, as described in Providing Parameter Values 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.

This returns the zero

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,

For more complicated objective functions, it is usually preferable to write the function as a nested function, as described in Providing Parameter Values Using Nested Functions.


Previous page  Providing Parameter Values Using Nested Functions Differential Equations Next page

© 1994-2005 The MathWorks, Inc.