optimization data fitting error
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!