Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/netlab3.3/minbrack.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 [br_min, br_mid, br_max, num_evals] = minbrack(f, a, b, fa, ... | |
2 varargin) | |
3 %MINBRACK Bracket a minimum of a function of one variable. | |
4 % | |
5 % Description | |
6 % BRMIN, BRMID, BRMAX, NUMEVALS] = MINBRACK(F, A, B, FA) finds a | |
7 % bracket of three points around a local minimum of F. The function F | |
8 % must have a one dimensional domain. A < B is an initial guess at the | |
9 % minimum and maximum points of a bracket, but MINBRACK will search | |
10 % outside this interval if necessary. The bracket consists of three | |
11 % points (in increasing order) such that F(BRMID) < F(BRMIN) and | |
12 % F(BRMID) < F(BRMAX). FA is the value of the function at A: it is | |
13 % included to avoid unnecessary function evaluations in the | |
14 % optimization routines. The return value NUMEVALS is the number of | |
15 % function evaluations in MINBRACK. | |
16 % | |
17 % MINBRACK(F, A, B, FA, P1, P2, ...) allows additional arguments to be | |
18 % passed to F | |
19 % | |
20 % See also | |
21 % LINEMIN, LINEF | |
22 % | |
23 | |
24 % Copyright (c) Ian T Nabney (1996-2001) | |
25 | |
26 % Check function string | |
27 f = fcnchk(f, length(varargin)); | |
28 | |
29 % Value of golden section (1 + sqrt(5))/2.0 | |
30 phi = 1.6180339887499; | |
31 | |
32 % Initialise count of number of function evaluations | |
33 num_evals = 0; | |
34 | |
35 % A small non-zero number to avoid dividing by zero in quadratic interpolation | |
36 TINY = 1.e-10; | |
37 | |
38 % Maximal proportional step to take: don't want to make this too big | |
39 % as then spend a lot of time finding the minimum inside the bracket | |
40 max_step = 10.0; | |
41 | |
42 fb = feval(f, b, varargin{:}); | |
43 num_evals = num_evals + 1; | |
44 | |
45 % Assume that we know going from a to b is downhill initially | |
46 % (usually because gradf(a) < 0). | |
47 if (fb > fa) | |
48 % Minimum must lie between a and b: do golden section until we find point | |
49 % low enough to be middle of bracket | |
50 c = b; | |
51 b = a + (c-a)/phi; | |
52 fb = feval(f, b, varargin{:}); | |
53 num_evals = num_evals + 1; | |
54 while (fb > fa) | |
55 c = b; | |
56 b = a + (c-a)/phi; | |
57 fb = feval(f, b, varargin{:}); | |
58 num_evals = num_evals + 1; | |
59 end | |
60 else | |
61 % There is a valid bracket upper bound greater than b | |
62 c = b + phi*(b-a); | |
63 fc = feval(f, c, varargin{:}); | |
64 num_evals = num_evals + 1; | |
65 bracket_found = 0; | |
66 | |
67 while (fb > fc) | |
68 % Do a quadratic interpolation (i.e. to minimum of quadratic) | |
69 r = (b-a).*(fb-fc); | |
70 q = (b-c).*(fb-fa); | |
71 u = b - ((b-c)*q - (b-a)*r)/(2.0*(sign(q-r)*max([abs(q-r), TINY]))); | |
72 ulimit = b + max_step*(c-b); | |
73 | |
74 if ((b-u)'*(u-c) > 0.0) | |
75 % Interpolant lies between b and c | |
76 fu = feval(f, u, varargin{:}); | |
77 num_evals = num_evals + 1; | |
78 if (fu < fc) | |
79 % Have a minimum between b and c | |
80 br_min = b; | |
81 br_mid = u; | |
82 br_max = c; | |
83 return; | |
84 elseif (fu > fb) | |
85 % Have a minimum between a and u | |
86 br_min = a; | |
87 br_mid = c; | |
88 br_max = u; | |
89 return; | |
90 end | |
91 % Quadratic interpolation didn't give a bracket, so take a golden step | |
92 u = c + phi*(c-b); | |
93 elseif ((c-u)'*(u-ulimit) > 0.0) | |
94 % Interpolant lies between c and limit | |
95 fu = feval(f, u, varargin{:}); | |
96 num_evals = num_evals + 1; | |
97 if (fu < fc) | |
98 % Move bracket along, and then take a golden section step | |
99 b = c; | |
100 c = u; | |
101 u = c + phi*(c-b); | |
102 else | |
103 bracket_found = 1; | |
104 end | |
105 elseif ((u-ulimit)'*(ulimit-c) >= 0.0) | |
106 % Limit parabolic u to maximum value | |
107 u = ulimit; | |
108 else | |
109 % Reject parabolic u and use golden section step | |
110 u = c + phi*(c-b); | |
111 end | |
112 if ~bracket_found | |
113 fu = feval(f, u, varargin{:}); | |
114 num_evals = num_evals + 1; | |
115 end | |
116 a = b; b = c; c = u; | |
117 fa = fb; fb = fc; fc = fu; | |
118 end % while loop | |
119 end % bracket found | |
120 br_mid = b; | |
121 if (a < c) | |
122 br_min = a; | |
123 br_max = c; | |
124 else | |
125 br_min = c; | |
126 br_max = a; | |
127 end |