lsqcurvefit fitting not good

>> xdata = ...
[0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 0.055 0.06 0.065 0.07 0.075 0.08];
ydata = ...
[-1.5641 4.331 10.226 10.328 10.43 9.2075 7.9845 6.9538 5.9227 4.7857 3.6488 2.1603 0.67176 0.22867 -0.21442 -0.10787];
fun = @(x,xdata)x(1)+x(2).*sqrt(x(3)./((2.*pi.*x(4).*(xdata+x(5))).^3)).*exp(-x(3).*((x(4).*(xdata+x(5))-x(6)).^3)./(2.*x(4).*(xdata+x(5)).*x(6).^2));
x0 = [0,8,0.9,13,0.01,0.9];
x = lsqcurvefitlsqcurvefit(fun,x0,xdata,ydata)
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
lb = [];
ub = [];
times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')
it should mindestens so:
but in my Matlab it is so:
what should i do? and where am i wrong?

 Accepted Answer

Choose different values for ‘x0’.
I used these:
x0 = [-2.6; 17; 0.7; 7; 0.0001; -0.7; 0.7];
to get this result:
x =
-4.324990458427486
29.118149115959035
0.816104607520526
6.992507999448133
0.003331267566044
-0.774459180826721
0.700000000000000
and this plot:
#lsqcurvefit fitting not good.png
I began with a random vector, and used fminsearch to produce the new ‘x0’ vector. It gave a good fit after about 10 initial starts. A better approach would have been to use the ga funciton.

11 Comments

