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

  1. %BANDEMO Banana function minimization demonstration.
  2. %     
  3. %   This script-file demonstrates the minimization of the 
  4. %   the banana function:  
  5. %    
  6. %           f(x)= 100*(x(2)-x(1)^2)^2+(1-x(1))^2 
  7. %   It is called the banana function because of the way the  
  8. %   curvature bends around the origin (also called Rosenbrock's 
  9. %   function). It is notorious in optimization examples because 
  10. %   of the slow convergence with which most methods exhibit 
  11. %   when trying to solve this  problem. 
  12. %   This function has a unique minimum at the point X=[1,1] f(x)=0. 
  13. %   We demonstrate here a number of techniques for its minimization 
  14. %   starting at the point X=[-1.9,2]. 
  15.  
  16. echo off
  17. clc
  18. help bandemo
  19. disp(' Strike any key for a mesh plot of the banana function')
  20. xx = [-2:0.125:2]';
  21. yy = [-1:0.125:3]';
  22. [x,y]=meshgrid(xx',yy') ;
  23. meshd = 100.*(y-x.*x).^2 + (1-x).^2; 
  24. pause
  25. mesh(meshd)
  26.  
  27. disp('')
  28. disp(' Strike any key for a contour plot of the banana function')
  29. pause
  30. clf
  31. conts = exp(3:20);
  32. contour(xx,yy,meshd,conts,'w--') 
  33. xlabel('x1')
  34. ylabel('x2')
  35. title('Minimization of the Banana function')
  36. drawnow; % Draws current graph now 
  37. hold on
  38. plot(-1.9,2,'o')
  39. text(-1.9,2,'Start Point')
  40. plot(1,1,'o')
  41. text(1,1,'Solution')
  42. disp('Please wait - compiling optimization routines')
  43.  
  44. % test_long is a variable used for auto testing of this routine
  45. if ~exist('test_long')  test_long = 0; end
  46. if exist('method')~= 1 method = 7; end
  47. if ~length(method) method = 7; end
  48. l = 2;
  49.  
  50. while 1
  51.     if ~test_long
  52.         clc
  53.         disp(' ')
  54.         disp('   Choose any of the following methods to minimize the banana function')
  55.         disp('')        
  56.         disp('        UNCONSTRAINED:    1) Broyden-Fletcher-Golfarb-Shanno')
  57.         disp('                          2) Davidon-Fletcher-Powell')
  58.         disp('                          3) Steepest Descent')
  59.         disp('                          4) Simplex Search')
  60.         disp('         LEAST SQUARES:   5) Gauss-Newton')
  61.         disp('                          6) Levenberg-Marquardt ')
  62.         disp('')
  63.         disp('                          0) Quit')
  64.     end
  65.  
  66.     if test_long
  67.         if l>=2
  68.             method=method-1;
  69.             l = 0;
  70.         end
  71.     else
  72.         method=-1;
  73.     end
  74.     while  (method < 0) | (method > 6) 
  75.         method = [];
  76.         while ~length(method) 
  77.             method = input('Select a method number: ');
  78.         end
  79.     end
  80.     if (method == 0) 
  81.         hold off
  82.         return
  83.     end
  84.     OPTIONS=0;
  85.     if method==2, OPTIONS(6)=1;
  86.     elseif method==3, OPTIONS(6)=2;
  87.     elseif method==5, OPTIONS(5)=1;
  88.     end
  89.     if test_long
  90.         l = l + 1;
  91.     else
  92.         l = [];
  93.     end
  94.     if method~=4
  95.         disp('')
  96.         disp('    Choose any of the following line search methods')
  97.         disp('')
  98.         disp('           1) Mixed Polynomial Interpolation')
  99.         disp('           2) Cubic Interpolation')
  100.         disp('')
  101.         while ~length(l)
  102.             l = input('Select a line search number: ');
  103.         end
  104.         if l==2, OPTIONS(7)=1; end
  105.     end
  106.     if method~=4
  107.         OPTIONS(14)=200;
  108.     else
  109.         OPTIONS(14)=300;
  110.     end
  111.     x=[-1.9,2];
  112.     disp(' ')
  113.     if method<4
  114.         GRAD='[100*(4*x(1)^3-4*x(1)*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1)^2); banplot(x,OLDX)]';
  115.         f='100*(x(2)-x(1)^2)^2+(1-x(1))^2';
  116.         disp('[x, options] = fminu(f,x,OPTIONS,GRAD);')
  117.         [x, options] = fminu(f,x,OPTIONS,GRAD);
  118.     elseif method==4
  119.         f='[100*(x(2)-x(1)^2)^2+(1-x(1))^2; banplot2(x)]';
  120.         disp('[x, options]=fmins(f,x,OPTIONS);')
  121.         [x, options]=fmins(f,x,OPTIONS);
  122.     else
  123.         GRAD='[-20*x(1), -1; 10, 0; banplot(x,OLDX)]';
  124.         f='[10*(x(2)-x(1)^2),(1-x(1))]';
  125.         disp('[x,options]=leastsq(f,x,OPTIONS,GRAD);')
  126.         [x,options]=leastsq(f,x,OPTIONS,GRAD);
  127.     end
  128.     if test_long
  129.         if OPTIONS(8)-100*(method==3) > 1e-8, error('Optimization Toolbox in datdemo'), end
  130.     end
  131.  
  132.     disp(sprintf('\nValue of the function at the solution: %g', options(8)) ); 
  133.     disp(sprintf('Number of function evaluations: %g', options(10)) ); 
  134.     disp(sprintf('Number of gradient evaluations: %g\n\n', options(11)) ); 
  135.     disp('Strike any key for menu')
  136.     pause
  137.     clf
  138.     hold off
  139.         contour(xx,yy,meshd,conts,'w--') 
  140.     xlabel('x1')
  141.     ylabel('x2')
  142.     title('Minimization of the Banana function')
  143.     hold on
  144.     plot(-1.9,2,'o')
  145.     text(-1.9,2,'Start Point')
  146.     plot(1,1,'o')
  147.     text(1,1,'Solution')
  148. end
  149.