optimization data fitting error

1 view (last 30 days)
Em
Em on 2 May 2020
Commented: Star Strider on 5 May 2020
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 Comments
Star Strider
Star Strider on 3 May 2020
... as I wrote two hours earlier.
Em
Em on 3 May 2020
Thanks to you both for your help! Unfortunately, I'm still getting error messages - just posted on the comment below.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 3 May 2020
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 Comments
Em
Em on 5 May 2020
Thanks a lot for the time and effort you have put in to help me!
Star Strider
Star Strider on 5 May 2020
As always, my pleasure!

Sign in to comment.

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!