Page 152 -
P. 152

r =  3  − 5                      (5.29)
                                                               2

                             and hence, the name of the method.
                              The following function M-file implements the above algorithm:

                                function [xmin,ymin]=goldensection(funname,a,b,
                                   tolerance)
                                r=(3-sqrt(5))/2;
                                c=a+r*(b-a);
                                fc=feval(funname,c);
                                d=a+(1-r)*(b-a);
                                fd=feval(funname,d);
                                while d-c>tolerance
                                   if fc>=fd
                                   dnew=c+(1-r)*(b-c);
                                   a=c;
                                   c=d;
                                   fc=fd;
                                   d=dnew;
                                   fd=feval(funname,dnew);

                                   else
                                   cnew=a+r*(d-a);
                                   b=d;
                                   d=c;
                                   fd=fc;
                                   c=cnew;
                                   fc=feval(funname,cnew);
                                   end
                                end

                                xmin=(c+d)/2;
                                ymin=feval(funname,xmin);

                              For example, if we wanted to find the position of the minimum of the
                             cosine function and its value in the interval 3 < x < 3.5, accurate to 10 , we
                                                                                           –4
                             would enter in the command window, after having saved the above function
                             M-file, the following command:


                             © 2001 by CRC Press LLC
   147   148   149   150   151   152   153   154   155   156   157