Bug in least squares fitting

1 回表示 (過去 30 日間)
Catherine Royer
Catherine Royer 2016 年 11 月 16 日
コメント済み: Brendan Hamm 2016 年 11 月 16 日
I am trying to fit my data as follows and I get the error message below. Can anyone help? Thanks in advance. Cathy R
>> xdata = intvp(:,1)
xdata =
195
390
590
795
985
1195
1300
1410
1510
1600
1710
1885
2065
2270
2480
2695
2900
>> ydata = intvp(:,2)
ydata =
1.0139
0.9816
0.9606
0.8848
0.7149
0.4705
0.3130
0.2017
0.1224
0.0517
0.0210
0.0132
0.0041
0.0020
0.0172
-0.0020
0.0231
>> fun = @(x,xdata)x(1)*(1/(1 + (x(2)*exp(x(3)*xdata))))
fun =
@(x,xdata)x(1)*(1/(1+(x(2)*exp(x(3)*xdata))))
>> x0 = [1.0,0.01,0.0045]
x0 =
1.0000 0.0100 0.0045
>> x = lsqcurvefit(fun,x0,xdata,ydata) Error using lsqcurvefit (line 248) Function value and YDATA sizes are not equal.

採用された回答

Brendan Hamm
Brendan Hamm 2016 年 11 月 16 日
The issue is that you are trying to do an element-wise division but are not using an element-wise operator. You can see that:
fun(x0,xdata)
returns a row vector, which is not the same size as ydata.
Change the definition of fun and use the element-wise division operator ./ as opposed to /
fun = @(x,xdata) x(1)*(1./(1 + (x(2)*exp(x(3)*xdata))))
  2 件のコメント
Catherine Royer
Catherine Royer 2016 年 11 月 16 日
Thanks!
Brendan Hamm
Brendan Hamm 2016 年 11 月 16 日
in case you were wondering in A/B when the denominator is a vector or matrix, MATLAB is actually using an inverse (or pseudo inverse) so this calculation is treated by MATLAB as: A*inv(B). Similarly A\B is treated as inv(A)*B.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by