Problem with fit function
40 views (last 30 days)
I am trying to fit this dataset
x1=[178 1316 3236 5155 7075 8996 10916 12836 14756 16676 18568]
y1=[1.0000 1.1848 1.4038 1.6632 1.9705 2.3347 2.7661 3.2773 3.8829 4.6005 5.4507]
When i plot datn i see some negative values (for example min(datn)=-1.3729e+10) which do not make sense to me for this plot. I have other dataset with very similar values that do not have any negative values as output so i am trying to understand what is different in this dataset. Is it possibe to force the fit to give us only positive values? Thank you.
Stephan on 3 May 2018
Edited: Stephan on 3 May 2018
x1=[178 1316 3236 5155 7075 8996 10916 12836 14756 16676 18568]'
y1=[1.0000 1.1848 1.4038 1.6632 1.9705 2.3347 2.7661 3.2773 3.8829 4.6005 5.4507]'
x1 and y1 were transposed to column vectors and the option of your fit-function is set to 'exp1' instead of 'exp2'. This change changes the R-square value from 1 to 0.9998, which seems acceptable. Does this work for you?
More Answers (1)
dpb on 3 May 2018
Edited: dpb on 3 May 2018
General model Exp2:
fitn(x) = a*exp(b*x) + c*exp(d*x)
Coefficients (with 95% confidence bounds):
a = 1.053 (1.05, 1.056)
b = 8.848e-05 (8.828e-05, 8.868e-05)
c = -0.8766 (-1.8e+05, 1.8e+05)
d = -0.01421 (-1153, 1153)
NB: the error bounds on the second exponential term are several orders of magnitude the size of the coefficients--iow, they're pure noise and shouldn't be left in a model. Also note that the sign of the second exponential term itself is negative.
Also, you're evaluating the fit beyond the range of the data; always risky and particularly when there are such terms included.
Try plotting the data and the fit and it's pretty clear what went wrong; you presumed a model that may work for some sets of data but is clearly just inappropriate for this set.
Although from the above evaluated range one won't get function values <0, with c<0 it is surely possible for x<0 and the d<0 means the second term is reduced for x<0.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ADDENDUM Interesting to look at the pieces...defined
>> fn1=@(x) fitn.a*exp(fitn.b*x);
>> fn2=@(x) fitn.c*exp(fitn.d*x);
for the two exponentials independently and plotted the raw data and the fit of the full model at the input data points; then over the range to the left of the actual day of your xnew excepting using only the xlim(1:x1(2)]) to blow up the region so can see what happens. Then added the full function at more points to see detail of its shape and then the two pieces that make the total...you see how the second just dives and has the opposite sign of the exponential to create the curvature in the LH region of the original data and while you can force a fit fairly well, it is a terrible model for extrapolation as you have done.