MATLAB: Difference between predefined and custom exponential fit function

curve fittingMATLAB

Hello all, I'm trying to define a custom function for fitting some data, of the shape a+b*exp(c*x). But to test whether Im' doing this correctly, I first tried to compare fitting a simple dataset with a predefined expoinential "exp1", and the one that I'd define myself.
x = 1:100;
y = 3*exp(0.2*x); % some random exponential data
f1 = fit(x', y', 'exp1')
This gives the output of
General model Exp1:
f1(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):

a = 3 (3, 3)
b = 0.2 (0.2, 0.2)
which is correct.
Now, defining the same thing with fittype:
testexp = fittype('a*exp(b*x)', 'independent', 'x');
f2 = fit(x', y', testexp)
gives the output of
General model:
f2(x) = a*exp(b*x)
Coefficients (with 95% confidence bounds):
a = -3.317e-19 (-1.473e-17, 1.407e-17)
b = 0.9402 (0.5052, 1.375)
which is slightly insane.
Can you please tell me what am I doing wrong?
Kind regards, Marko

Best Answer

As described in the "Optimized Starting Points and Default Constraints" section of this documentation page, when you use the predefined exponential model it uses a heuristic to determine the starting point. When you use a custom nonlinear model, it uses a random starting point.
You can specify a starting point using the fitoptions function. Given that your model to fit includes exp(b*100) which for something as small as b = 0.5 is on the order of 5e21, I think the better a starting guess you can specify for the b coefficient the more likely it is that your fitting will succeed.