Page 338 - Applied Numerical Methods Using MATLAB
P. 338

UNCONSTRAINED OPTIMIZATION  327

              function [xo,fo] = Nelder0(f,abc,fabc,TolX,TolFun,k)
              [fabc,I] = sort(fabc); a = abc(I(1),:); b = abc(I(2),:); c = abc(I(3),:);
              fa = fabc(1); fb = fabc(2); fc = fabc(3);  fba = fb - fa; fcb = fc - fb;
              ifk<=0 | abs(fba) + abs(fcb) < TolFun | abs(b - a) + abs(c - b) < TolX
               xo=a;fo=fa;
               if k == 0, fprintf(’Just best in given # of iterations’), end
              else
               m = (a + b)/2;  e = 3*m - 2*c;  fe = feval(f,e);
               if fe < fb, c = e;  fc = fe;
                else
                  r = (m+e)/2;  fr = feval(f,r);
                  if fr < fc,  c = r;  fc = fr;  end
                  if fr >= fb
                    s = (c + m)/2;  fs = feval(f,s);
                    if fs < fc,  c = s;  fc = fs;
                    elseb= m;c=(a+ c)/2;    fb = feval(f,b); fc = feval(f,c);
                    end
                  end
               end
               [xo,fo] = Nelder0(f,[a;b;c],[fa fb fc],TolX,TolFun,k - 1);
              end
              function [xo,fo] = opt_Nelder(f,x0,TolX,TolFun,MaxIter)
              N = length(x0);
              if N == 1 %for 1-dimensional case
               [xo,fo] = opt_quad(f,x0,TolX,TolFun); return
              end
              S = eye(N);
              for i = 1:N %repeat the procedure for each subplane
                i1=i+1;if i1>N,i1=1;end
                abc = [x0; x0 + S(i,:); x0 + S(i1,:)]; %each directional subplane
                fabc = [feval(f,abc(1,:)); feval(f,abc(2,:)); feval(f,abc(3,:))];
                [x0,fo] = Nelder0(f,abc,fabc,TolX,TolFun,MaxIter);
                ifN<3, break; end %No repetition needed for a 2-dimensional case
              end
              xo = x0;
              %nm713.m: do_Nelder
              f713 = inline(’x(1)*(x(1)-4-x(2)) +x(2)*(x(2)-1)’,’x’);
              x0 = [0 0], TolX = 1e-4; TolFun = 1e-9; MaxIter = 100;
              [xon,fon] = opt_Nelder(f713,x0,TolX,TolFun,MaxIter)
              %minimum point and its function value
              [xos,fos] = fminsearch(f713,x0) %use the MATLAB built-in function


            This program also applies the MATLAB built-in routine “fminsearch()”to min-
            imize the same objective function for practice and confirmation. The minimization
            process is illustrated in Fig. 7.4.


            (cf) The MATLAB built-in routine “fminsearch()” uses the Nelder–Mead algorithm
               to minimize a multivariable objective function. It corresponds to “fmins()”inthe
               MATLAB of version.5.x.
   333   334   335   336   337   338   339   340   341   342   343