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
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!

请先登录,再进行评论。

更多回答(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