Bug in least squares fitting

조회 수: 2(최근 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
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개)

Community Treasure Hunt

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

Start Hunting!

Translated by