Mercurial > hg > octave-nkf
annotate scripts/optimization/fzero.m @ 10544:9961fc022d9d
fix assignment to non-existing variables and octave_value::assign
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 23 Apr 2010 11:23:43 +0200 |
parents | 9500a66118dc |
children | a8ce6bdecce5 |
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 -*- |
9143
74d5c1a4ca96
Eliminate 'unbalanced parentheses in @def...' error during texi2pdf.
Rik <rdrider0-list@yahoo.com>
parents:
9051
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 |
10470 | 24 ## handle or name. @var{x0} should be a two-element vector specifying the initial |
25 ## bracketing. It should hold | |
26 ## @example | |
27 ## sign (@var{fun}(@var{x0}(1))) * sign (@var{fun}(@var{x0}(2))) <= 0 | |
28 ## @end example | |
29 ## If only a single scalar is given as @var{x0}, several nearby and distant | |
30 ## values are probed in an attempt to obtain a valid bracketing. If this | |
31 ## is not successful, the function fails. | |
32 ## @var{options} is a structure specifying additional options. | |
33 ## Currently, @code{fzero} | |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
34 ## 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
|
35 ## @code{"TolX"}, @code{"MaxIter"}, @code{"MaxFunEvals"}. |
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
36 ## 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
|
37 ## |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
38 ## 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
|
39 ## 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
|
40 ## @var{info} is an exit flag that can have these values: |
10297
ed88ea036716
improve docs of fzero/fminbnd
Jaroslav Hajek <highegg@gmail.com>
parents:
10296
diff
changeset
|
41 ## |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
42 ## @itemize |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
43 ## @item 1 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
44 ## 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
|
45 ## @item 0 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
46 ## 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
|
47 ## @item -1 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
48 ## The algorithm has been terminated from user output function. |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
49 ## @item -5 |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
50 ## The algorithm may have converged to a singular point. |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
51 ## @end itemize |
8515
ec2715c76039
fzero.m, fsolve.m: additional doc fixes
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
52 ## @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
|
53 ## @end deftypefn |
8305 | 54 |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
55 ## 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
|
56 ## 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
|
57 ## 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
|
58 ## 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
|
59 ## 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
|
60 ## 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
|
61 ## 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
|
62 ## 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
|
63 ## 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
|
64 ## 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
|
65 ## 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
|
66 |
8648
ff61b53eb294
optimization: use PKG_ADD: comments instead of PKG_ADD file
John W. Eaton <jwe@octave.org>
parents:
8596
diff
changeset
|
67 ## 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
|
68 |
8305 | 69 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
|
70 |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
71 ## Get default options if requested. |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
72 if (nargin == 1 && ischar (fun) && strcmp (fun, 'defaults')) |
10296 | 73 x = optimset ("MaxIter", Inf, "MaxFunEvals", Inf, "TolX", 1e-8, \ |
8596
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
74 "OutputFcn", [], "FunValCheck", "off"); |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
75 return; |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
76 endif |
8833c0b18eb2
enable default settings queries in optim funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8515
diff
changeset
|
77 |
8305 | 78 if (nargin < 2 || nargin > 3) |
79 print_usage (); | |
80 endif | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
81 |
8305 | 82 if (ischar (fun)) |
9464
e598248a060d
safer str2func use in optim functions
Jaroslav Hajek <highegg@gmail.com>
parents:
9143
diff
changeset
|
83 fun = str2func (fun, "global"); |
8305 | 84 endif |
85 | |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
86 ## TODO |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
87 ## 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
|
88 funvalchk = strcmpi (optimget (options, "FunValCheck", "off"), "on"); |
8305 | 89 outfcn = optimget (options, "OutputFcn"); |
10296 | 90 tolx = optimget (options, "TolX", 1e-8); |
8305 | 91 maxiter = optimget (options, "MaxIter", Inf); |
92 maxfev = optimget (options, "MaxFunEvals", Inf); | |
93 | |
94 persistent mu = 0.5; | |
95 | |
96 if (funvalchk) | |
8507 | 97 ## Replace fun with a guarded version. |
8305 | 98 fun = @(x) guarded_eval (fun, x); |
99 endif | |
100 | |
8507 | 101 ## 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
|
102 info = 0; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
103 niter = 0; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
104 nfev = 0; |
8305 | 105 |
106 x = fval = a = fa = b = fb = NaN; | |
10296 | 107 eps = eps (class (x0)); |
8305 | 108 |
8507 | 109 ## Prepare... |
8467
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
110 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
|
111 fa = fun (a); |
8305 | 112 nfev = 1; |
113 if (length (x0) > 1) | |
114 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
|
115 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
|
116 nfev += 1; |
8305 | 117 else |
8507 | 118 ## Try to get b. |
8305 | 119 if (a == 0) |
120 aa = 1; | |
121 else | |
122 aa = a; | |
123 endif | |
124 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] | |
125 fb = fun (b); nfev += 1; | |
126 if (sign (fa) * sign (fb) <= 0) | |
127 break; | |
128 endif | |
129 endfor | |
130 endif | |
131 | |
132 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
|
133 u = a; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
134 a = b; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
135 b = u; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
136 |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
137 fu = fa; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
138 fa = fb; |
77b8d4aa2743
fsolve.m, fzero.m: style fixes; use strcmpi to compare options
John W. Eaton <jwe@octave.org>
parents:
8305
diff
changeset
|
139 fb = fu; |
8305 | 140 endif |
141 | |
142 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
|
143 error ("fzero:bracket", "fzero: not a valid initial bracketing"); |
8305 | 144 endif |
145 | |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
146 slope0 = (fb - fa) / (b - a); |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
147 |
10470 | 148 if (fa == 0) |
149 b = a; | |
150 fb = fa; | |
151 elseif (fb == 0) | |
152 a = b; | |
153 fa = fb; | |
154 endif | |
155 | |
8305 | 156 itype = 1; |
157 | |
158 if (abs (fa) < abs (fb)) | |
159 u = a; fu = fa; | |
160 else | |
161 u = b; fu = fb; | |
162 endif | |
163 | |
164 d = e = u; | |
165 fd = fe = fu; | |
166 mba = mu*(b - a); | |
167 while (niter < maxiter && nfev < maxfev) | |
168 switch (itype) | |
169 case 1 | |
8507 | 170 ## The initial test. |
8305 | 171 if (b - a <= 2*(2 * abs (u) * eps + tolx)) |
172 x = u; fval = fu; | |
173 info = 1; | |
174 break; | |
175 endif | |
176 if (abs (fa) <= 1e3*abs (fb) && abs (fb) <= 1e3*abs (fa)) | |
8507 | 177 ## Secant step. |
8305 | 178 c = u - (a - b) / (fa - fb) * fu; |
179 else | |
8507 | 180 ## Bisection step. |
8305 | 181 c = 0.5*(a + b); |
182 endif | |
183 d = u; fd = fu; | |
184 itype = 5; | |
185 case {2, 3} | |
186 l = length (unique ([fa, fb, fd, fe])); | |
187 if (l == 4) | |
8507 | 188 ## Inverse cubic interpolation. |
8305 | 189 q11 = (d - e) * fd / (fe - fd); |
190 q21 = (b - d) * fb / (fd - fb); | |
191 q31 = (a - b) * fa / (fb - fa); | |
192 d21 = (b - d) * fd / (fd - fb); | |
193 d31 = (a - b) * fb / (fb - fa); | |
194 q22 = (d21 - q11) * fb / (fe - fb); | |
195 q32 = (d31 - q21) * fa / (fd - fa); | |
196 d32 = (d31 - q21) * fd / (fd - fa); | |
197 q33 = (d32 - q22) * fa / (fe - fa); | |
198 c = a + q31 + q32 + q33; | |
199 endif | |
200 if (l < 4 || sign (c - a) * sign (c - b) > 0) | |
8507 | 201 ## Quadratic interpolation + newton. |
8305 | 202 a0 = fa; |
203 a1 = (fb - fa)/(b - a); | |
204 a2 = ((fd - fb)/(d - b) - a1) / (d - a); | |
8507 | 205 ## Modification 1: this is simpler and does not seem to be worse. |
8305 | 206 c = a - a0/a1; |
207 if (a2 != 0) | |
208 c = a - a0/a1; | |
209 for i = 1:itype | |
210 pc = a0 + (a1 + a2*(c - b))*(c - a); | |
211 pdc = a1 + a2*(2*c - a - b); | |
212 if (pdc == 0) | |
213 c = a - a0/a1; | |
214 break; | |
215 endif | |
216 c -= pc/pdc; | |
217 endfor | |
218 endif | |
219 endif | |
220 itype += 1; | |
221 case 4 | |
8507 | 222 ## Double secant step. |
8305 | 223 c = u - 2*(b - a)/(fb - fa)*fu; |
8507 | 224 ## Bisect if too far. |
8305 | 225 if (abs (c - u) > 0.5*(b - a)) |
226 c = 0.5 * (b + a); | |
227 endif | |
228 itype = 5; | |
229 case 5 | |
8507 | 230 ## Bisection step. |
8305 | 231 c = 0.5 * (b + a); |
232 itype = 2; | |
233 endswitch | |
234 | |
8507 | 235 ## Don't let c come too close to a or b. |
8305 | 236 delta = 2*0.7*(2 * abs (u) * eps + tolx); |
237 if ((b - a) <= 2*delta) | |
238 c = (a + b)/2; | |
239 else | |
240 c = max (a + delta, min (b - delta, c)); | |
241 endif | |
242 | |
8507 | 243 ## Calculate new point. |
8305 | 244 x = c; |
245 fval = fc = fun (c); | |
246 niter ++; nfev ++; | |
247 | |
8507 | 248 ## Modification 2: skip inverse cubic interpolation if |
249 ## nonmonotonicity is detected. | |
8305 | 250 if (sign (fc - fa) * sign (fc - fb) >= 0) |
8507 | 251 ## The new point broke monotonicity. |
252 ## Disable inverse cubic. | |
8305 | 253 fe = fc; |
254 else | |
255 e = d; fe = fd; | |
256 endif | |
257 | |
8507 | 258 ## Bracketing. |
8305 | 259 if (sign (fa) * sign (fc) < 0) |
260 d = b; fd = fb; | |
261 b = c; fb = fc; | |
262 elseif (sign (fb) * sign (fc) < 0) | |
263 d = a; fd = fa; | |
264 a = c; fa = fc; | |
265 elseif (fc == 0) | |
266 a = b = c; fa = fb = fc; | |
267 info = 1; | |
268 break; | |
269 else | |
8507 | 270 ## 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
|
271 error ("fzero:bracket", "fzero: zero point is not bracketed"); |
8305 | 272 endif |
273 | |
8507 | 274 ## If there's an output function, use it now. |
8305 | 275 if (outfcn) |
10296 | 276 optv.funccount = nfev; |
8305 | 277 optv.fval = fval; |
278 optv.iteration = niter; | |
279 if (outfcn (x, optv, "iter")) | |
280 info = -1; | |
281 break; | |
282 endif | |
283 endif | |
284 | |
285 if (abs (fa) < abs (fb)) | |
286 u = a; fu = fa; | |
287 else | |
288 u = b; fu = fb; | |
289 endif | |
290 if (b - a <= 2*(2 * abs (u) * eps + tolx)) | |
291 info = 1; | |
292 break; | |
293 endif | |
294 | |
8507 | 295 ## Skip bisection step if successful reduction. |
8305 | 296 if (itype == 5 && (b - a) <= mba) |
297 itype = 2; | |
298 endif | |
299 if (itype == 2) | |
300 mba = mu * (b - a); | |
301 endif | |
302 endwhile | |
303 | |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
304 ## Check solution for a singularity by examining slope |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
305 if (info == 1) |
10394
bc475cd49147
avoid div by zero in fzero
Jaroslav Hajek <highegg@gmail.com>
parents:
10337
diff
changeset
|
306 if ((b - a) != 0 && abs ((fb - fa)/(b - a) / slope0) > max (1e6, 0.5/(eps+tolx))) |
10337
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
307 info = -5; |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
308 endif |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
309 endif |
7c97da90fc8f
check for discontinuities in fzero
Rik <rdrider0-list@yahoo.com>
parents:
10297
diff
changeset
|
310 |
8305 | 311 output.iterations = niter; |
10296 | 312 output.funcCount = nfev; |
8305 | 313 output.bracket = [a, b]; |
314 output.bracketf = [fa, fb]; | |
315 | |
316 endfunction | |
317 | |
8507 | 318 ## 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
|
319 ## bad results. |
8305 | 320 function fx = guarded_eval (fun, x) |
321 fx = fun (x); | |
322 fx = fx(1); | |
323 if (! isreal (fx)) | |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
324 error ("fzero:notreal", "fzero: non-real value encountered"); |
8305 | 325 elseif (isnan (fx)) |
8468
866492035ecf
fsolve.m, fzero.m: undo part of previous change
John W. Eaton <jwe@octave.org>
parents:
8467
diff
changeset
|
326 error ("fzero:isnan", "fzero: NaN value encountered"); |
8305 | 327 endif |
328 endfunction | |
329 | |
10296 | 330 %!shared opt0 |
331 %! opt0 = optimset ("tolx", 0); | |
332 %!assert(fzero(@cos, [0, 3], opt0), pi/2, 10*eps) | |
333 %!assert(fzero(@(x) x^(1/3) - 1e-8, [0,1], opt0), 1e-24, 1e-22*eps) |