home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 4.ddi / OPTIM.DI$ / SEARCHQ.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  2.6 KB  |  83 lines

  1. function [pcnt, matl,matx,stepsize,fnew,how]=searchq(pcnt,fnew,oldx,matl,matx,sd,gdold,stepsize,how)
  2. %SEARCHQ Line search routine for FMINU and LEASTSQ functions.
  3. %    Performs line search procedure for unconstrained and least squares
  4. %     optimization. Uses Quadratic Interpolation.
  5. %    When finished pcnt returns 0.
  6.  
  7. %    Copyright (c) 1990 by the MathWorks, Inc.
  8. %    Andy Grace 7-9-90.
  9. if pcnt==1
  10. % Case 1: Next point less than initial point. 
  11. %      Increase step-length based on last gradient evaluation
  12.     if fnew<matl(1)
  13. % Quadratic Extrapolation using gradient of first point and 
  14. % values of two other points.
  15.         matl(2)=fnew;
  16.         matx(2)=stepsize;
  17.         newstep=-0.5*gdold*stepsize*stepsize/(fnew-gdold*stepsize-matl(1)+eps);
  18.         if newstep<stepsize,how=[how,'QEF ']; newstep=1.2*stepsize; end
  19.         stepsize=1.2*newstep;
  20.         pcnt=2;
  21.     else
  22. % Case 2: New point greater than initial point. Decrease step-length.
  23.         matl(3)=fnew;
  24.         matx(3)=stepsize;
  25. %Interpolate to get stepsize
  26.         stepsize=max([1e-8*stepsize,-gdold*0.5*stepsize^2/(fnew-gdold*stepsize-matl(1)+eps)]);
  27.         how=[how,'r'];
  28.         pcnt=3;
  29.     end
  30. % Case 3: Last run was Case 1 (pcnt=2) and new point less than 
  31. %      both of other 2. Replace. 
  32. elseif pcnt==2  & fnew< matl(2)
  33.     newstep=cubici2(gdold,[matl(1);matl(2);fnew],[matx(1);matx(2);stepsize]);
  34.     if newstep<stepsize,how=[how, 'CEF ']; end
  35.         matl(1)=matl(2);
  36.         matx(1)=matx(2);
  37.         matl(2)=fnew;
  38.         matx(2)=stepsize;
  39.         stepsize=min([newstep,1])+1.5*stepsize;
  40.         stepsize=max([1.2*newstep,1.2*stepsize]);
  41.         how=[how,'i'];
  42. % Case 4: Last run was Case 2: (pcnt=3) and new function still 
  43. %      greater than initial value.
  44. elseif pcnt==3 & fnew>=matl(1)
  45.     matl(2)=fnew;
  46.     matx(2)=stepsize;
  47.      if stepsize<1e-6
  48.         newstep=-stepsize/2;
  49. %        if abs(newstep)<eps, newstep=rand(1)-0.5; how=[how,'RAND']; end
  50.     else
  51.         newstep=cubici2(gdold,[matl(1);matl(3);fnew],[matx(1);matx(3);stepsize]);
  52.     end
  53.     matx(3)=stepsize;
  54.     if isnan(newstep), stepsize=stepsize/2; else stepsize=newstep; end
  55.     matl(3)=fnew;
  56.     how=[how,'R'];
  57. % Otherwise must have Bracketed Minimum so do quadratic interpolation.
  58. %  ... having just increased step.
  59. elseif pcnt==2 & fnew>matl(2)
  60.     matx(3)=stepsize;
  61.     matl(3)=fnew;
  62.     [stepsize]=cubici2(gdold,matl,matx);
  63.     pcnt=4;
  64. % ...  having just reduced step.
  65. elseif  pcnt==3  & fnew<matl(1)
  66.     matx(2)=stepsize;
  67.     matl(2)=fnew;
  68.     [stepsize]=cubici2(gdold,matl,matx);
  69.     pcnt=4;
  70. % Have just interpolated - Check to see whether function is any better 
  71. % - if not replace.
  72. elseif pcnt==4 
  73.     pcnt=0;
  74.     stepsize=abs(stepsize);
  75. % If interpolation failed use old point.
  76.      if fnew>matl(2),
  77.         fnew=matl(2);
  78.         how='f';
  79.          stepsize=matx(2);        
  80.     end
  81. end %if pcnt==1
  82.  
  83.