MATLAB Mathematics - SERC - Index of
MATLAB Mathematics - SERC - Index of MATLAB Mathematics - SERC - Index of
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
- Page 82 and 83: 2 Polynomials and Interpolation 7 3
- Page 84 and 85: 2 Polynomials and Interpolation T =
- Page 86 and 87: 2 Polynomials and Interpolation Bec
- Page 88 and 89: 2 Polynomials and Interpolation Int
- Page 90 and 91: 2 Polynomials and Interpolation 2-3
- Page 92 and 93: 2 Polynomials and Interpolation 2-3
- Page 94 and 95: 3 Fast Fourier Transform (FFT) Intr
- Page 96 and 97: 3 Fast Fourier Transform (FFT) 200
- Page 98 and 99: 3 Fast Fourier Transform (FFT) 2 x
- Page 100 and 101: 3 Fast Fourier Transform (FFT) 500
- Page 102 and 103: 3 Fast Fourier Transform (FFT) Func
- Page 104 and 105: 4 Function Functions Function Summa
- Page 106 and 107: 4 Function Functions You can also c
- Page 108 and 109: 4 Function Functions 25 20 15 10 5
- Page 110 and 111: 4 Function Functions Minimizing Fun
- Page 112 and 113: 4 Function Functions To try fminsea
- Page 114 and 115: 4 Function Functions Plotting the R
- Page 116 and 117: 4 Function Functions The number of
- Page 118 and 119: 4 Function Functions and displays t
- Page 120 and 121: 4 Function Functions end function s
- Page 122 and 123: 4 Function Functions States of the
- Page 124 and 125: 4 Function Functions 100 80 60 40 2
- Page 126 and 127: 4 Function Functions You can verify
- Page 128 and 129: 4 Function Functions Troubleshootin
- Page 130 and 131: 4 Function Functions A three-dimens
- Page 134 and 135: 4 Function Functions “Anonymous F
- Page 136 and 137: 5 Differential Equations Initial Va
- Page 138 and 139: 5 Differential Equations odephas3 o
- Page 140 and 141: 5 Differential Equations This secti
- Page 142 and 143: 5 Differential Equations The basic
- Page 144 and 145: 5 Differential Equations 2 Code the
- Page 146 and 147: 5 Differential Equations function.
- Page 148 and 149: 5 Differential Equations One way to
- Page 150 and 151: 5 Differential Equations y0, yp0 Ve
- Page 152 and 153: 5 Differential Equations For exampl
- Page 154 and 155: 5 Differential Equations 1 0.8 0.6
- Page 156 and 157: 5 Differential Equations 2.5 Soluti
- Page 158 and 159: 5 Differential Equations To run thi
- Page 160 and 161: 5 Differential Equations 2 u′ i =
- Page 162 and 163: 5 Differential Equations dydt(i+1,:
- Page 164 and 165: 5 Differential Equations refine = 4
- Page 166 and 167: 5 Differential Equations Example: A
- Page 168 and 169: 5 Differential Equations function [
- Page 170 and 171: 5 Differential Equations Note The R
- Page 172 and 173: 5 Differential Equations 1 Robertso
- Page 174 and 175: 5 Differential Equations The MATLAB
- Page 176 and 177: 5 Differential Equations Summary of
- Page 178 and 179: 5 Differential Equations Problem Si
- Page 180 and 181: 5 Differential Equations Error Tole
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