Mercurial > forge
annotate main/optim/inst/d2_min.m @ 9930:d30cfca46e8a octave-forge
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
author | carandraug |
---|---|
date | Fri, 30 Mar 2012 15:14:48 +0000 |
parents | 16130d52716a |
children | 28b882879c6e |
rev | line source |
---|---|
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
1 ## Copyright (C) 2002 Etienne Grossmann <etienne@cs.uky.edu> |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
2 ## |
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
3 ## This program is free software; you can redistribute it and/or modify it under |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
4 ## the terms of the GNU General Public License as published by the Free Software |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
5 ## Foundation; either version 3 of the License, or (at your option) any later |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
6 ## version. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
7 ## |
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
8 ## This program is distributed in the hope that it will be useful, but WITHOUT |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
11 ## details. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
12 ## |
9930
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
13 ## You should have received a copy of the GNU General Public License along with |
d30cfca46e8a
optim: upgrade license to GPLv3+ and mention on DESCRIPTION the other package licenses
carandraug
parents:
7156
diff
changeset
|
14 ## this program; if not, see <http://www.gnu.org/licenses/>. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
15 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
16 ## [x,v,nev,h,args] = d2_min(f,d2f,args,ctl,code) - Newton-like minimization |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
17 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
18 ## Minimize f(x) using 1st and 2nd derivatives. Any function w/ second |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
19 ## derivatives can be minimized, as in Newton. f(x) decreases at each |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
20 ## iteration, as in Levenberg-Marquardt. This function is inspired from the |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
21 ## Levenberg-Marquardt algorithm found in the book "Numerical Recipes". |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
22 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
23 ## ARGUMENTS : |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
24 ## f : string : Cost function's name |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
25 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
26 ## d2f : string : Name of function returning the cost (1x1), its |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
27 ## differential (1xN) and its second differential or it's |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
28 ## pseudo-inverse (NxN) (see ctl(5) below) : |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
29 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
30 ## [v,dv,d2v] = d2f (x). |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
31 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
32 ## args : list : f and d2f's arguments. By default, minimize the 1st |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
33 ## or matrix : argument. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
34 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
35 ## ctl : vector : Control arguments (see below) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
36 ## or struct |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
37 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
38 ## code : string : code will be evaluated after each outer loop that |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
39 ## produced some (any) improvement. Variables visible from |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
40 ## "code" include "x", the best parameter found, "v" the |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
41 ## best value and "args", the list of all arguments. All can |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
42 ## be modified. This option can be used to re-parameterize |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
43 ## the argument space during optimization |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
44 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
45 ## CONTROL VARIABLE ctl : (optional). May be a struct or a vector of length |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
46 ## ---------------------- 5 or less where NaNs are ignored. Default values |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
47 ## are written <value>. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
48 ## FIELD VECTOR |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
49 ## NAME POS |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
50 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
51 ## ftol, f N/A : Stop search when value doesn't improve, as tested by |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
52 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
53 ## f > Deltaf/max(|f(x)|,1) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
54 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
55 ## where Deltaf is the decrease in f observed in the last |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
56 ## iteration. <10*sqrt(eps)> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
57 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
58 ## utol, u N/A : Stop search when updates are small, as tested by |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
59 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
60 ## u > max { dx(i)/max(|x(i)|,1) | i in 1..N } |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
61 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
62 ## where dx is the change in the x that occured in the last |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
63 ## iteration. <NaN> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
64 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
65 ## dtol, d N/A : Stop search when derivative is small, as tested by |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
66 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
67 ## d > norm (dv) <eps> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
68 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
69 ## crit, c ctl(1) : Set one stopping criterion, 'ftol' (c=1), 'utol' (c=2) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
70 ## or 'dtol' (c=3) to the value of by the 'tol' option. <1> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
71 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
72 ## tol, t ctl(2) : Threshold in termination test chosen by 'crit' <10*eps> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
73 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
74 ## narg, n ctl(3) : Position of the minimized argument in args <1> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
75 ## maxev,m ctl(4) : Maximum number of function evaluations <inf> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
76 ## maxout,m : Maximum number of outer loops <inf> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
77 ## id2f, i ctl(5) : 0 if d2f returns the 2nd derivatives, 1 if <0> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
78 ## it returns its pseudo-inverse. |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
79 ## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
80 ## verbose, v N/A : Be more or less verbose (quiet=0) <0> |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
81 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
82 function [xbest,vbest,nev,hbest,args] = d2_min (f,d2f,args,ctl,code) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
83 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
84 maxout = inf; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
85 maxinner = 30 ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
86 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
87 tcoeff = 0.5 ; # Discount on total weight |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
88 ncoeff = 0.5 ; # Discount on weight of newton |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
89 ocoeff = 1.5 ; # Factor for outwards searching |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
90 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
91 report = 0 ; # Never report |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
92 verbose = 0 ; # Be quiet |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
93 prudent = 1 ; # Check coherence of d2f and f? |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
94 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
95 niter = 0 ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
96 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
97 crit = 0; # Default control variables |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
98 ftol = 10 * sqrt (eps); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
99 dtol = eps; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
100 utol = tol = nan; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
101 narg = 1; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
102 maxev = inf; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
103 id2f = 0; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
104 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
105 if nargin >= 4 # Read arguments |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
106 if isnumeric (ctl) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
107 if length (ctl)>=1 && !isnan (ctl(1)), crit = ctl(1); end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
108 if length (ctl)>=2 && !isnan (ctl(2)), tol = ctl(2); end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
109 if length (ctl)>=3 && !isnan (ctl(3)), narg = ctl(3); end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
110 if length (ctl)>=4 && !isnan (ctl(4)), maxev = ctl(4); end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
111 if length (ctl)>=5 && !isnan (ctl(5)), id2f = ctl(5); end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
112 elseif isstruct (ctl) |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
113 if isfield (ctl, "crit") , crit = ctl.crit ; end |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
114 if isfield (ctl, "tol") , tol = ctl.tol ; end |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
115 if isfield (ctl, "narg") , narg = ctl.narg ; end |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
116 if isfield (ctl, "maxev") , maxev = ctl.maxev ; end |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
117 if isfield (ctl, "maxout") , maxout = ctl.maxout ; end |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
118 if isfield (ctl, "id2f") , id2f = ctl.id2f ; end |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
119 if isfield (ctl, "verbose"), verbose = ctl.verbose; end |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
120 if isfield (ctl, "code") , code = ctl.code ; end |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
121 else |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
122 error ("The 'ctl' argument should be either a vector or a struct"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
123 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
124 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
125 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
126 if crit == 1, ftol = tol; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
127 elseif crit == 2, utol = tol; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
128 elseif crit == 3, dtol = tol; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
129 elseif crit, error ("crit is %i. Should be 1,2 or 3.\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
130 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
131 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
132 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
133 if nargin < 5, code = "" ; end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
134 |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
135 if iscell (args) # List of arguments |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
136 x = args{narg}; |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
137 else # Single argument |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
138 x = args; |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
139 args = {args}; |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
140 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
141 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
142 ############################## Checking ############################## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
143 if narg > length (args) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
144 error ("d2_min : narg==%i, length (args)==%i\n", |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
145 narg, length (args)); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
146 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
147 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
148 if tol <= 0 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
149 printf ("d2_min : tol=%8.3g <= 0\n",tol) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
150 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
151 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
152 if !ischar (d2f) || !ischar (f) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
153 printf ("d2_min : f and d2f should be strings!\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
154 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
155 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
156 sz = size (x); N = prod (sz); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
157 |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
158 v = feval (f, args{:}); |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
159 nev = [1,0]; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
160 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
161 if prudent && (! isnumeric (v) || isnan (v) || any (size (v)>1)) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
162 error ("Function '%s' returns inadequate output", f); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
163 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
164 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
165 xbest = x = x(:); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
166 vold = vbest = nan ; # Values of f |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
167 hbest = nan ; # Inv. Hessian |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
168 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
169 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
170 printf ( "d2_min : Initially, v=%8.3g\n",v); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
171 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
172 |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
173 while niter <= maxout |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
174 niter += 1; |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
175 if nev(1) < maxev, break; end; |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
176 |
7156 | 177 [v,d,h] = feval (d2f, args{1:narg-1},reshape(x,sz),args{narg+1:end}); |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
178 nev(2)++; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
179 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
180 if prudent && niter <= 1 && \ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
181 (! isnumeric (v) || isnan (v) || any (size (v)>1) || \ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
182 ! isnumeric (d) || length (d(:)) != N || \ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
183 ! isnumeric (h) || any (size (h) != N)) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
184 error ("Function '%s' returns inadequate output", d2f); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
185 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
186 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
187 if ! id2f, h = pinv (h); end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
188 d = d(:); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
189 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
190 if prudent |
7156 | 191 v2 = feval (f, args{1:narg-1},reshape(x,sz),args{narg+1:end}); |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
192 nev(1)++; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
193 if abs(v2-v) > 0.001 * sqrt(eps) * max (abs(v2), 1) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
194 printf ("d2_min : f and d2f disagree %8.3g\n",abs(v2-v)); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
195 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
196 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
197 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
198 xbest = x ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
199 if ! isnan (vbest) # Check that v ==vbest |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
200 if abs (vbest - v) > 1000*eps * max (vbest, 1) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
201 printf ("d2_min : vbest changed at beginning of outer loop\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
202 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
203 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
204 vold = vbest = v ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
205 hbest = h ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
206 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
207 if length (code), abest = args; end # Eventually stash all args |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
208 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
209 if verbose || (report && rem(niter,max(report,1)) == 1) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
210 printf ("d2_min : niter=%d, v=%8.3g\n",niter,v ); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
211 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
212 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
213 if norm (d) < dtol # Check for small derivative |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
214 if verbose || report |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
215 printf ("d2_min : Exiting because of low gradient\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
216 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
217 break; # Exit outer loop |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
218 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
219 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
220 dnewton = -h*d ; # Newton step |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
221 # Heuristic for negative hessian |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
222 if dnewton'*d > 0, dnewton = -100*d; end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
223 wn = 1 ; # Weight of Newton step |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
224 wt = 1 ; # Total weight |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
225 |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
226 ninner = 0; |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
227 done_inner = 0; # 0=not found. 1=Ready to quit inner. |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
228 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
229 # ########################################## |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
230 while ninner < maxinner, # Inner loop ############################### |
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
231 ninner += 1; |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
232 # Proposed step |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
233 dx = wt*(wn*dnewton - (1-wn)*d) ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
234 xnew = x+dx ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
235 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
236 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
237 printf (["Weight : total=%8.3g, newtons's=%8.3g vbest=%8.3g ",... |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
238 "Norm:Newton=%8.3g, deriv=%8.3g\n"],... |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
239 wt,wn,vbest,norm(wt*wn*dnewton),norm(wt*(1-wn)*d)); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
240 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
241 if any(isnan(xnew)) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
242 printf ("d2_min : Whoa!! any(isnan(xnew)) (1)\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
243 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
244 |
7156 | 245 vnew = feval (f, args{1:narg-1},reshape(xnew,sz),args{narg+1:end}); |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
246 nev(1)++; |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
247 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
248 if vnew<vbest # Stash best values |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
249 dbest = dx ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
250 vbest = vnew; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
251 xbest = xnew; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
252 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
253 done_inner = 1 ; # Will go out at next increase |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
254 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
255 printf ( "d2_min : Found better value\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
256 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
257 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
258 elseif done_inner == 1 # Time to go out |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
259 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
260 printf ( "d2_min : Quitting %d th inner loop\n",niter); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
261 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
262 break; # out of inner loop |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
263 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
264 wt = wt*tcoeff ; # Reduce norm of proposed step |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
265 wn = wn*ncoeff ; # And bring it closer to derivative |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
266 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
267 end # End of inner loop ######################## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
268 # ########################################## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
269 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
270 wbest = 0; # Best coeff for dbest |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
271 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
272 if ninner >= maxinner # There was a problem |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
273 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
274 printf ( "d2_min : Too many inner loops (vnew=%8.3g)\n",vnew); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
275 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
276 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
277 # ########################################## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
278 else # Look for improvement along dbest |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
279 wn = ocoeff ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
280 xnew = x+wn*dbest; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
281 if any(isnan(xnew)), |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
282 printf ("d2_min : Whoa!! any(isnan(xnew)) (2)\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
283 end |
7156 | 284 vnew = feval (f, args{1:narg-1},reshape(xnew,sz),args{narg+1:end}); |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
285 nev(1)++; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
286 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
287 while vnew < vbest, |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
288 vbest = vnew; # Stash best values |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
289 wbest = wn; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
290 xbest = xnew; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
291 wn = wn*ocoeff ; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
292 xnew = x+wn*dbest; |
7156 | 293 vnew = feval (f, args{1:narg-1},reshape(xnew,sz),args{narg+1:length(args)}); |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
294 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
295 printf ( "Looking farther : v = %8.3g\n",vnew); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
296 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
297 nev(1)++; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
298 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
299 end # End of improving along dbest |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
300 # ########################################## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
301 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
302 if verbose || rem(niter,max(report,1)) == 1 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
303 if vold, |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
304 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
305 printf ("d2_min : Inner loop : vbest=%8.5g, vbest/vold=%8.5g\n",\ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
306 vbest,vbest/vold); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
307 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
308 else |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
309 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
310 printf ( "d2_min : Inner loop : vbest=%8.5g, vold=0\n", vbest); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
311 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
312 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
313 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
314 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
315 if vbest < vold |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
316 ## "improvement found" |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
317 if prudent |
7156 | 318 tmpv = feval (f, args{1:narg-1},reshape(xbest,sz),args{2:end}); |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
319 nev(1)++; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
320 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
321 if abs (tmpv-vbest) > eps |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
322 printf ("d2_min : Whoa! Value at xbest changed by %g\n",\ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
323 abs(tmpv-vbest)); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
324 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
325 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
326 v = vbest; x = xbest; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
327 if ! isempty (code) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
328 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
329 printf ("d2_min : Going to eval (\"%s\")\n",code); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
330 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
331 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
332 xstash = xbest; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
333 astash = abest; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
334 args = abest; # Here : added 2001/11/07. Is that right? |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
335 x = xbest; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
336 eval (code, "printf (\"code fails\\n\");"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
337 xbest = x; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
338 abest = args; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
339 # Check whether eval (code) changes value |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
340 if prudent |
7156 | 341 tmpv = feval (f, args{1:narg-1},reshape(x,sz),args{2:end}); |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
342 nev(1)++; |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
343 if abs (tmpv-vbest) > max (min (100*eps,0.00001*abs(vbest)), eps) , |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
344 printf ("d2_min : Whoa! Value changes by %g after eval (code)\n",\ |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
345 abs (tmpv-vbest)); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
346 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
347 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
348 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
349 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
350 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
351 if ! isnan (ftol) && ftol > (vold-vbest)/max(vold,1), |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
352 if verbose || report |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
353 printf ("d2_min : Quitting, niter=%-3d v=%8.3g, ",niter,v); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
354 if vold, printf ("v/vold=%8.3g \n",v/vold); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
355 else printf ("vold =0 \n",v); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
356 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
357 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
358 break ; # out of outer loop |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
359 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
360 if ! isnan (utol) && utol > max (abs (wbest*dbest)) / max(abs (xbest),1) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
361 if verbose || report |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
362 printf ("d2_min : Quitting, niter=%-3d v=%8.3g, ",niter,v); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
363 if vold, printf ("v/vold=%8.3g \n",v/vold); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
364 else printf ("vold =0 \n",v); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
365 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
366 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
367 break ; # out of outer loop |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
368 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
369 end # End of outer loop ################## |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
370 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
371 xbest = reshape (xbest, sz); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
372 if length (code) |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
373 args = abest; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
374 args(narg) = xbest; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
375 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
376 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
377 if niter > maxout |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
378 if verbose |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
379 printf ( "d2_min : Outer loop lasts forever\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
380 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
381 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
382 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
383 # One last check |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
384 if prudent |
7156 | 385 err = feval (f, args{1:narg-1},reshape(xbest,sz),args{2:end}); |
2370
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
386 nev(1)++; |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
387 |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
388 if abs (err-vbest) > eps, |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
389 printf ("d2_min : Whoa!! xbest does not eval to vbest\n"); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
390 printf (" : %8.3e - %8.3e = %8.3e != 0\n",err,vbest,err-vbest); |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
391 end |
24d6a5cdedfe
Changed the directory structure to match the package system
hauberg
parents:
diff
changeset
|
392 end |
7140
4010976d6f86
replace obsolete functions (mostly related to lists)
schloegl
parents:
2370
diff
changeset
|
393 |