| Mathematics | ![]() |
Providing Parameter Values Using Nested Functions
One way to provide parameters to the polynomial is to write a single 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:
fzero to find a zero of the polynomial
poly, which is called by fzero
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.
| Parameterizing Functions Called by Function Functions | Providing Parameter Values to Anonymous Functions | ![]() |
© 1994-2005 The MathWorks, Inc.