Line search algorithm: nonlinear least squares method for fitting experimental data to a model
Mostrar comentarios más antiguos
Hello, I have tried to fit my experimental data with a nonlinear model. But the error between the obtained curves is very high. I tried to solve it with lsqnonlin and lsqcurvefit but the exitflag that I received is 1, so i can not get better results with this model. I find then that in some articles, they used line search method. I am not familiar with these types of codes. Can you, please, help me adapting my code to line search algorithm? Otherwise, any ideas to get better fit.
Thank you in advance.
clc
close all
clear all
figure;
A=importdata('1.xlsx');
xdata = ...
[A(:,1) A(:,1) A(:,1)];
ydata = ...
[A(:,2) (A(:,3)) (A(:,4))];
%%
% Create the model.
fun = @(x,xdata)[(x(1)*exp(-(x(2)*(357.15-x(4)))/(x(3)+357.15-x(4))))...
./(1.+(xdata(:,1).*(x(1)*exp(-(x(2)*(357.15-x(4)))./(x(3)+357.15-x(4))))./x(6)).^(1-x(5))),...
(x(1)*exp(-(x(2)*(367.15-x(4)))./(x(3)+367.15-x(4))))...
./(1.+(xdata(:,1).*(x(1)*exp(-(x(2)*(367.15-x(4)))./(x(3)+367.15-x(4))))./x(6)).^(1-x(5))),...
(x(1)*exp(-(x(2)*(377.15-x(4)))./(x(3)+377.15-x(4))))...
./(1.+(xdata(:,1).*(x(1)*exp(-(x(2)*(377.15-x(4)))./(x(3)+377.15-x(4))))./x(6)).^(1-x(5)))];
%%
% Fit the model using the starting point
% x0 = [1e5;10;10;340;0.2;500];
x0 = [1e4;2;2;300;0.3;6];
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','Display','iter','TolX', 1e-6, 'TolFun', 1e-6, 'MaxFunEvals', 40000, 'MaxIter', 400);
lb = [1 1 1 1 0 1];
ub = [1e17 100 100 1000 1 1e9];
x = lsqcurvefit(fun,x0,[xdata(:,1) xdata(:,1) xdata(:,1)],[ydata(:,1) ydata(:,2) ydata(:,3)],lb,ub,options)
%%
% Plot the data and the fitted curve.
loglog(xdata(:,1),fun(x,xdata(:,1)))
hold on
loglog(xdata(:,1),ydata(:,1),'ko')
hold on
loglog(xdata(:,1),ydata(:,2),'ro')
hold on
loglog(xdata(:,1),ydata(:,3),'go')
legend('Fitted1','Fitted2','Fitted3','Data1','Data2','Data3')
title('Data and Fitted Curve')
8 comentarios
Torsten
el 5 de Abr. de 2019
I guarantee that if lsqcurvefit does not succeed fitting your model, you won't succeed with a self-written line search code, either.
Kaouthar Kaouthar
el 5 de Abr. de 2019
Kaouthar Kaouthar
el 5 de Abr. de 2019
Torsten
el 5 de Abr. de 2019
Can't you choose the values from the article ?
Kaouthar Kaouthar
el 5 de Abr. de 2019
Torsten
el 5 de Abr. de 2019
Then you should first play around with the parameters.
Without good initial guesses, your fitting procedure won't be successful (independent of the method you try).
Kaouthar Kaouthar
el 5 de Abr. de 2019
Respuesta aceptada
Más respuestas (2)
Alex Sha
el 28 de Abr. de 2019
1 voto
Please upload your data,I may try.
1 comentario
Kaouthar Kaouthar
el 4 de Mayo de 2019
John D'Errico
el 5 de Mayo de 2019
Editada: John D'Errico
el 5 de Mayo de 2019
A bad idea: to write your own code to implement a numerical method, because you cannot make professionally written code to do the same. Writing your own line search code is exactly that - a really bad idea.
Instead, learn why it is that your model fit has problems.
xdata = [A(:,1);A(:,1);A(:,1)];
ydata = [A(:,2);A(:,3);A(:,4)];
fun = @(x,xdata) (x(1)*exp(-(x(2)*(357.15-x(4)))/(x(3)+357.15-x(4))))...
./(1.+(xdata(:,1).*(x(1)*exp(-(x(2)*(357.15-x(4)))./(x(3)+357.15-x(4))))./x(6)).^(1-x(5)));
lb = [100 1 1 1 0 1];
ub = [5e5 100 100 1000 1 1000];
x0 = [1e4;2;2;300;0.3;6];
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','Display','iter','TolX', 1e-6, 'TolFun', 1e-6, 'MaxFunEvals', 40000, 'MaxIter', 400);
lb = [1 1 1 1 0 1];
ub = [1e17 100 100 1000 1 1e9];
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
x
x =
9651.15786422703
5.32989985066721
1.96617535975919
289.839867479332
0.325125105721047
15.7701020874321
Now, what does the data plot suggest?
loglog(xdata,ydata,'ro',xdata,fun(x,xdata),'b*')
grid on

Ok, we see that the curve does not fit well at the bottom end. This stems from two reasons. your fit was done in terms of a sum of squares, but the plot is show on loglog axes. Down at the bottom end, errors in those points are virtually irrelevant when you compute a straight sum of squares. They are tiny numbers compared to the rest, and the algorithm does not "care" since the errors down there are tiny.
As bad, you are trying to fit all three curves with the same set of parameters. They are very clearly different curves. Wanting to use the same parameters for all three curves seems a bit silly, because the variation between curves is quite large.
I'd suggest that either your model simply does not fit that data, or your starting values for the model are terribly poor. But using a different search method is not the way to fix this problem.
Essentially, the question I would ask is if this model actually means something to you in context? Do those parameters make physical sense? Did you derive this model from first physical principles, or did you just pick some terms that might give the curve some flexibility? Lets see, maybe I'll add an exponent here? So, maybe you started with something that makes some vague physical sense in context, but then you started adding terms and parameters?
This is all important, because what you intend to do with that model will matter. Because each of those three curves are fundamentally different in shape from each other, even while they all have distinct similarities too. So the question strongly becomes, why are you trying to fit all three of those curves with one set of parameters, when your model does not even fit any of them well? Thus a simple set of interpolating splines will do very nicely? Or a simple cubic least squares spline could be used to fit each curve, and nail down the behavior of each of those curves.
For example:
xdata = A(:,1);
ydata = [A(:,2) (A(:,3)) (A(:,4))];
loglog(xdata,ydata,'ro')
hold on
grid on
loglog(xdata,exp(fnval(sp1,log(xdata))),'b-')
loglog(xdata,exp(fnval(sp2,log(xdata))),'g-')
loglog(xdata,exp(fnval(sp3,log(xdata))),'k-')

Now, if the various parameters in your model actually have some meaning in context about the shape of that curve, you could now try to extracct them from the shape of those splines too. And you would have far more success in the process.
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


