optimization data fitting error

조회 수: 1 (최근 30일)
Em
Em 2020년 5월 2일
댓글: Star Strider 2020년 5월 5일
I want find a model fit for a set of data. I'm just adapting the example code for my model, and there is just one line (my model equation) which is causing an error. Please could somebody tell me what about this line needs adjusting?
The error message:
"Error using lsqcurvefit (line 251)
Function value and YDATA sizes are not equal.
Error in curvefittingpractice2 (line 39)
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,t,y)"
Code:
Data = ...
[0.0000 5.8955
0.1000 3.5639
0.2000 2.5173
0.3000 1.9790
0.4000 1.8990
0.5000 1.3938
0.6000 1.1359
0.7000 1.0096
0.8000 1.0343
0.9000 0.8435
1.0000 0.6856
1.1000 0.6100
1.2000 0.5392
1.3000 0.3946
1.4000 0.3903
1.5000 0.5474
1.6000 0.3459
1.7000 0.1370
1.8000 0.2211
1.9000 0.1704
2.0000 0.2636];
plot(t,y,'ro')
title('Data points')
%F = @(x,xdata)x(1)*exp(-x(2)*xdata); --for this I get no error, for the
%following line I get an error message
F = @(x,xdata)x(1)*exp(x(2)/xdata).^x(3);
x0 = [1 1 1 0];
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,t,y)
hold on
plot(t,F(x,t))
hold off
  댓글 수: 3
Star Strider
Star Strider 2020년 5월 3일
... as I wrote two hours earlier.
Em
Em 2020년 5월 3일
Thanks to you both for your help! Unfortunately, I'm still getting error messages - just posted on the comment below.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2020년 5월 3일
For the second ‘F’, as written, returns a row vector rather than a column vector. The solution is to use element-wise division:
F = @(x,xdata)x(1)*exp(x(2)./xdata).^x(3);
(The first ‘F’ returns a column vector as written, so no problem.)
However the other problem is that the second ‘F’ produces an infinite result with the first value of ‘t’, since it is 0. One possible way to address that is to eliminate the first row of ‘Data’, however the fit is inferior to the first ‘F’ with the full data set.
I would go with the first ‘F’ and be done with it.
  댓글 수: 12
Em
Em 2020년 5월 5일
Thanks a lot for the time and effort you have put in to help me!
Star Strider
Star Strider 2020년 5월 5일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by