数学建模社区-数学中国

标题: 【fittype问题】fittype接受的匿名函数中带有阶乘相,MATLAB提示错误 [打印本页]

作者: sinpen    时间: 2015-4-12 15:54
标题: 【fittype问题】fittype接受的匿名函数中带有阶乘相,MATLAB提示错误
先贴代码:
  1. t = 1:1:11;
  2. h = [0.0572 0.0572 0.0572 0.0714 .1143 .1429 .1571 .1429 .1143 .1000 .0714 ];
  3. plot(t, h, 'or'), hold on

  4. s = fitoptions('Method', 'LinearLeastSquares');

  5. ffun = fittype(@(lambda,n)(((lambda.^n)./factorial(n)).*exp(-lambda)),...
  6.     'Coefficients', {'lambda'},...
  7.     'Independent', 'n'...
  8.     );

  9. [cfun, gof, output] = fit(t', h', ffun,...
  10.     'StartPoint', [7]);

  11. plot(cfun,'b')
复制代码
这段script运行到 fittype 函数这里,会提示说传入的函数句柄为不可计算的MATLAB表达式:
  1. Error using fittype (line 356)
  2. Expression (((lambda.^n)./factorial(n)).*exp(-lambda)) is not a valid MATLAB expression, has non-scalar coefficients,
  3. or cannot be evaluated:
  4. Error while trying to evaluate FITTYPE function :

  5. N must be a matrix of non-negative integers.

  6. Error in poissonFit (line 7)
  7. ffun = fittype(@(lambda,n)(((lambda.^n)./factorial(n)).*exp(-lambda)),...
复制代码
但是,我直接将这个函数句柄拿出来用,是可以加算的。

作者: sinpen    时间: 2015-4-12 16:07
我深深地怀疑是不是那个factorial阶乘并不适合使用fittype?





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5