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

  1. %DATDEMO Nonlinear data fitting demonstration.
  2. echo on;  clc
  3. %       This example demonstrates fitting a nonlinear function to a
  4. %    set of data.  It is used to demonstrate many of the different
  5. %       methods which can be used in the OPTIMIZATION TOOLBOX.
  6.  
  7. %    Consider the following data:
  8.  
  9. pause    % Strike any key to continue.
  10.  
  11. Data = ...
  12. [ 0.0000    5.8955
  13. 0.1000    3.5639
  14. 0.2000    2.5173
  15. 0.3000    1.9790
  16. 0.4000    1.8990
  17. 0.5000    1.3938
  18. 0.6000    1.1359
  19. 0.7000    1.0096
  20. 0.8000    1.0343
  21. 0.9000    0.8435
  22. 1.0000    0.6856
  23. 1.1000    0.6100
  24. 1.2000    0.5392
  25. 1.3000    0.3946
  26. 1.4000    0.3903
  27. 1.5000    0.5474
  28. 1.6000    0.3459
  29. 1.7000    0.1370
  30. 1.8000    0.2211
  31. 1.9000    0.1704
  32. 2.0000    0.2636];
  33.  
  34. %    Let's plot this data.
  35.  
  36. pause    % Strike any key for plot.
  37. t = Data(:,1);
  38. y = Data(:,2);
  39. clf
  40. plot(t,y,'o'), title('Input data'), pause
  41. clc
  42. %    We would like to fit the function
  43. %
  44. %      y =  c(1)*exp(-lam(1)*t) + c(2)*exp(-lam(2)*t)
  45. %
  46. %    to the data.  This function has 2 linear parameters
  47. %    and 2 nonlinear parameters.
  48.  
  49. echo off
  50. disp('Please Wait - Compiling Optimization Routines')
  51.  
  52. % test_long is a variable used for auto testing of this routine
  53. if ~exist('test_long')  test_long = 0; end
  54. if exist('method')~=1 method = 8; end
  55. if ~length(method) method = 8; end
  56. l = 2; 
  57.  
  58. while 1
  59.     lam = [1 0]'; 
  60.     if ~test_long
  61.         clc
  62.         disp(' ')
  63.         disp('   Choose any of the following methods to perform the data fit')
  64.         disp('')        
  65.         disp('        UNCONSTRAINED:    1) Broyden-Fletcher-Golfarb-Shanno')
  66.         disp('                          2) Davidon-Fletcher-Powell')
  67.         disp('                          3) Steepest Descent')
  68.         disp('                          4) Simplex Search')
  69.         disp('         LEAST SQUARES:   5) Gauss-Newton  ')
  70.         disp('                          6) Levenberg-Marquardt ')
  71.         disp('         MINIMAX          7) Seq. Quadratic Progr.')
  72.         disp('')
  73.         disp('                          0) Quit')
  74.         disp('')
  75.         disp('Note: Options 1:6 perform a least squares fit')
  76.         disp('      Option 7 minimizes the worst case error')
  77.         disp('      Gauss-Newton is the fastest method')
  78.     end
  79.  
  80.     
  81.     if test_long 
  82.         if l>=2 
  83.             method=method-1;
  84.             l = 0;
  85.         end
  86.     else 
  87.         method=-1; 
  88.     end
  89.     while (method<0 | method>7)
  90.         method = [];
  91.         while ~length(method)
  92.             method = input('Select a method number: ');
  93.         end
  94.     end
  95.     if (method == 0) 
  96.         return
  97.     end
  98.     OPTIONS=0;
  99.     if method==2, OPTIONS(6)=1;
  100.     elseif method==3, OPTIONS(6)=2;
  101.     elseif method==4, OPTIONS(5)=1;
  102.     elseif method==5, OPTIONS(5)=1;
  103.     end
  104.     if test_long
  105.         l = l + 1;  
  106.     else 
  107.         l = [];
  108.     end
  109.     if method~=4&method~=7
  110.         disp('')
  111.         disp('    Choose any of the following line search methods')
  112.         disp('')
  113.         disp('           1) Mixed Polynomial Interpolation')
  114.         disp('           2) Cubic Interpolation')
  115.         disp('')
  116.  
  117.         while ~length(l)
  118.             l = input('Select a line search number: ');
  119.         end
  120.         if l==2, OPTIONS(7)=1; end
  121.     end
  122.  
  123.     disp('')
  124.     OPTIONS(2)=1e-3;
  125.     t0=clock;
  126.     if method==5|method==6
  127.         disp('[lam,OPTIONS]=leastsq(''fitfun2'', lam,OPTIONS,[],Data);')
  128.         [lam,OPTIONS]=leastsq('fitfun2', lam,OPTIONS,[],Data);
  129.     elseif method==4
  130.         disp('[lam,OPTIONS]=fmins(''norm(fitfun2(x,P1))'',lam, OPTIONS,[],Data); ')
  131.         [lam,OPTIONS]=fmins('norm(fitfun2(x,P1))',lam, OPTIONS,[],Data); 
  132.     elseif method~=7;
  133.         disp('[lam,OPTIONS]=fminu(''norm(fitfun2(x,P1))'',lam,OPTIONS,[],Data);')
  134.         [lam,OPTIONS]=fminu('norm(fitfun2(x,P1))',lam,OPTIONS,[],Data);
  135.     else
  136.         OPTIONS(15)=length(t);
  137.         disp('[lam,OPTIONS]=minimax(''f=fitfun2(x,P1); g=[];'', lam, OPTIONSR,[],[],[],Data);')
  138.         [lam,OPTIONS]=minimax('f=fitfun2(x,P1); g=[];', lam, OPTIONS,[],[],[],Data);
  139.     end
  140.     if test_long
  141.         if (method<=4) OPTIONS(8) = OPTIONS(8).^2; end
  142.         if OPTIONS(8)-0.03*(method==7)-(method==3) > 0.15, error('Optimization Toolbox in datdemo'), end
  143.     end
  144.     execution_time=etime(clock, t0)
  145.     disp('Strike any key for menu')
  146.     pause
  147. end
  148.