【问题标题】:Fitting a power law: "NaN computed by model function"拟合幂律:“模型函数计算的 NaN”
【发布时间】:2014-12-10 13:22:59
【问题描述】:

我尝试用 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, ...

哇哇哇!?我的 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 的值复制粘贴到其他矩阵中(不是通过分配,实际上是通过复制粘贴值)...拟合没有问题..!

【问题讨论】:

  • 我认为问题不在于你有或没有0,而是拟合算法是否可以解决。不幸的是,并非所有东西都可以安装:(。但是,我不确定是否是这种情况。
  • 谢谢安德斯。但只是愚蠢地想,当我绘制数据时,没有异常值,非常顺利。和 EXCEL 找到一个合适的!!!
  • mmmm 我执行了它,我得到了结果... a = 13.42 b = 0.9464
  • 在 Matlab 中??? fit(x,y,'power1') 与我的 x 和 y???
  • mm 确保你首先做的是正确的......发布你的整个代码(应该是大约 3-4 行)

标签: matlab model-fitting


【解决方案1】:

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

【讨论】:

  • 嗨!谢谢你的回答。但我怀疑是不是这样,因为如果我尝试以相反的方式拟合幂函数(“fit(y,x,'power1')”),就没有问题。无论如何,您不必对数据进行排序以进行拟合。 .. :(
  • 好的,我会删除我的答案。
  • 不要!答案总是很重要... :)
  • 编辑了我的答案以表明可以得到适合数据的幂律。
  • 嘿!再次感谢。但是你确定你在这里拟合幂律吗?幂律函数(带有指数表达式)更像 y = a * exp(b * ln(x)) 而不是 y = a * exp (b*x)
猜你喜欢
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多