# Problem fitting a exponential function to data points [closed]

finale edit: i found someone who solved my problem via matlab. therefore i wont try out the proposed solutions now, but wont delete my question in case someone might have use for the proposed solutions in the future. thx to everyone who tried to help me

I have a csv data with measure points. These points follow a exponential curve in regular sequences.

edit: had to remove the plot

Now I need to find an exponential function to fit for example for the time interval between 6 minutes and 9 minutes (the second gray bar). I created a new data for this which only contains the measure points between 6 and 9 min). But if I try the usual:

data=Import["mydataselection","CSV"];
nlm=NonlinearModelFit[data[[All,{1,2}]],a Exp[-k t+m],{a,k,m},t]


I get completely wrong numbers for a,k and m. in this case: $a=0.142679$, $m=0.441977$ and $k=0.122386$. (btw I think I need the $+m$ somewhere because I should get the same "a" and "k" for the time interval 6 to 9 and 12 to 15 etc.

The plot of this exponential function looks like this: here you can see what the plot of the data points for 6 to 9 min looks like and on the right what the plot of the exponential fit with a Exp[-k t] looks like, which clearly doesn't fit.

In another case (working with another csv data I even got a negative "a" although the curve is very similar.

Maybe its also important to note that I have a lot of datapoints (from 6 to 9 min its about 24000 datapoints).

Can anyone help me out here?

Edit: Maybe I should note that the CSV data looks like this: x1,y1, x2,y2, ...

edit2: I can't post more pictures or the actual data because I need 10 "reputation" for that.

Also I removed the "+m", but I still can't get proper values. For example for 9 to 12 min I get positive a Exp[k t] with both positive "a" and "k"

Edit3: I switched the second pic for a better one.

## closed as off-topic by MarcoB, m_goldberg, user9660, ubpdqn, Dr. belisariusFeb 22 '16 at 7:01

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – MarcoB, m_goldberg, Community, ubpdqn, Dr. belisarius
If this question can be reworded to fit the rules in the help center, please edit the question.

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Feb 20 '16 at 15:22
• The first part of your question about exponential fitting is discussed in many threads on this site. The second part of your question is essentially about simultaneous fitting of one model to multiple datasets which was also discussed in details here. If you still have difficulties, please clarify what makes trouble and also provide your dataset (you can put it in pastebin.com and publish a link here). – Alexey Popkov Feb 20 '16 at 15:27
• You need to share your data in order for members to resolve your problem. I have seen people using a site called Pastebin in order to share data files. – Jack LaVigne Feb 20 '16 at 15:28
• You have an identifiability issue in that you really only have 2 distinct parameters rather than 3. Note that $a e^{-k t + m}=a e^{m} e^{-k t}=c e^{-k t}$. So rewriting your model as $c e^{-k t}$ should get you more consistent results. – JimB Feb 20 '16 at 15:51
• i tried out removing the +m. didnt help getting better results. – Christopher Bee Feb 20 '16 at 16:09

I assume that you need to compare parameters among the different 3-minute time intervals. To do so you'll need to construct a functional form that allows such a comparison. You mention an "exponential" curve but that needs more specificity. One possibility is the following.

First set the beginning of the time period: t0.

t0 = Min[data[[All, 1]]];


Then fit the following model:

nlm = NonlinearModelFit[data, {a Exp[-k (t - t0)] + m, m < Min[data[[All, 2]]]},
{a, k, {m, 0.075}}, t]
nlm["BestFitParameters"]
(* {a->0.03810162618882989, k->1.0157428075372188, m->0.07725243271210368} *)


Then display the fit and various residual plots:

(* Collect some summary statistics *)
residuals = nlm["FitResiduals"];
predicted = nlm["PredictedResponse"];
residMean = Mean[residuals];
residSD = StandardDeviation[residuals];

(* Data and predictions *)
Show[ListPlot[data, PlotStyle -> LightGray],
Plot[nlm[t], {t, 6, 9}, PlotStyle -> Red],
PlotLabel -> "Data and prediction", Frame -> True]


(* Predicted vs. Residual *)
ListPlot[Transpose[{predicted, residuals}],
PlotLabel -> "Predicted vs. Residual",
Frame -> True, PlotStyle -> LightGray]


(* Histogram of residuals *)
Show[{Histogram[residuals, Automatic, "PDF"],
Plot[PDF[NormalDistribution[residMean, residSD], x], {x, Min[residuals], Max[residuals]}]},
PlotRange -> All, PlotLabel -> "Histogram of residuals", Frame -> True]


(* Quantile plot *)
QuantilePlot[residuals, PlotLabel -> "QuantilePlot", Frame -> True]


The histogram and quantile plot suggest some departures from the assumed normality (a slightly heavy right tail and a slightly light left tail - i.e., residuals are skewed to the right a bit). Given the large amount of data, the standard errors for the parameter estimates might be a bit off but probably nothing to worry about.

• +1 for identifying the crucial factor of adding an offset m. As you pointed out in your earlier comment the time offset, t0 is really not needed. However, I think it is a nice touch and probably useful when describing real physical systems so that the amplitude is significant. – Jack LaVigne Feb 21 '16 at 14:59
• @JackLaVigne. Thanks. I agree with what you say about the amplitude: t0 is needed to allow for meaningful comparisons of the a parameter across datasets. – JimB Feb 21 '16 at 16:32

I think I saw this data once on this site and the plots look suspiciously familiar ... but whatever, a start to discuss:

myData = Import["data.txt", "Data"]


model = -a  x^3 + b  x^2 - c x + d

myFit = Normal[NonlinearModelFit[myData, model, {a, b, c, d}, x]]


$-0.00160329 x^3+0.0411292 x^2-0.354777 x+1.10907$

lp1 = ListPlot[{myData}]
p2 = Plot[myFit, {x, 6, 9}, PlotStyle -> Red]
Show[lp1, p2]


• i dont know what you mean with "suspicious". i know that you cant have seen this exact dataset because it was taken two months ago and not posted anywhere. you might have seen similar plots because its from a "special" way to analyze certain molecules (dont want to touch on chemistry in this forum^^) – Christopher Bee Feb 21 '16 at 16:03
• now to your "solution": as i stated in my last edit, the dataset was fittable with an exponentil funktion very nicely. and sinc in the end i needed the "k" value, your solution doesnt solve the problem. – Christopher Bee Feb 21 '16 at 16:08