Page 300 - MATLAB an introduction with applications
P. 300

Optimization ———  285

                   function [xMin,fMin] = goldSearch(func,a,b,tol)
                   % Golden section search method
                   % func = function that returns f(x)
                   % a, b = limits of the interval for the minimum
                   % tol = error tol = 1.0e–6
                   % fMin = min of f(x)
                   % xMin = x at min point
                   if nargin < 4; tol = 1.0e–6; end
                   nIter = ceil(–2.078087*log(tol/abs(b–a)));
                   R = 0.618033989;
                   C = 1.0 – R;
                   x1 = R*a + C*b;
                   x2 = C*a + R*b;
                   f1 = feval(func,x1);
                   f2 = feval(func,x2);
                   for i =1:nIter
                   if f1 > f2
                   a = x1; x1 = x2; f1 = f2;
                   x2 = C*a + R*b;
                   f2 = feval(func,x2);
                   else
                   b = x2; x2 = x1; f2 = f1;
                   x1 = R*a + C*b;
                   f1 = feval(func,x1);
                   end
                   end
                   if f1 < f2; fMin = f1; xMin = x1;
                   else; fMin = f2; xMin = x2;
                   end

                   Example E5.9: Minimize the following function f (x) by the penalty function method:
                                                                   2
                                                        2
                                              2
                                                                                2
                                 f (x) = [(x  + 2)  + 5(x  – 2) ] [(x  – 1.5)  + 0.5(x  – 0.5) ]
                                                                          2
                                                    2
                                                             1
                                         1
                                        −x         0 
                                          1        
                                                     0
                                        −x 2       
                                                  
                   subject to       3 −x 1  x x  +  4x 2  − 6 ≤  0 
                                        1 2
                                                  
                                     2 +x 1  x 2  −  5    
                                                     0
                                                     
                                          2        
                                     3 −x 1  4x 2  −  4x 2     0  . 
   295   296   297   298   299   300   301   302   303   304   305