%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Golden Section Search for Minimizer of a Unimodal Function %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 A=[];C=[];
%%
%%   Select Parameters: endpoints & error tolerance
 x_l = -5 , x_r = 15 , eps= .001, 
%
%%   Initialize variables
 a= x_l; b = x_r; 
 rho=(3-sqrt(5))/2, l = b-a;
 N = round( log(eps/(b-a))/log(1-rho)) +1,
%%
%%   Initial step 
 D = rho * l;
 a1 = a +D    ; b1 = b-D;
 F1 = phi(a1) ; F2 = phi(b1); F = min(F1,F2);
%%
%%   Begin interation loop
 for i=1:N;
   l = (1-rho)*l; D= rho * l;
   if F1 < F2,            %%  select LEFT subinterval
	  F= min(F,F1);
      b = b1 ; b1 = a1; F2 = F1;
	  a1 = a + D;       F1 = phi(a1);  
     else,               %%  select RIGHT subinterval 
	  F= min(F,F2);
      a = a1 ; a1 = b1; F1 = F2;
	  b1 = b - D;       F2 = phi(b1);
     end
	 	 
   C=[ C
       [a a1 b1 b] ];    %%  build matrix for output

   A=[ A                 %%  build matrix for output
       [a b F] ];
   end
%%   End interation loop

C                        %%  output current interval and subinterval 
A                        %%  output subinterval and current min
     
