Fitting an exponential using nlinfit

5 vues (au cours des 30 derniers jours)
Aindrila Saha
Aindrila Saha le 10 Juil 2020
Commenté : John D'Errico le 10 Juil 2020
I am trying to fit the attached data to an exponential decay A*exp(k.x) to the following data between stepFit(x)=5000 to 6000. I am having trouble getting the initials.
start = 5000;
ending = 6000;
points = ending-start+1;
avgSPResp = Mean(6,:);
avgSPResp2 = avgSPResp(start:ending);
stepFit = step(start:ending);
expFit = @(beta, stepFit) (beta(1).* exp(beta(2).*stepFit));
initials = [60, -0.1];
coeffs = nlinfit(stepFit, avgSPResp2, expFit, initials);

Réponses (1)

John D'Errico
John D'Errico le 10 Juil 2020
Most of the code you posted is meaningless, since you save only the variables
whos
Name Size Bytes Class Attributes
avgSPResp2 1x1001 8008 double
stepFit 1x1001 8008 double
But it looks like you want to fit an exponential model to this data.
PLOT YOUR DATA. ALWAYS. Don't just throw it at a fitting tool, and expect something meaningful to come out. THINK ABOUT WHAT YOU SEE. This should be the number 1 rule for people to follow. If you don't, then expect someone like me to be forced to remind you that you did not bother to think about what you have and what you are doing. Sorry, but true.
plot(stepFit,avgSPResp2,'.')
Next, Look at the plot. What model are you wanting to fit to that data? Yes, an exponential model. However, consider what shape the exponential model you have chosen has?
An exponential decay model looks like this:
fplot(@(x) exp(-x),[0,5])
You can want it to do anything you want, but that is simple mathematics. And mathematics is cruel, rarely doing something just because you want it to do so. If you change the constants in beta, thus:
(beta(1).* exp(beta(2).*stepFit))
you can change how fast it drops off, or you can change where it starts from. No matter what, that is the fundamental shaoe you will get.
Note that as the independent variable (stepfit) goes to infinity, the exponential will decrease to zero. It will not be asymptotic to some other value. Wishing won't make it so. And no curve fitting routine in the universe will make it do any better.
So what you need to understand is that the is not a problem of nlinfit, or how to find good starting values. This is a fundamental problem that the model you want to use is never going to fit that curve. NEVER. It will not even fit poorly. The model and the data are inconsistent.
Could you change the model? Certainly. But that model and that data are inconsistent.
  2 commentaires
Aindrila Saha
Aindrila Saha le 10 Juil 2020
I toggled a bit with the limits and then screened the data to fit an exponential between x=5005 and 5090. With initials 100 and -2, this is what I'm getting: (red: data, blue: fit)
Still not a fit, but definitely getting there. However, I can't get the fit any closer.
(I have to fit the exponential function to the decaying phase of the data, cannot change the function but can change the range of data to fit)
John D'Errico
John D'Errico le 10 Juil 2020
No. It is not "getting there".
You are not listening to me. You WANT to use an exponential decay. But your data does not have the shape of an exponential decay. Just wanting a square peg to fit into a round hole will not be enough.
You have clearly been told to use an exponential decay model here. But sadly, either you misunderstood what your teacher, boss, instructor, mentor, advisor, (whatever) told you to use, or they were not clear about what you had to do, or maybe they just never looked at it themselves.
Explain to them clearly that this is not possible to achieve. If you need, show them what I wrote.
Maybe, just maybe, you could suggest a different model. But reducing the data region to only a few points is NOT a meaningful thing to do, s then you could fit any model you wanted by choosing the correct 2 or 3 data points that made you happy.
So go back, and talk to whoever assigned this to you. And tell them to be more clear about what you need to do, but also insist they look more carefully at the problem.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by