MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of MATLAB Mathematics - SERC - Index of

serc.iisc.ernet.in
from serc.iisc.ernet.in More from this publisher
15.11.2014 Views

4 Function Functions Parameterizing Functions Called by Function Functions At times, you might want use a function function that calls a function with several parameters. For example, if you want to use fzero to find zeros of the cubic polynomial x 3 + bx + c for different values of the coefficients b and c, you would like the function that computes the polynomial to accept the additional parameters b and c. When you invoke fzero, you must also provide values for these additional parameters to the polynomial function. This section describes two ways to do this: • “Providing Parameter Values Using Nested Functions” on page 4-30 • “Providing Parameter Values to Anonymous Functions” on page 4-31 Providing Parameter Values Using Nested Functions One way to provide parameters to the polynomial is to write a single M-file that • Accepts the additional parameters as inputs • Invokes the function function • Contains the function called by the function function as a nested function The following example illustrates how to find a zero of the cubic polynomial x 3 + 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: • Invokes the function fzero to find a zero of the polynomial • Computes the polynomial in a nested function, poly, which is called by fzero 4-30

Parameterizing Functions Called by Function Functions 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 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, 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, as described in “Providing Parameter Values Using Nested Functions” on page 4-30, 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 4-31

Parameterizing Functions Called by Function Functions<br />

You can call findzero with any values <strong>of</strong> the coefficients b and c, which are<br />

seen by poly because it is a nested function.<br />

As an example, to find a zero <strong>of</strong> the polynomial with b = 2 and c = 3.5, using<br />

the starting point x0 = 0, call findzero as follows.<br />

x = findzero(2, 3.5, 0)<br />

This returns the zero<br />

x =<br />

-1.0945<br />

Providing Parameter Values to Anonymous<br />

Functions<br />

Suppose you have already written a standalone M-file for the function poly<br />

containing the following code, which computes the polynomial for any<br />

coefficients b and c,<br />

function y = poly(x, b, c) % Compute the polynomial.<br />

y = x^3 + b*x + c;<br />

You then want to find a zero for the coefficient values b = 2 and c = 3.5. You<br />

cannot simply apply fzero to poly, which has three input arguments, because<br />

fzero only accepts functions with a single input argument. As an alternative<br />

to rewriting poly as a nested function, as described in “Providing Parameter<br />

Values Using Nested Functions” on page 4-30, you can pass poly to fzero as a<br />

function handle to an anonymous function that has the form<br />

@(x) poly(x, b, c). The function handle has just one input argument x, so<br />

fzero accepts it.<br />

b = 2;<br />

c = 3.5;<br />

x = fzero(@(x) poly(x, b, c), 0)<br />

This returns the zero<br />

x =<br />

-1.0945<br />

4-31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!