【问题标题】:MATLAB - least square curve fit for multiple independent variablesMATLAB - 多个自变量的最小二乘曲线拟合
【发布时间】:2017-02-13 21:52:05
【问题描述】:

我有以下功能:

我想用 MATLAB 函数 lsqcurvefit 得到 least squares method 的系数。

问题是,我不知道,当我的函数t 有多个自变量而不仅仅是一个时,是否可以使用该函数。所以,根据链接,我应该有多个 xData 向量 - 像这样:

lsqcurvefit(f, [1 1 1], nprocs, ndoms, nDOF, measuredVals)

你知道怎么做吗?


我的尝试

我试图像这样定义我的目标函数

f = @(c, x) c(1)*x(2).^(c(2)*x(1).^c(3)) + (c(4) + c(5)*x(1))/x(3);

并像这样使用lsqcurvefit

lsqcurvefit(f, [1 1 1], [ndoms nDOF nprocs], measuredVals),

但是有一个问题,measuredVals 是一个大小为 56x1 的向量,但我的“xData”是一个大小为 56x3 的矩阵,所以我收到了这个错误:

Index exceeds matrix dimensions.

Error in factorizatonKGlobRegr>@(c,x)c(1)*x(2).^(c(2)*x(1).^c(3))+(c(4)+c(5)*x(1))/x(3)

Error in lsqcurvefit (line 202)
            initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});

Caused by:
    Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.

但是当 $t: \mathbb{R}^3 \rightarrow \mathbb{R}$?


第二次尝试

我将目标函数稍微更改为

f = @(c, x) c(1)*x(:,2).^(c(2)*x(:,1).^c(3)) + (c(4) + c(5)*x(:,1))/x(:,3);,

但错误仍然存​​在。


我的数据

measuredVals = [
0.1647815
0.06300775
0.05769325
0.04803725
0.04290825
0.0405065
0.03807525
0.03487725
0.284112
0.13495675
0.12740075
0.11109725
0.105036
0.11022575
0.100587
0.09803775
0.48695475
0.30563525
0.30084925
0.283312
0.2745085
0.271998
0.27472625
0.27103925
0.89953925
0.68234025
0.6783635
0.65540225
0.64421475
0.64214725
0.63949875
0.623119
1.588605
1.37335275
1.36082075
1.35097375
1.34813125
1.34932025
1.3519095
1.34521625
2.820884
2.63251325
2.640659
2.6338805
2.636361
2.62748
2.6233345
2.63821
4.81472975
4.65116425
4.664892
4.64225625
4.6734825
4.63981675
4.635483
4.6280245];

n = 56;

ndoms = [];
for i=1:n
    ndoms = [ndoms; 288];
end

tmp = [
        375
        1029
        2187
        3993
        6591
        10125
        14739];
nDOF = [];
for i=1:7
    for j=1:8
        nDOF = [
            nDOF
            tmp(i)];
    end
end

nprocs = [];
for i=1:7
    nprocs = [nprocs; [1 2 3 4 6 8 12 24]'];
end

【问题讨论】:

  • 感觉这与您定义函数的方式有关。您可以在定义 f 的位置添加代码吗?
  • @GregPetersen 我已将目标函数添加到“我的尝试”部分

标签: least-squares matlab


【解决方案1】:

当我使用模拟的 c 和 x 输入测试您的目标函数时,它返回了一个与您的数据集大小相同的 NxN 矩阵。这不应该是这样。您应该只为每个数据集获取一个值,因此我期望一个 Nx1 矩阵。尝试将您的函数更新为此

f = @(c, x) c(1).*x(:,2).^(c(2).*x(:,1).^c(3)) + (c(4 ) + c(5)*x(:,1))./x(:,3)

我在除数上加了句号。

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 2015-02-14
    • 2012-07-14
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多