www.pudn.com > mgoldensection.rar > mgoldensection.m


function [xopt,fval,k,err]=mgoldensection(f,a,b,delta,max1) 
 
 
% Purpose: golden section algorithm for linear search  
% Programmer: LU Dagang 
% Date: 25/05/2003 
% Revised: 25/05/2003 
% Version 1.0 
% Copyright (C) 2003, LU Dagang, HIT 
% 
% Modified by Peng Xiaobo (Institute of Engineering Mechanics) 
% Data :27/05/2006 
% 
% Input  - f is the objective function input as a string 'f' 
%        - a and b are the lower and upper bound of the optimized variable 
%        - delta is the prescibed precision 
%        - max1 is the maximum number interations 
%  
% Output - xopt is the optimal value of the variable 
%        - fval is the optimal value of the objective function 
 
% This program is free software; you can redistribute it and/or modify it 
% under the permission of LU Dagang 
%  
% This program is distributed in the hope that it will be useful in studying 
% the graduate course "Structural Optimum Design", but WITHOUT ANY WARRANTY. 
 
a1 = b-0.618*(b-a); 
f1 = feval(f,a1); 
b1 = a+0.618*(b-a); 
f2 = feval(f,b1); 
for k=1:max1 
   if abs(b-a) > delta  
      if f1 < f2 
        b = b1; 
        b1 = a1; 
        f2 = f1; 
        a1 = b-0.618*(b-a); 
        f1 = feval(f,a1); 
      else 
        a = a1; 
        a1 = b1; 
        f1 = f2; 
        b1 = a+0.618*(b-a); 
        f2 = feval(f,b1); 
      end 
  else break,end 
end 
xopt = (b+a)/2; 
fval = feval(f,xopt);