3
$\begingroup$

I am having trouble with the below code. I have two data sets, dataf and datag, which are exponential decays. datag decays faster than dataf. These two data sets are then later joined to make datah. I would like to simultaneously fit these two data sets with the functions f[x] and g[x], respectively. In the NonLinearModelFit of datah I use suitable initial conditions for the values of a and b, but as can be seen from the below plot the fits are not good. If I don't use initial conditions, I get the following error message: "General::munfl: Exp[-1.25177*10^10] is too small to represent as a normalized machine number; precision may be lost", and other similar error messages.

dataf = Table[{x, Exp[-x/250]}, {x, 0, 500, 50}];
datag = Table[{x, Exp[-x/100]}, {x, 0, 500, 50}];

datah = Join[{1, Sequence @@ #} & /@ dataf, {2, Sequence @@ #} & /@ 
    datag];

f[x_] := Exp[-47000^2 ((a (4 a^2 + 21 a b + 9 b^2))/(
     30 (a + b) (4 a + b))) x];
g[x_] := Exp[-130000^2 ((a (4 a^2 + 21 a b + 9 b^2))/(
     30 (a + b) (4 a + b))) x];

h[\[Omega]_, x_] := 
  KroneckerDelta[\[Omega] - 1]*f[x] + 
   KroneckerDelta[\[Omega] - 2]*g[x];

Clear[a, b]

nlmf = NonlinearModelFit[datah, 
   h[\[Omega], x], {{a, 10^-12}, {b, 10^-12}}, {\[Omega], x}];

Show[ListPlot[{dataf, datag}, PlotMarkers -> {\[FilledCircle], 10}, 
  Joined -> False], Plot[{nlmf[1, x], nlmf[2, x]}, {x, 0, 500}]]

nlmf["ParameterConfidenceIntervalTable"]

Simultaneous NonLinearModelFit

If the exponential decays are quicker and the values inside the exponentials are smaller then the fits become reasonable. If anyone has any suggestions about how to resolve this issue then that would be greatly appreciated. Thanks.

$\endgroup$
5
  • $\begingroup$ not a comment but a quick pointer: do you know about this? resources.wolframcloud.com/FunctionRepository/resources/… $\endgroup$
    – chris
    Commented Sep 11, 2021 at 10:14
  • $\begingroup$ Thanks very much for the suggestion. This will certainly be incredibly useful. However, I have now gone through the MultiNonLinearModelFit and I get the same poor fitting curves and similar values for "a" and "b". This is now accompanied by the following error message: "The step size in the search has become less than the tolerance prescribed by the PrecisionGoal option, but the gradient is larger than the tolerance specified by the AccuracyGoal option. There is a possibility that the method has stalled at a point that is not a local minimum". Any thoughts? Thanks. $\endgroup$
    – sje1g13
    Commented Sep 11, 2021 at 10:47
  • $\begingroup$ You could try to start from this? dataf = Table[{x, Exp[-x/2]}, {x, 0, 5, 1/2}]; datag = Table[{x, Exp[-x/1]}, {x, 0, 5, 1/2}]; data = {dataf, datag} // N; f[x_] = Exp[-a x]; g[x_] = Exp[-b x]; fit = ResourceFunction["MultiNonlinearModelFit"][ data, {f[x], g[x]}, {a, b}, {x}]; Show[ ListPlot[data], Plot[{fit[1, x], fit[2, x]}, {x, -5, 5}] ] $\endgroup$
    – chris
    Commented Sep 11, 2021 at 13:23
  • 2
    $\begingroup$ It seems to me it's more of a conceptual problem than a numerical one. The argument of the exponential cannot match -1/250 x and - 1/100 x respectively, whatever the value of (a,b) $\endgroup$
    – chris
    Commented Sep 11, 2021 at 13:32
  • $\begingroup$ Thanks very much for the above code. It certainly works very well. I also agree with you about the problem at hand, i.e., that this is indeed a conceptual issue. $\endgroup$
    – sje1g13
    Commented Sep 15, 2021 at 11:55

1 Answer 1

6
$\begingroup$

There's a very simple answer to your question: you have only one free parameter in your fitting function.

You may think you have two of them, $a$ and $b$, but in reality, you only have one, let's name it $c$:

$$c = \frac{a (4 a^2 + 21 a b + 9 b^2)}{ (a + b) (4 a + b)}$$

The functions you are trying to fit are then:

$$f(x) = e^{-\alpha c x}, \quad g(x) = e^{-\beta c x}, \quad \alpha=\frac{220900000 }{3}, \beta=\frac{1690000000}{3}.$$

This means that the two exponentials have a predefined ratio of their decays. That is:

$$\frac{\text{decay rate of }f(x)}{\text{decay rate of }g(x)} = \frac{\alpha c}{\beta c} = \frac{\alpha}{\beta}= \frac{2209}{16900}\approx 0.13.$$

Therefore, when you decide about the decay rate of $f(x)$, you also fix the other one. But look at your data! dataf decays with the rate $-1/250$ and datag with $-1/100$. Their ratio is $2/5 = 0.4$.

\[Alpha] = 220900000/3;
\[Beta] = 1690000000/3;
f[x_, c_] := Exp[-\[Alpha] c x];
g[x_, c_] := Exp[-\[Beta] c x];
h[\[Omega]_, x_, c_] := 
 KroneckerDelta[\[Omega] - 1]*f[x, c] + 
  KroneckerDelta[\[Omega] - 2]*g[x, c]

Manipulate[
 Show[Plot[Evaluate[{h[1, x, c], h[2, x, c]}], {x, 0, 500}, 
   PlotRange -> {0, 1}], ListPlot[{dataf, datag}]], {c, 0, 1*^-10}]

Animation

I don't really know what the underlying model is and why you are trying to simultaneously fit both of the curves. You can, of course, fit each of them separately with their own $c$ (or $a$ and $b$, if you want).

nlmf = NonlinearModelFit[#[[1]], Exp[-#[[2]] c x], {{c, 1*^-10}}, 
    x] & /@ {{dataf, \[Alpha]}, {datag, \[Beta]}}

Show[ListPlot[{dataf, datag}], 
 Plot[Evaluate@Through[nlmf[x]], {x, 0, 500}]]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thanks very much for your super clear answer. A colleague of mine also came to the same conclusion. $\endgroup$
    – sje1g13
    Commented Sep 15, 2021 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.