comparison toolboxes/FullBNT-1.0.7/netlab3.3/linemin.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [x, options] = linemin(f, pt, dir, fpt, options, ...
2 varargin)
3 %LINEMIN One dimensional minimization.
4 %
5 % Description
6 % [X, OPTIONS] = LINEMIN(F, PT, DIR, FPT, OPTIONS) uses Brent's
7 % algorithm to find the minimum of the function F(X) along the line DIR
8 % through the point PT. The function value at the starting point is
9 % FPT. The point at which F has a local minimum is returned as X. The
10 % function value at that point is returned in OPTIONS(8).
11 %
12 % LINEMIN(F, PT, DIR, FPT, OPTIONS, P1, P2, ...) allows additional
13 % arguments to be passed to F().
14 %
15 % The optional parameters have the following interpretations.
16 %
17 % OPTIONS(1) is set to 1 to display error values.
18 %
19 % OPTIONS(2) is a measure of the absolute precision required for the
20 % value of X at the solution.
21 %
22 % OPTIONS(3) is a measure of the precision required of the objective
23 % function at the solution. Both this and the previous condition must
24 % be satisfied for termination.
25 %
26 % OPTIONS(14) is the maximum number of iterations; default 100.
27 %
28 % See also
29 % CONJGRAD, MINBRACK, QUASINEW
30 %
31
32 % Copyright (c) Ian T Nabney (1996-2001)
33
34 % Set up the options.
35 if(options(14))
36 niters = options(14);
37 else
38 niters = 100;
39 end
40 options(10) = 0; % Initialise count of function evaluations
41
42 display = options(1);
43
44 % Check function string
45 f = fcnchk(f, length(varargin));
46
47 % Value of golden section (1 + sqrt(5))/2.0
48 phi = 1.6180339887499;
49 cphi = 1 - 1/phi;
50 TOL = sqrt(eps); % Maximal fractional precision
51 TINY = 1.0e-10; % Can't use fractional precision when minimum is at 0
52
53 % Bracket the minimum
54 [br_min, br_mid, br_max, num_evals] = feval('minbrack', 'linef', ...
55 0.0, 1.0, fpt, f, pt, dir, varargin{:});
56 options(10) = options(10) + num_evals; % Increment number of fn. evals
57 % No gradient evals in minbrack
58
59 % Use Brent's algorithm to find minimum
60 % Initialise the points and function values
61 w = br_mid; % Where second from minimum is
62 v = br_mid; % Previous value of w
63 x = v; % Where current minimum is
64 e = 0.0; % Distance moved on step before last
65 fx = feval('linef', x, f, pt, dir, varargin{:});
66 options(10) = options(10) + 1;
67 fv = fx; fw = fx;
68
69 for n = 1:niters
70 xm = 0.5.*(br_min+br_max); % Middle of bracket
71 % Make sure that tolerance is big enough
72 tol1 = TOL * (max(abs(x))) + TINY;
73 % Decide termination on absolute precision required by options(2)
74 if (max(abs(x - xm)) <= options(2) & br_max-br_min < 4*options(2))
75 options(8) = fx;
76 return;
77 end
78 % Check if step before last was big enough to try a parabolic step.
79 % Note that this will fail on first iteration, which must be a golden
80 % section step.
81 if (max(abs(e)) > tol1)
82 % Construct a trial parabolic fit through x, v and w
83 r = (fx - fv) .* (x - w);
84 q = (fx - fw) .* (x - v);
85 p = (x - v).*q - (x - w).*r;
86 q = 2.0 .* (q - r);
87 if (q > 0.0) p = -p; end
88 q = abs(q);
89 % Test if the parabolic fit is OK
90 if (abs(p) >= abs(0.5*q*e) | p <= q*(br_min-x) | p >= q*(br_max-x))
91 % No it isn't, so take a golden section step
92 if (x >= xm)
93 e = br_min-x;
94 else
95 e = br_max-x;
96 end
97 d = cphi*e;
98 else
99 % Yes it is, so take the parabolic step
100 e = d;
101 d = p/q;
102 u = x+d;
103 if (u-br_min < 2*tol1 | br_max-u < 2*tol1)
104 d = sign(xm-x)*tol1;
105 end
106 end
107 else
108 % Step before last not big enough, so take a golden section step
109 if (x >= xm)
110 e = br_min - x;
111 else
112 e = br_max - x;
113 end
114 d = cphi*e;
115 end
116 % Make sure that step is big enough
117 if (abs(d) >= tol1)
118 u = x+d;
119 else
120 u = x + sign(d)*tol1;
121 end
122 % Evaluate function at u
123 fu = feval('linef', u, f, pt, dir, varargin{:});
124 options(10) = options(10) + 1;
125 % Reorganise bracket
126 if (fu <= fx)
127 if (u >= x)
128 br_min = x;
129 else
130 br_max = x;
131 end
132 v = w; w = x; x = u;
133 fv = fw; fw = fx; fx = fu;
134 else
135 if (u < x)
136 br_min = u;
137 else
138 br_max = u;
139 end
140 if (fu <= fw | w == x)
141 v = w; w = u;
142 fv = fw; fw = fu;
143 elseif (fu <= fv | v == x | v == w)
144 v = u;
145 fv = fu;
146 end
147 end
148 if (display == 1)
149 fprintf(1, 'Cycle %4d Error %11.6f\n', n, fx);
150 end
151 end
152 options(8) = fx;