拟合幂律:" NaN由模型函数计算"

时间:2014-10-15 12:46:43

标签: matlab model-fitting

我尝试用matlab拟合幂律函数(y = ax ^ b)

Here are my x and y matrices

我只是用

来计算拟合
  

拟合(X,Y, 'POWER1')

我收到此错误:

  

使用fit> iFit时出错(第415行)   NaN由模型函数计算,拟合不能继续。   尝试使用或收紧系数的上限和下限。   适合误差(第109行)   [fitobj,goodness,output,convmsg] = iFit(xdatain,ydatain,fittypeobj,...

WHYYYY!?在我的x和y矩阵中没有0,没有任何东西可以返回我认为的NaN值,并且我可以毫无问题地计算逆关系拟合(y,x,'power1')。

感谢您的任何帮助/建议!!

编辑:(同样精确)Excel确实找到适合(x,y)的幂律!

EDIT2:代码,一旦x和y存储为变量:

[p_powerlaw,results_powerlaw] = fit(x,y,'power1');

EDIT3:我改变了链接。现在,在我的Dropbox中,你会发现x和y的.mat ...尝试用power1来装它们......:P不起作用!为什么?我不明白......

尝试将2个矩阵x和y的值粘贴到其他矩阵中(不是通过赋值,实际上是通过复制粘贴值)...没有问题的拟合......!

1 个答案:

答案 0 :(得分:0)

在Octave中没有等效的fit,但是,我可以做一些非线性曲线拟合(MATLAB等价物将是lsqcurvefitlsqnonlin优化工具箱):

% Define function handle for optimisation (equivalent to power1 fit in MATLAB)
f = @(p, x) p(1) * x.^p(2);

% Initial guess for power law coefficients
init = [1 1]';

% Run optimisation - data is contained in X matrix
[p,y_power,cvg,op] = nonlin_curvefit(f,init,X(:,1),X(:,2));

% Sort out the fitted data to be monotonically increasing
Y = zeros(size(X));
[Y(:,1),idx] = sort(X(:,1),1);
Y(:,2) = y_power(idx);

% Plot the data
plot(X(:,1),X(:,2),'x',Y(:,1),Y(:,2))

% Display the power law coefficients
clc
disp('y = a * x^b');
disp(['a = ' num2str(p(1))])
disp(['b = ' num2str(p(2))])

这给了我以下系数(与@Ander Biguri相同)和图表:

y = a * x^b
a = 13.416
b = 0.94642

enter image description here