设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1137|回复: 0

曲线拟合

[复制链接]

8

主题

80

金钱

115

积分

入门用户

发表于 2018-9-13 15:30:57 | 显示全部楼层 |阅读模式
  1. function [x,OPTIONS,CostFunction,JACOB] = curvefit(FUN,x0,XDATA,YDATA,OPTIONS,GRADFUN,varargin)
  2. %曲线拟合
  3. %   C=CURVEFIT('拟合模型',参数C初值,X数据,Y数据)
  4. %求参数C使得 sum {(FUN(C,X数据)-Y数据).^2}最小化
  5. %例
  6. %   x | 0.1  0.2  0.15 0.0  -0.2 0.3
  7. %   --|------------------------------
  8. %   y | 0.95 0.84 0.86 1.06 1.50 0.72
  9. %拟合 y=a*exp(b*x).先写M-函数fitfun2.m
  10. %                   function   f=fitfun2(c,x)
  11. %                   f=c(1)*exp(c(2)*x);
  12. %  解法
  13. %  x=[0.1,0.2,0.15,0,-0.2,0.3];
  14. %  y=[0.95,0.84,0.86,1.06,1.50,0.72];
  15. %  c=curvefit('fitfun2',[1,1]',x,y);
  16. %  a=c(1),b=c(2)
  17. %  f='a*exp(b*xi)';
  18. %  xi=-0.2:0.01:0.3;
  19. %  yi=eval(f);
  20. %  plot(x,y,'o',xi,yi,'k')
  21. %  title('CURVEFIT');   

  22. %CURVEFIT Solves non-linear least squares problems.
  23. %   CURVEFIT solves problems of the form:
  24. %   min  sum {(FUN(X,XDATA)-YDATA).^2}  where FUN, XDATA and YDATA are
  25. %    X                                  matrices.   
  26. %
  27. %   X=CURVEFIT('FUN',X0,XDATA,YDATA) starts at X0 and finds
  28. %   coefficients X to best fit the nonlinear function FUN(X,XDATA)
  29. %   to the data YDATA (in the least-squares sense). FUN is an M-file
  30. %   that computes a function of X and XDATA and returns a matrix of
  31. %   the objective function values: F=FUN(X,XDATA).
  32. %   NOTE: YDATA must be the same size as the matrix F returned by FUN.
  33. %
  34. %   X=CURVEFIT('FUN',X0,XDATA,YDATA,OPTIONS) allows a vector of optional
  35. %   parameters to be defined. OPTIONS(2) is a measure of the precision
  36. %   required for the values of X at the solution. OPTIONS(3) is a measure
  37. %   of the precision required of the objective function at the solution.
  38. %   See HELP FOPTIONS.
  39. %
  40. %   X=CURVEFIT('FUN',X0,XDATA,YDATA,OPTIONS,'GRADFUN') enables a
  41. %   function 'GRADFUN' to be entered which returns the partial derivatives
  42. %   of the functions, dF/dX, (stored in columns) at the point X:
  43. %   gf = GRADFUN(X,XDATA).
  44. %
  45. %   X=CURVEFIT('FUN',X,XDATA,YDATA,OPTIONS,'GRADFUN',P1,P2,..) passes the
  46. %   problem-dependent parameters P1,P2,... directly to the functions FUN
  47. %   and GRADFUN: FUN(X,XDATA,P1,P2,...) and GRADFUN(X,XDATA,P1,P2,...).  
  48. %   Pass empty matrices for OPTIONS and 'GRADFUN' to use the default values.
  49. %
  50. %   [X,OPTIONS,F,J]=CURVEFIT('FUN',X0,XDATA,YDATA,...) returns, F,
  51. %   the value of FUN(X,XDATA)-YDATA at the solution X, and J the Jacobian
  52. %   of the function FUN at the solution.
  53. %
  54. %   FUN must be an M-file and not an inline object or expression. Use LEASTSQ
  55. %   instead on inline objects or expressions.

  56. %   Copyright (c) 1990-98 by The MathWorks, Inc.
  57. %   $Revision: 1.6 $  $Date: 1997/11/29 01:23:03 $
  58. %   Mary Ann Branch 8-22-96.

  59. %   The default algorithm is the Levenberg-Marquardt method with a
  60. %   mixed quadratic and cubic line search procedure.  A Gauss-Newton
  61. %   method is selected by setting  OPTIONS(5)=1.
  62. %

  63. % ------------Initialization----------------

  64. if nargin < 4, error('curvefit requires four input arguments');end
  65. if nargin < 5, OPTIONS=[]; end
  66. if nargin < 6, GRADFUN=[]; end

  67. % Need extra argname for XDATA
  68. curvefitarg = 1;
  69. lenVarIn = length(varargin);

  70. % Convert to inline function as needed.
  71. if isempty(FUN)
  72.    error('FUN must be a function name.');
  73. elseif isa(FUN,'inline')
  74.    error('FUN must be a function name, not an inline object. Use LEASTSQ instead.');
  75. elseif isstr(FUN)
  76.    % Make sure FUN isn't an expression via fcnchk
  77.    fun0 = fcnchk(FUN);
  78.    if isa(fun0,'inline')
  79.       error('FUN must be a function name, not a string expression. Use LEASTSQ instead.');
  80.    else
  81.       % Momentarily turn FUN into an inline to get at the argnames and formula
  82.       fun1 = inline(FUN,lenVarIn+1);
  83.       fun1args = argnames(fun1);
  84.       % Compute the inline function that is the difference with YDATA      
  85.       arglist = sprintf('%s(', formula(fun1));
  86.       for k = 1:(nargin(fun1)-1);
  87.          arglist = sprintf('%s%s,', arglist, deblank(fun1args{k,:}));
  88.       end
  89.       arglist = sprintf('%s%s)',arglist, deblank(fun1args{nargin(fun1),:}));
  90.       funfcn = inline([arglist, ...
  91.             '-P',int2str(lenVarIn+2)],lenVarIn+2);
  92.    end
  93. else
  94.    error('FUN is an unrecognized data type.')
  95. end

  96. if ~isempty(GRADFUN)
  97.    if isa(GRADFUN,'inline')
  98.       error('GRADFUN must be a function name and not an inline object.');
  99.    elseif isstr(GRADFUN)
  100.       % Make sure FUN isn't an expression via fcnchk
  101.       fun0 = fcnchk(GRADFUN);
  102.       if isa(fun0,'inline')
  103.          error('GRADFUN must be a function name, not a string expression.');
  104.       else
  105.          % Momentarily turn GRADFUN into an inline to get at the argnames and formula
  106.          fun1 = inline(GRADFUN,lenVarIn+1);
  107.          fun1args = argnames(fun1);
  108.          % Compute the inline function that is the difference with YDATA
  109.          arglist = sprintf('%s(', formula(fun1));
  110.          for k = 1:(nargin(fun1)-1);
  111.             arglist = sprintf('%s%s,', arglist, deblank(fun1args{k,:}));
  112.          end
  113.          arglist = sprintf('%s%s)',arglist, deblank(fun1args{nargin(fun1),:}));
  114.          gradfcn = inline(arglist,lenVarIn+2);
  115.       end   
  116.    else
  117.       error('GRADFUN is an unrecognized data type.')
  118.    end
  119. else
  120.    gradfcn = [];
  121. end


  122. % Check the size of f and YDATA
  123. try
  124.    f = feval(FUN,x0,XDATA,varargin{:});
  125. catch
  126.    error(['Error in evaluating user supplied function: ',FUN])
  127. end

  128. if ~isequal(size(f), size(YDATA))
  129.    error('Function value and YDATA sizes are incommensurate.')
  130. end

  131. % The order YDATA, XDATA is relevant: match P2 to YDATA
  132. [x,OPTIONS,CostFunction,JACOB] = ...
  133.    nlsqold(funfcn,x0,OPTIONS,gradfcn,XDATA,varargin{:},YDATA);

  134. %--end of curvefit--

复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表