2

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

这是我的 x 和 y 矩阵

我只是计算拟合

适合(x,y,'power1')

我收到此错误:

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

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

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

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

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

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

EDIT3:我更改了链接。现在,在我的保管箱中,您会找到 x 和 y 的 .mat ...尝试将它们与 power1 匹配...:P 不起作用!为什么?我不明白...

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

4

1 回答 1

0

在 Octave中没有等价物fit,但是,我可以进行一些非线性曲线拟合(MATLAB 等价物是lsqcurvefit或者lsqnonlin优化工具箱):

% 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

在此处输入图像描述

于 2014-10-15T13:20:59.217 回答