annotate toolboxes/FullBNT-1.0.7/netlab3.3/linemin.m @ 0:cc4b1211e677 tip

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