MATLAB: Unexpected results from ‘exp2’ least squares fit

fit exp2 exponential curve fitting

For most of my data this code works fine, but I am having a very poor fit for the following section of data. Does anyone know what I did wrong?
x = [1.7125 5.0430 9.1750 15.4250 22.7550 30.3800 40.4500 52.4150 63.9400 74.5300 89.5500 105.3000 119.8000 135.9000 153.4500 167.5000 185.4000 205.9000 223.9000 244.5000 267.7000 290.8500 317.6000 341.9500 367.9000 399.1000 432.7500 461.8000 493.4500 521.5000 555.6000 588.4500 617.5500 654.2000 688.9000 729.6000 772.2000 820.5000 862.5000]';
y = [3.7148 3.3178 2.7135 2.4753 2.2017 1.9689 1.6732 1.5649 0.6837 0.4081 0.3183 0.2845 -0.3022 -0.1981 -1.1470 -1.7174 -1.6783 -1.2277 -2.3289 -2.5576 -2.4626 -2.5281 -3.4626 -2.8697 -4.7081 -3.6971 -3.7245 -4.7752 -4.7768 -4.6965 -5.3429 -6.0585 -5.4439 -6.2222 -6.2646 -6.1612 -6.4458 -6.8387 -6.9563]';
scatter(x,y,5,[0 0 0],'fill');
hold on
f = fit(x,y,'exp2');
x2=linspace(min(x),max(x));
y2=f(x2);
plot(x2,y2,'color',[0 0 0],'linewidth',2);

Best Answer

Sums of exponential fits are difficult to solve. They are HIGHLY susceptible to starting value choices.
1. When you start with the wrong starting values, then expect crap.
2. Fit by default starts from some very arbitrary starting values. So what should you expect, at least some of the time? (See 1.)
A better scheme to estimate that nonlinear model is to use my fminspleas . It is on the file exchange. Follow the link.
mdlterms = {@(coef,X) exp(-coef(1)*X), @(coef,X) exp(-coef(2)*X)};
format long g
[nlcoef,lincoef] = fminspleas(mdlterms,[0.001,0.002],x,y)
nlcoef =
-0.000869991132855897 0.00532565665197467
lincoef =
-3.4116826458322
6.61061671877021
Fminspleas survives here because it uses a completely different scheme to estimate the linear versus nonlinear coefficients in the model. It reduces the nonlinear search space.
yhat = lincoef(1)*exp(-nlcoef(1)*x) + lincoef(2)*exp(-nlcoef(2)*x);
plot(x,y,'ro',x,yhat,'b-')
To be honest, I really don't like the choice of model here at all. What did I say above? They are difficult to fit. But worse, look at the coefficients!!!!!!!!! Look at them carefully!
One of the nonlinear rate coefficients is positive, the other negative. So if we tried to extrapolate this model, see what happens.
xextrap = linspace(-150,3000,500);
yextrap = lincoef(1)*exp(-nlcoef(1)*xextrap) + lincoef(2)*exp(-nlcoef(2)*xextrap);
plot(x,y,'ro',xextrap,yextrap,'b-')
So, the question is, do rate coefficients of completely different sign make ANY sense in context of why you chose that model? Be honest. Why did you choose that model in the first place? If your only reason was because you thought it might have the right shape, that is a bad reason.
An alternative model is just to use a spline fit. Using my SLM toolbox (also on the file exchange)
slm = slmengine(x,y,'decreasing','on','concaveup','on','plot','on');
The fit looks at least as good a fit as the sum of exponentials.