lsqcurvefit does not follow convergence criteria in summation.

I have a function that uses summation to fit an bi-exponential function. Because I have to retrieve other information from that fit, I cannot use the exp2-fit function unfortunately. As for the procedure over the 8 parts (n=8). The data is in t(xdata) and S(ydata). For each of the terms 1-8 the individual contribution (yfit_n) is calculated and then added up together to get the final results for the fit (yfit). The fucntion has 7 variables for the fitting in the vector x or x0 for the respective startin point (x(2) is not used at this stage).
FS_1=0.05;
FS_2=2.0264;
d=1E-4;
dy=1E-5;
n=1:8;
t=t_S_mathworks(:,1);
S=t_S_mathworks(:,2);
x0=rand(7,1);
fun=@(x,t) x(1)+x(3).*(FS_1+FS_2*sum(((-1.^n)./n.^2).*(1-cos(n*pi*dy/d))...
.*(x(4).*exp(-x(6)*(t).*n.^2.*pi^2./d^2)...
+x(5).*exp(-x(7)*(t).*n.^2.*pi^2./d^2)),2));
options.OptimalityTolerance = 1e-15;
[x,resnorm]=lsqcurvefit(fun,x0,t,S);
Because I got the flag that indicates that the convergence criterion is too low I wanted to change, but it was incorporated in the fit-options, I think.
After the fit has converged (but not giving a useful result) I checked for the convergence criterion and it wasn't replaced with the new one of 1E-15.
Optimization completed: The first-order optimality measure, 1.132517e-08,
is less than options.OptimalityTolerance = 1.000000e-06.
And when I check for the fit using
S_fitted =x(1)+x(3).*(FS_1+FS_2*sum(((-1.^n)./n.^2).*(1-cos(n*pi*dy/d))...
.*(x(4).*exp(-x(6)*(t).*n.^2.*pi^2./d^2)...
+x(5).*exp(-x(7)*(t).*n.^2.*pi^2./d^2)),2));
plot(t,S_fitted)
I see that only the first two data points gave a result other than the minium of data points. So it seems that the fitting itself did not happen. I inlcued the data to let you see of that error is reproducable. I hope that it is just a bening error I cannot seem locate right now.
All help is appreciated.
All the best,
Chris

3 Comments

There are multi-solutions, only x6 and x7 are fixed unchangeable:
1:
Sum Squared Error (SSE): 2.71889334509456E-6
Root of Mean Square Error (RMSE): 9.64949665997712E-5
Correlation Coef. (R): 0.993946167822174
R-Square: 0.987928984528385
Parameter Best Estimate
--------- -------------
x1 -0.691823638307674
x3 -0.0209093910879083
x4 0.0025318914735974
x5 0.403457679083398
x6 -3.60330173461531E-13
x7 4.6721687974993E-12
2:
Sum Squared Error (SSE): 2.71889334509476E-6
Root of Mean Square Error (RMSE): 9.64949665997748E-5
Correlation Coef. (R): 0.993946167822174
R-Square: 0.987928984528385
Parameter Best Estimate
--------- -------------
x1 -0.692685812890158
x3 -0.00366589896857348
x4 0.0144412918653729
x5 2.30122391388349
x6 -3.60330142737079E-13
x7 4.67216895648856E-12
Hi Alex,
thank you for your post. I can't seem to achieve these outputs. Are you using the code exactly as I posted it above, or did you make changes to it? I am aware of the co-dependece of the fitting values, I have to contrain a few of these to make physical sense.
What comand did you use to get the fitting parameters as you show it here?
All the best,
Chris
@Alex Sha has his own software - usually with extraordinary good results for fitting problems.

Sign in to comment.

 Accepted Answer

After the fit has converged (but not giving a useful result) I checked for the convergence criterion and it wasn't replaced with the new one of 1E-15.
Because you have not passed the options variable to lsqcurvefit
options.OptimalityTolerance = 1e-15;
[x,resnorm]=lsqcurvefit(fun,x0,t,S,[],[],options);

7 Comments

Hi Matt,
thank you for your prompt reply. I have now copied what you poseted here, but it seems that I still this for the <stopping criteria details>
Optimization stopped because the relative sum of squares (r) is changing
by less than options.FunctionTolerance = 1.000000e-06.
You haven't explained why that's a problem. You are free to lower the FunctionTolerance parameter if you wish.
Hi Matt,
thank you for your reply. The issue that I have right now, that if I try to run it, it does either converge immediatly, by giving x that is equal to x0, or it does not converge at all. (I let run over night and still hasn't converged). So I do not know what is going on here. The fact that it did converge with a good fitting result in post that Alex did above makes me think that something strange is going on when I do it. Maybe I am just missing a very basic step or element. Just to be totally clear in what I am doing: Do load the t-S-data into my workspace (there is nothing else in there) and only copy-paste the code above into the Comand Window, nothing else added. If you do that, does the fit converge?
Yes, it does converge. As you can see below. The exitflag is 1.
load("t_S_mathworks")
FS_1=0.05;
FS_2=2.0264;
d=1E-4;
dy=1E-5;
n=1:8;
t=t_S_mathworks(:,1);
S=t_S_mathworks(:,2);
x0=rand(7,1);
fun=@(x,t) x(1)+x(3).*(FS_1+FS_2*sum(((-1.^n)./n.^2).*(1-cos(n*pi*dy/d))...
.*(x(4).*exp(-x(6)*(t).*n.^2.*pi^2./d^2)...
+x(5).*exp(-x(7)*(t).*n.^2.*pi^2./d^2)),2));
options.OptimalityTolerance = 1e-15;
[x,resnorm,~,exitflag]=lsqcurvefit(fun,x0,t,S,[],[],options);
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
exitflag
exitflag = 1
Hi Matt,
thank you so much for your patience on this problem.
I do use the fit as above and does, however, when I then plot the fit versus the data using:
S_fitted =x(1)+x(3).*(FS_1+FS_2*sum(((-1.^n)./n.^2).*(1-cos(n*pi*dy/d))...
.*(x(4).*exp(-x(6)*(t).*n.^2.*pi^2./d^2)...
+x(5).*exp(-x(7)*(t).*n.^2.*pi^2./d^2)),2));
plot(t,S,t,S_fitted)
it is evident that the fitting did not work. I found out that it just outputs in the input values, so doesn't really do a fit at all. (t_S_fit_plot.fig).
Then of course I thought that maybe there is something wrong with the fit function itself (typos etc.), but after seeing the results from Alex (from above) and using their values, my eqn provides an adequate fit. So I am still very much puzzeld about how he did find these values for x. Is it a problem for the fit function that x(2) of x is not used at all? (I will need that variable later on, but not in at stage of the fitting process)
All the best,
Chris
I imagine it is because you are using a random initial guess, x0. If you initialize with Alex's solution, or values close to it, I would bet that you will get a more reasonable looking fit.
In any case, it is unrelated to your posted question. Your question was why the optimization was not obeying your options settings, and that has been solved.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Asked:

on 27 May 2023

Commented:

on 30 May 2023

Community Treasure Hunt

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

Start Hunting!