home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / FITDEMO.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.9 KB  |  86 lines

  1. %FITDEMO Nonlinear curve fit with simplex algorithm.
  2.  
  3. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  4.  
  5. clf
  6. echo on
  7. clc
  8. %    This example demonstrates fitting a nonlinear function to a
  9. %    set of data.  We'll use a function called FMINS that implements
  10. %    the Nelder-Mead simplex algorithm for minimizing a nonlinear
  11. %    function of several variables.
  12.  
  13. pause  % Press any key to continue.
  14. clc
  15. %    Consider the following data:
  16.  
  17. global Data
  18.  
  19. Data = ...
  20.   [ 0.0000    5.8955
  21.     0.1000    3.5639
  22.     0.2000    2.5173
  23.     0.3000    1.9790
  24.     0.4000    1.8990
  25.     0.5000    1.3938
  26.     0.6000    1.1359
  27.     0.7000    1.0096
  28.     0.8000    1.0343
  29.     0.9000    0.8435
  30.     1.0000    0.6856
  31.     1.1000    0.6100
  32.     1.2000    0.5392
  33.     1.3000    0.3946
  34.     1.4000    0.3903
  35.     1.5000    0.5474
  36.     1.6000    0.3459
  37.     1.7000    0.1370
  38.     1.8000    0.2211
  39.     1.9000    0.1704
  40.     2.0000    0.2636];
  41.  
  42. pause    % Strike any key to continue.
  43.  
  44. clc
  45. %    Let's plot this data.
  46.  
  47. t = Data(:,1);
  48. y = Data(:,2);
  49. axis([0 2 0 6])
  50. hold on
  51. plot(t,y,'co','EraseMode','none')
  52. title('Input data')
  53.  
  54. pause    % Strike any key to continue.
  55.  
  56. clc
  57. %    We would like to fit the function
  58. %
  59. %      y =  c(1)*exp(-lam(1)*t) + c(2)*exp(-lam(2)*t)
  60. %
  61. %    to the data.  This function has 2 linear parameters
  62. %    and 2 nonlinear parameters.  Here it is.
  63. type fitfun
  64.  
  65. %    To fit this function to the data, we write a function, called
  66. %    say FITFUN, that, given the nonlinear parameters lam and the data,
  67. %    returns the error in the fit.  We can then guess for initial
  68. %    estimates of the nonlinear parameters, and invoke FMINS.
  69.  
  70. pause    % Strike any key to start fitting.
  71.  
  72. global Plothandle
  73. Plothandle = plot(t,y,'y-','EraseMode','xor');
  74.  
  75. lam = [1 0]'; 
  76. trace = 0;
  77. tol = .1;
  78. lambda = fmins('fitfun',lam,[trace tol]);
  79.  
  80. hold off
  81. echo off
  82. s = ['lambda = fmins(''fitfun'',[1 0]'',[0 .1]);'];
  83. uicontrol('Units','normal','Position',[.2 .01 .1 .06],'String','Again','callback',s)
  84.  
  85. disp('End')
  86.