Mercurial > hg > octave-lyh
annotate scripts/optimization/fzero.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | 74d5c1a4ca96 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2008, 2009 VZLU Prague, a.s. |
8305 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 ## | |
19 ## Author: Jaroslav Hajek <highegg@gmail.com> | |
20 | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
21 ## -*- texinfo -*- |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
22 ## @deftypefn{Function File}{[@var{x}, @var{fval}, @var{info}, @var{output}] =} fzero (@var{fun}, @var{x0}, @var{options}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## Find a zero point of a univariate function. @var{fun} should be a function |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## handle or name. @var{x0} specifies a starting point. @var{options} is a |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## structure specifying additional options. Currently, @code{fzero} |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
26 ## recognizes these options: @code{"FunValCheck"}, @code{"OutputFcn"}, |
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
27 ## @code{"TolX"}, @code{"MaxIter"}, @code{"MaxFunEvals"}. |
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
28 ## For description of these options, see @ref{doc-optimset,,optimset}. |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
29 ## |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
30 ## On exit, the function returns @var{x}, the approximate zero point |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
31 ## and @var{fval}, the function value thereof. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
32 ## @var{info} is an exit flag that can have these values: |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
33 ## @itemize |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
34 ## @item 1 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
35 ## The algorithm converged to a solution. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
36 ## @item 0 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
37 ## Maximum number of iterations or function evaluations has been exhausted. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
38 ## @item -1 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
39 ## The algorithm has been terminated from user output function. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
40 ## @item -2 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
41 ## A general unexpected error. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
42 ## @item -3 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
43 ## A non-real value encountered. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
44 ## @item -4 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
45 ## A NaN value encountered. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
46 ## @end itemize |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
47 ## @seealso{optimset, fsolve} |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
48 ## @end deftypefn |
8305 | 49 |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
50 ## This is essentially the ACM algorithm 748: Enclosing Zeros of |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
51 ## Continuous Functions due to Alefeld, Potra and Shi, ACM Transactions |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
52 ## on Mathematical Software, Vol. 21, No. 3, September 1995. Although |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
53 ## the workflow should be the same, the structure of the algorithm has |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
54 ## been transformed non-trivially; instead of the authors' approach of |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
55 ## sequentially calling building blocks subprograms we implement here a |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
56 ## FSM version using one interior point determination and one bracketing |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
57 ## per iteration, thus reducing the number of temporary variables and |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
58 ## simplifying the algorithm structure. Further, this approach reduces |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
59 ## the need for external functions and error handling. The algorithm has |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
60 ## also been slightly modified. |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
61 |
8648
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8596
diff
changeset
|
62 ## PKG_ADD: __all_opts__ ("fzero"); |
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8596
diff
changeset
|
63 |
8305 | 64 function [x, fval, info, output] = fzero (fun, x0, options = struct ()) |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
65 |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
66 ## Get default options if requested. |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
67 if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults')) |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
68 x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 0, \ |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
69 "OutputFcn", [], "FunValCheck", "off"); |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
70 return; |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
71 endif |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
72 |
8305 | 73 if (nargin < 2 || nargin > 3) |
74 print_usage (); | |
75 endif | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
76 |
8305 | 77 if (ischar (fun)) |
78 fun = str2func (fun); | |
79 endif | |
80 | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
81 ## TODO |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
82 ## displev = optimget (options, "Display", "notify"); |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
83 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
8305 | 84 outfcn = optimget (options, "OutputFcn"); |
85 tolx = optimget (options, "TolX", 0); | |
86 maxiter = optimget (options, "MaxIter", Inf); | |
87 maxfev = optimget (options, "MaxFunEvals", Inf); | |
88 | |
89 persistent mu = 0.5; | |
90 | |
91 if (funvalchk) | |
8507 | 92 ## Replace fun with a guarded version. |
8305 | 93 fun = @(x) guarded_eval (fun, x); |
94 endif | |
95 | |
8507 | 96 ## The default exit flag if exceeded number of iterations. |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
97 info = 0; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
98 niter = 0; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
99 nfev = 0; |
8305 | 100 |
101 x = fval = a = fa = b = fb = NaN; | |
102 | |
8507 | 103 ## Prepare... |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
104 a = x0(1); |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
105 fa = fun (a); |
8305 | 106 nfev = 1; |
107 if (length (x0) > 1) | |
108 b = x0(2); | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
109 fb = fun (b); |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
110 nfev += 1; |
8305 | 111 else |
8507 | 112 ## Try to get b. |
8305 | 113 if (a == 0) |
114 aa = 1; | |
115 else | |
116 aa = a; | |
117 endif | |
118 for b = [0.9*aa, 1.1*aa, aa-1, aa+1, 0.5*aa 1.5*aa, -aa, 2*aa, -10*aa, 10*aa] | |
119 fb = fun (b); nfev += 1; | |
120 if (sign (fa) * sign (fb) <= 0) | |
121 break; | |
122 endif | |
123 endfor | |
124 endif | |
125 | |
126 if (b < a) | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
127 u = a; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
128 a = b; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
129 b = u; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
130 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
131 fu = fa; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
132 fa = fb; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
133 fb = fu; |
8305 | 134 endif |
135 | |
136 if (! (sign (fa) * sign (fb) <= 0)) | |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
137 error ("fzero:bracket", "fzero: not a valid initial bracketing"); |
8305 | 138 endif |
139 | |
140 itype = 1; | |
141 | |
142 if (abs (fa) < abs (fb)) | |
143 u = a; fu = fa; | |
144 else | |
145 u = b; fu = fb; | |
146 endif | |
147 | |
148 d = e = u; | |
149 fd = fe = fu; | |
150 mba = mu*(b - a); | |
151 while (niter < maxiter && nfev < maxfev) | |
152 switch (itype) | |
153 case 1 | |
8507 | 154 ## The initial test. |
8305 | 155 if (b - a <= 2*(2 * abs (u) * eps + tolx)) |
156 x = u; fval = fu; | |
157 info = 1; | |
158 break; | |
159 endif | |
160 if (abs (fa) <= 1e3*abs (fb) && abs (fb) <= 1e3*abs (fa)) | |
8507 | 161 ## Secant step. |
8305 | 162 c = u - (a - b) / (fa - fb) * fu; |
163 else | |
8507 | 164 ## Bisection step. |
8305 | 165 c = 0.5*(a + b); |
166 endif | |
167 d = u; fd = fu; | |
168 itype = 5; | |
169 case {2, 3} | |
170 l = length (unique ([fa, fb, fd, fe])); | |
171 if (l == 4) | |
8507 | 172 ## Inverse cubic interpolation. |
8305 | 173 q11 = (d - e) * fd / (fe - fd); |
174 q21 = (b - d) * fb / (fd - fb); | |
175 q31 = (a - b) * fa / (fb - fa); | |
176 d21 = (b - d) * fd / (fd - fb); | |
177 d31 = (a - b) * fb / (fb - fa); | |
178 q22 = (d21 - q11) * fb / (fe - fb); | |
179 q32 = (d31 - q21) * fa / (fd - fa); | |
180 d32 = (d31 - q21) * fd / (fd - fa); | |
181 q33 = (d32 - q22) * fa / (fe - fa); | |
182 c = a + q31 + q32 + q33; | |
183 endif | |
184 if (l < 4 || sign (c - a) * sign (c - b) > 0) | |
8507 | 185 ## Quadratic interpolation + newton. |
8305 | 186 a0 = fa; |
187 a1 = (fb - fa)/(b - a); | |
188 a2 = ((fd - fb)/(d - b) - a1) / (d - a); | |
8507 | 189 ## Modification 1: this is simpler and does not seem to be worse. |
8305 | 190 c = a - a0/a1; |
191 if (a2 != 0) | |
192 c = a - a0/a1; | |
193 for i = 1:itype | |
194 pc = a0 + (a1 + a2*(c - b))*(c - a); | |
195 pdc = a1 + a2*(2*c - a - b); | |
196 if (pdc == 0) | |
197 c = a - a0/a1; | |
198 break; | |
199 endif | |
200 c -= pc/pdc; | |
201 endfor | |
202 endif | |
203 endif | |
204 itype += 1; | |
205 case 4 | |
8507 | 206 ## Double secant step. |
8305 | 207 c = u - 2*(b - a)/(fb - fa)*fu; |
8507 | 208 ## Bisect if too far. |
8305 | 209 if (abs (c - u) > 0.5*(b - a)) |
210 c = 0.5 * (b + a); | |
211 endif | |
212 itype = 5; | |
213 case 5 | |
8507 | 214 ## Bisection step. |
8305 | 215 c = 0.5 * (b + a); |
216 itype = 2; | |
217 endswitch | |
218 | |
8507 | 219 ## Don't let c come too close to a or b. |
8305 | 220 delta = 2*0.7*(2 * abs (u) * eps + tolx); |
221 if ((b - a) <= 2*delta) | |
222 c = (a + b)/2; | |
223 else | |
224 c = max (a + delta, min (b - delta, c)); | |
225 endif | |
226 | |
8507 | 227 ## Calculate new point. |
8305 | 228 x = c; |
229 fval = fc = fun (c); | |
230 niter ++; nfev ++; | |
231 | |
8507 | 232 ## Modification 2: skip inverse cubic interpolation if |
233 ## nonmonotonicity is detected. | |
8305 | 234 if (sign (fc - fa) * sign (fc - fb) >= 0) |
8507 | 235 ## The new point broke monotonicity. |
236 ## Disable inverse cubic. | |
8305 | 237 fe = fc; |
238 else | |
239 e = d; fe = fd; | |
240 endif | |
241 | |
8507 | 242 ## Bracketing. |
8305 | 243 if (sign (fa) * sign (fc) < 0) |
244 d = b; fd = fb; | |
245 b = c; fb = fc; | |
246 elseif (sign (fb) * sign (fc) < 0) | |
247 d = a; fd = fa; | |
248 a = c; fa = fc; | |
249 elseif (fc == 0) | |
250 a = b = c; fa = fb = fc; | |
251 info = 1; | |
252 break; | |
253 else | |
8507 | 254 ## This should never happen. |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
255 error ("fzero:bracket", "fzero: zero point is not bracketed"); |
8305 | 256 endif |
257 | |
8507 | 258 ## If there's an output function, use it now. |
8305 | 259 if (outfcn) |
260 optv.funccount = niter + 2; | |
261 optv.fval = fval; | |
262 optv.iteration = niter; | |
263 if (outfcn (x, optv, "iter")) | |
264 info = -1; | |
265 break; | |
266 endif | |
267 endif | |
268 | |
269 if (abs (fa) < abs (fb)) | |
270 u = a; fu = fa; | |
271 else | |
272 u = b; fu = fb; | |
273 endif | |
274 if (b - a <= 2*(2 * abs (u) * eps + tolx)) | |
275 info = 1; | |
276 break; | |
277 endif | |
278 | |
8507 | 279 ## Skip bisection step if successful reduction. |
8305 | 280 if (itype == 5 && (b - a) <= mba) |
281 itype = 2; | |
282 endif | |
283 if (itype == 2) | |
284 mba = mu * (b - a); | |
285 endif | |
286 endwhile | |
287 | |
288 output.iterations = niter; | |
289 output.funcCount = niter + 2; | |
290 output.bracket = [a, b]; | |
291 output.bracketf = [fa, fb]; | |
292 | |
293 endfunction | |
294 | |
8507 | 295 ## An assistant function that evaluates a function handle and checks for |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
296 ## bad results. |
8305 | 297 function fx = guarded_eval (fun, x) |
298 fx = fun (x); | |
299 fx = fx(1); | |
300 if (! isreal (fx)) | |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
301 error ("fzero:notreal", "fzero: non-real value encountered"); |
8305 | 302 elseif (isnan (fx)) |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
303 error ("fzero:isnan", "fzero: NaN value encountered"); |
8305 | 304 endif |
305 endfunction | |
306 | |
307 %!assert(fzero(@cos, [0, 3]), pi/2, 10*eps) | |
308 %!assert(fzero(@(x) x^(1/3) - 1e-8, [0,1]), 1e-24, 1e-22*eps) |