Double exponential fits, but single curves away from the data- why?

When I try single exponential fit (red line in attached pic) to my data (the part in blue on the attached graph), it seems to curve way from the values, and clearly does not fit. I used:
f=fit(newx(10000:11000), yy2(10000:11000), 'a*exp(-(1/b)*x)');
plot(f, newx(10000:11000), yy2(10000:11000))
I tried to filter and smooth the signal down to a single line, and I also tried cftool. When I use double exponential, it works, but for my calculations, I need a single exponential fit to get the b-coeff. Is there any reason why single exponential just does not work with this trace?
-----edit
I attached the whole trace, and I want to fit to points 10000:10500

1 Comment

Because the data aren't exponential, maybe, would be the firstmost that comes to mind.
Need to attach a dataset if expect somebody to try to do anything other than look at a picture.

Sign in to comment.

 Accepted Answer

very steep power laws can be notoriously difficult to fit, since small changes in x lead to big changes in y, it makes it hard on the fitting algorithms.
A trick you might try is (in your case) take the natural log of the data and then fit it to the natural log of your fit equation...
ln(a e^(-(1/b)*x)) = ln(a) - x/b;
so that now your fit problem has been reduced to a linear fit y = Mx+B, with M=-1/b and B = ln(a).
Also remember in matlab that the function for ln is log() which drives me crazy.

9 Comments

Hi J. Webster
Thanks for pitching in. I tried your suggestion, but the fit is still wrong.
IJ
You provided y data; what's x? Your plot shows values <1 but not what the interval really is.
Looking at your plot I used 0.5 and 0.0001 as dx altho for plotting it really makes no difference, the magnitude of the variables in scaling can make "issues".
But, as I suspected and commented in first response, much (if not all) of the reason for the poor fit is the data simply are not exponential in the range of interest. To see this, try
figure
yy=y(10000;10500);
semilogx(yy)
figure
semilogy(abs(yy))
ylim([0.06 0.2]])
You'll see both are still quite nonlinear; hence the single exponential model in either variable won't fit well at all.
Your data don't match the model you've tried to force upon them.
Don't think you did it quite right.
  1. Take the log of the data (just the y values, not the x values) you are trying to fit to. If you plot it, then it should be line-like. If it isn't your data isn't really exponential.
  2. fit that data to a line... Mx+B;
  3. Once you get M and B, you can calculate a and b fromb = -1/M;a = exp(a);
Something like
x = 1:.01:10; %
y = 4.234.*exp(-x./.10234); % represents the data you want to fit
plot(x,y); %very steep
logy = log(y); %take log (natural log) of your data
%(if not a line here, then your data isn't exponential)
plot(x,logy); %nice line!
myfit = polyfit(x,logy,1) %fit a line to that (first order polynomial...
%y = Mx+B
%get back original values
%myfit(1) is M
%myfit(2) is B
b = -1/myfit(1)
a = exp(myfit(2))
%plot with original data to check it
newfit = a.*exp(-x./b);
plot(x,y, x,newfit);
His problem is the first--it isn't exponential as the semilog plots clearly reveal (unless his unspecified x variable were to somehow make it so, but it clearly isn't in whatever he has used).
loglog() isn't terribly nonlinear over a fair portion of the range, however...
What semilog plots are you referring to?
But yeah, like I said in my answer above, if the plot of (x,log(y)) isn't linear, the data isn't exponential.
My sampling rate was 20kHz. When fitting Webster's equation, I could not do just x, log(y), without making x a log as well. When I tried using normal x (or no x at all), I got the following error:
Using only the real component of complex data.
Complex value computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.
I am quite surprised that my data is not exponential... unless I am doing something wrong. The method I am trying to use is described here ( figure 1A with fitting demonstration attached, in case no access) and my trace is the same sort of recording as that that paper/figure uses.
@J -- I'm referring to the plot of
semilogy(abs(yy))
where yy=y(10000:10500), the range over which the decay is apparent. Restricting it to that range makes the range of the axes more nearly filled with the data of interest and illustrates the extreme nonlinearity.
and the others I posted in earlier remark. semilogy is simply the same thing as your suggested log transform in original coordinates; if that plot isn't linear, the data aren't first-order exponential, same as for the plot of the transformed data.
@IJ - I don't know anything about the subject matter but as is clearly shown by the plot(s) above, the curvature in your data is nearly as great still on the logarithmic scale as it is on linear; it's extremely removed from being simply a first-order exponential.
I can't explain "why"; only can point out that that's the data you have.
As for the fitting problems, log(<0) --> complex value; you can use abs(y) to solve the inverse problem or offset the data or otherwise transform to get it positive but it's still going to be stronger than single exponential.
Thanks for pinpointing where the problem was, dpb and J. Webster. Now I can start looking for alternative approaches.
IJ
Who knows, maybe there's a Nobel prize hidden in the extra curvature waiting to be discovered... :)

Sign in to comment.

More Answers (0)

Categories

Asked:

IJ
on 27 Apr 2016

Commented:

dpb
on 2 May 2016

Community Treasure Hunt

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

Start Hunting!