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