!! it is super!!
but how kan you find the good x0?
I tryed a lot, but all passt not good
Zuyu An
Zuyu An on 17 Oct 2019
Edited: Zuyu An on 17 Oct 2019
and it schould only 6 parameterx(1)-x(6), why is here 7?
sorry, l am beginer for this
Thank you!
I ran fminsearch (that uses a derivative-free algorithm) about 10 times using random values for ‘x0’ until it discovered an appropriate ‘x0’.
B = fminsearch(@(b)norm(ydata - fun(b,xdata)), x0)
(A more systematic approach would have been to use the ga algorithm, and I would have done that had I not just been lucky!)
There are 7 parameters because I miscounted! (Just delete the last one.)
I generated the smooth plot with:
xv = linspace(min(xdata), max(xdata), 250);
figure
plot(xdata, ydata, 'p')
hold on
plot(xv, fun(x,xv), '-r')
grid
The plot that I uploaded using the ‘Picture’ icon for some reason is not visible (that has occurred frequently in the last month or so), so I attached it as well.
Zuyu An
Zuyu An on 17 Oct 2019
Edited: Zuyu An on 17 Oct 2019
with 7 parameter x0 = [-2.6; 17; 0.7; 7; 0.0001; -0.7; 0.7];
output is
x =
-4.3249
29.1186
0.8161
6.9924
0.0033
-0.7744
0.7000
with 6 parameter x0 = [-2.6; 17; 0.7; 7; 0.0001; -0.7];
output is
x =
1.0e+03 *
-0.0043
0.0291
0.0008
1.4449
0.0000
-0.1600
it is very differernt(meist für 1441.9 und -160), kan you tell me that why?
thank you!
Note that they are actually very similar, and only appear to differ with respect to some of them that are inter-related in your function. Those combinations may not be unique, and they likely result in the same actual values. The function would likely have ignored the seventh parameter anyway, so my guess is that you ran the routine twice, and simply produced different results, similar to what might have occurred if you gave it the same ‘x0’ several different times. The estimated parameters may not be unique, and probably represent different ranges of the allowable parameter estimates that result in the least mean-squared error.
I am so sorry! I made a mistake by Problemdiscription(felht ein x(4)), I correct it already. kan you help me with it again? and show me the complete code, so I kan see how fminsearch work.
Thank you VERY VERY much!
With your new definition of ‘fun’, even using ga, I cannot fit your data to it.
I ran ga (unconstrained) several different times, comprising millions of ‘x0’ vectors, none of which resulted in a decent fit to your data. It is quite likely that the new version of ‘fun’ does not at all describe the process that created your data.
Use the version of ‘fun’ that you originally posted, and the associated parameters. Those work.
My fminsearch results were pure luck! To use the genetic algorithm (ga) function, see the documentation for it that I link to in this Comment.
The complete code that I used (using ga) is:
% xdata = [0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 0.055 0.06 0.065 0.07 0.075 0.08];
% ydata = [-1.5641 4.331 10.226 10.328 10.43 9.2075 7.9845 6.9538 5.9227 4.7857 3.6488 2.1603 0.67176 0.22867 -0.21442 -0.10787];
fun = @(x,xdata)x(1)+x(2).*sqrt(x(3)./((2.*pi.*x(4).*(xdata+x(5))).^3)).*exp(-x(3).*((x(4).*(xdata+x(5))-x(6)).^3)./(2.*x(4).*(xdata+x(5)).*x(6).^2));
ftns = @(x) norm(ydata-fun(x,xdata));
PopSz = 500;
Parms = 6;
opts = optimoptions('ga', 'PopulationSize',PopSz, 'InitialPopulationMatrix',randi(1E+4,PopSz,Parms)*1E-2, 'MaxGenerations',2E3, 'PlotFcn',@gaplotbestf, 'PlotInterval',1);
t0 = clock;
fprintf('\nStart Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t0)
[x,fval,exitflag,output] = ga(ftns, Parms, [],[],[],[],[],[],[],[],opts)
t1 = clock;
fprintf('\nStop Time: %4d-%02d-%02d %02d:%02d:%07.4f\n', t1)
GA_Time = etime(t1,t0)
fprintf('\nElapsed Time: %23.15E\t\t%02d:%02d:%02d.%03d\n', GA_Time, hmsdv(GA_Time))
fprintf(1,'\tParameters:\n')
for k1 = 1:length(x)
fprintf(1, '\t\tX(%d) = %8.5f\n', k1, x(k1))
end
xv = linspace(min(xdata), max(xdata), 250);
figure
plot(xdata, ydata, 'p')
hold on
plot(xv, fun(x,xv), '-r')
grid
The revised version of ‘fun’ just does not describe your data. The earlier version did, and quite well. You can use any other version of ‘fun’ without changing my code here otherwise, providing it has the same number of parameters. If it does not, change the ‘Parms’ assignment to reflect that. You must have the Global Optimization Toolbox to use my code. There are no other restrictions.
thank you so much, and i am so sorry my problem take you so much time, you already help me a lot ! (fogive me my poor englisch, i kan not say it better to thank you). Rest kann i selbest machen.
As always, my pleasure!
No worries about your English! Unfortunately, I have not used my college German in decades, so I would need extensive study to be as capable with it as you are in English.
I am sorry to ask you again, but my probleme seems still not solved. I submit a new question, Could you please help me?
I posted an Answer to your new Question.

Sign in to comment.

More Answers (1)

Alex Sha
Alex Sha on 18 Oct 2019
Edited: Alex Sha on 22 Oct 2019
whether fminsearch or lsqcurvefit in Matlab use local optimization algorithms, it is why the initial start values are so important, unfortunately, guessing good initial start values is nightmare for most of users, although GA toolbox in Matlab use global algorithm, but the effect of GA in Matlab is far below expectations。The follow results are calculated from one of other software by using global optimization, no need to guess initial start values:
Root of Mean Square Error (RMSE): 0.448475537821338
Sum of Squared Residual: 3.21808492838621
Correlation Coef. (R): 0.993866542618332
R-Square: 0.987770704536117
Adjusted R-Square: 0.98588927446475
Determination Coef. (DC): 0.987770704536117
Chi-Square: 0.0996829655505242
F-Statistic: 161.541718751279
Parameter Best Estimate
---------- -------------
x1 -4.32518017924354
x2 1.5161832530864
x3 0.816035356312747
x4 0.139454519659979
x5 0.00333139106331274
x6 -0.015446312666105

Products

Asked:

on 17 Oct 2019

Edited:

on 22 Oct 2019

Community Treasure Hunt

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

Start Hunting!