Page 299 - MATLAB an introduction with applications
P. 299

284 ———  MATLAB: An Introduction with Applications


                   [a,b] = goldBracket(@fLine,0.0,h);
                   [s,fMin] = goldSearch(@fLine,a,b);
                   X = X + s*V;
                   g1 = –feval(DFUNC,X);
                   if sqrt(dot(g1,g1)) <= tol
                   xMin = X; nCyc = i; return
                   end
                   gamma = dot((g1 – g0),g1)/dot(g0,g0);
                   V = g1 + gamma*V;
                   g0 = g1;
                   end
                   error(‘Method did not converge’)
                   function z = fLine(s)
                   global X FUNC V
                   z = feval(FUNC,X+s*V);

                   function [a,b] = goldBracket(func,x1,h)
                   % Brackets the minimum point of f(x)
                   % func =  returns f(x)
                   % x1= starting value of x
                   % h = initial step size
                   % a, b = limits on x
                   c = 1.618033989;
                   f1 = feval(func,x1);
                   x2 = x1 + h; f2 = feval(func,x2);
                   if f2 > f1
                   h = –h;
                   x2 = x1 + h; f2 = feval(func,x2);
                   if f2 > f1
                   a = x2; b = x1 – h; return
                   end
                   end
                   % Search loop
                   for i = 1:100
                   h = c*h;
                   x3 = x2 + h; f3 = feval(func,x3);
                   if f3 > f2
                   a = x1; b = x3; return
                   end
                   x1 = x2; f1 = f2; x2 = x3; f2 = f3;
                   end
                   error(‘Failed to find minimum’)
   294   295   296   297   298   299   300   301   302   303   304