File size: 7,893 Bytes
5ab75a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
function [xmin,ymin,converged,nbisect,nshift,xbuf,ybuf] = shift_bisect_minsearch( ...
    fun,x0,y0,Nbisectmax,Nshiftmax,tolX,tolY,verbose)

% Find the minimum of the function "y = fun(x)" given the initial search
% interval (x1,x2).
%
% Because this function uses an algorithm that performs both shifting and
% bisection in search for the minimum, (x1,x2) does not necessarily have to
% bound the minimum.
%
% This function finds one and only one minimum, so if there are multiple
% minima over (x1,x2), then this function will only find one at most.
%

% Set defaults for inputs
Nargin = nargin;

% Initial number of grid points (should be 5 or larger)
N0 = numel(x0);
if (N0 < 5)
    error('shift_bisect_minsearch requires five or more points');
end
if N0 ~= numel(y0)
    error('shift_bisect_minsearch needs same number of (x,y) grid points');
end
if any(diff(x0) < 0)
    error('shift_bisect_minsearch needs monotonically increasing grid points');
end

% Max number of bisections
na = 4;
if (Nargin < na) || isempty(Nbisectmax)
    Nbisectmax = 100;
end

% Max number of shifting iterations
na = na+1;
if (Nargin < na) || isempty(Nshiftmax)
    Nshiftmax = 100;
end

% Tolerance for x convergence (can be NaN to only use y convergence)
na = na+1;
if (Nargin < na) || isempty(tolX)
    tolX = 1e-4;
end

% Tolerance for y convergence (must be positive)
na = na+1;
if (Nargin < na) || isempty(tolY)
    tolY = 1e-4;
end
if isnan(tolY) || tolY < 0
    error('Invalid tolY parameter');
end

% Verbosity (0 => no reports, 1 => few reports to user, 2 => many reports)
na = na+1;
if (Nargin < na) || isempty(verbose)
    verbose = 1;
end

% Check to see if output buffering is required
buffering = (nargout > 5);

% Find minimum value over initial grid
if max(y0) ~= min(y0)
    % Find min point
    [~, nmin] = min(y0);
else
    % Use center point if all initial y values are equal
    nmin = round((Ninitial-1)/2+1);
end

% Initialize the 5-point bisection search buffer

if (nmin < 3)
    % Minimum is near or at left boundary
    n1 = 1;
    n2 = 5;
elseif (nmin > N0-2)
    % Minimum is near or at right boundary
    n1 = N0-4;
    n2 = N0;
else
    % Minimum point in center somewhere
    n1 = nmin-2;
    n2 = nmin+2;
end

x = x0(n1:n2);      % Holds 5 x values
y = y0(n1:n2);      % Holds 5 y values
c = false(size(x));  % Holds 5 logical values (true => y needs to be calculated)

% Initialize the buffers to hold new variables

xnew = zeros(size(x));
ynew = xnew;
cnew = true(size(x));

% Perform iterative search

if (verbose > 0)
    if buffering
        bufstr = ' (with output buffering)';
    else
        bufstr = '';
    end
    disp(['Performing shifting + bisection search for minimum' bufstr]);
end

% Iteration and convergence flags
still_iterating  = true; converged = false;

nshift = 0;  % Counts shifts to left or right
nbisect = 0; % Counts bisections

if buffering
    xbuf = x0;
    ybuf = y0;
end

% Iterate until converged or stopped
while still_iterating

    % Calculate any required y values
    for n=1:5
        if c(n)
            y(n) = fun(x(n));
            c(n) = false;
            if buffering
                xbuf = cat(2,xbuf,x(n));
                ybuf = cat(2,ybuf,y(n));
            end
        end
    end
    
    % Find min among the 5 calculated values
    [ymin, nmin] = min(y);
    if (ymin == max(y))
        % Use center point if all y values are equal
        nmin = 3;
    end
    xmin = x(nmin);
    
    if (verbose > 1)
        disp(['  (x,y) = ' num2str([x(nmin) y(nmin)])]);
    end    
    
    % Either shift if min is on edge, or bisect if min is interior
    
    if (nmin == 1)

        % First x point is the lowest y value (perform shift to left)
        
        if (verbose > 1)
            disp([' Shifting left, nshift = ' num2str(nshift)]);
        end    

        xnew(2:5) = x(1:4);
        ynew(2:5) = y(1:4);
        cnew(2:5) = false;

        dx        = x(2)-x(1);
        xnew(1)   = x(1)-dx;
        cnew(1)   = true;
        
        nshift    = nshift+1;

    elseif (nmin == 5)

        % Last x point is the highest y value (perform shift to right)

        if (verbose > 1)
            disp([' Shifting right, nshift = ' num2str(nshift)]);
        end    
        
        xnew(1:4) = x(2:5);
        ynew(1:4) = y(2:5);
        cnew(1:4) = false;

        dx        = x(5)-x(4);
        xnew(5)   = x(5)+dx;
        cnew(5)   = true;
        
        nshift    = nshift+1;

    else
        
        % One of the interior points has the highest y value, so check for
        % convergence, and set up to perform next bisection if required

        nlo = nmin-1; nhi = nmin+1;
        
        % Check for convergence
        
        dxlo = abs(x(nmin)-x(nlo));
        dxhi = abs(x(nmin)-x(nhi));
        dx   = max([dxlo dxhi]);
        
        if verbose > 2
            disp([' dx = ' num2str(dx) ' dx/tolX = ' num2str(dx/tolX)]);
        end
        
        if (dx < tolX) || (dx < 10*eps(x(nmin)))
            
            converged = true;
            if (verbose > 0)
                disp(['Convergence achieved because dx < tolX or dx < epsX (nbisect = ' ...
                    num2str(nbisect) ' nshift = ' num2str(nshift) ')']);
            end 
            
        else
            
            dylo = y(nlo)-y(nmin);
            dyhi = y(nhi)-y(nmin);
            dy   = max([dylo dyhi]);
            
            if verbose > 2 && ~isnan(tolY)
                disp([' dy = ' num2str(dy) ' dy/tolY = ' num2str(dy/tolY)]);
            end
            
            if (dy < tolY)
                converged = true;
                if (verbose > 0)
                    disp(['Convergence achieved because dy < tolY (nbisect = ' ...
                        num2str(nbisect) ' nshift = ' num2str(nshift) ')']);
                end                
            end
            
        end

        % If not yet converged then set up to perform next bisection
        
        if ~converged

            xnew(1) = x(nlo);
            ynew(1) = y(nlo);
            cnew(1) = false;

            xnew(2) = (x(nlo)+x(nmin))/2;
            cnew(2) = true;

            xnew(3) = x(nmin);
            ynew(3) = y(nmin);
            cnew(3) = false;

            xnew(4) = (x(nhi)+x(nmin))/2;
            cnew(4) = true;

            xnew(5) = x(nhi);
            ynew(5) = y(nhi);
            cnew(5) = false;

            nbisect = nbisect+1;
            
        end
        
    end

    % Check for NaN values, or too many shift or bisection iterations
    
    if any(isnan(y))
        
        % NaN values detected
        if (verbose > 0)
            disp('NaN y-values detected; returning unconverged result');
        end
        still_iterating = false;
  
    elseif (nbisect > Nbisectmax)

        % Too many bisections
        if (verbose > 0)
            disp('Maximum number of bisections exceeded; returning unconverged result');
        end
        still_iterating = false;

    elseif (nshift > Nshiftmax)

        % Too many shifts
        if (verbose > 0)
            disp('Maximum number of shifts exceeded; returning unconverged result');
        end
        still_iterating = false;

    end
    
    % Check for convergence        
    if converged
        % Stop iterating if converged
        still_iterating = false;
    else
        % Copy the new buffers to set up for next iteration
        x = xnew;
        y = ynew;
        c = cnew;
    end
    
end

% Sort the buffered values
if buffering
    [xbuf,srt] = sort(xbuf);
    ybuf = ybuf(srt);
end

return;
end