1
$\begingroup$

Can someone please tell me why this expression returns error messages about division by zero.

NnLnMdlFt = 
 NonlinearModelFit[
  dtptsC, (100.0*((Subscript[k, 1]*Subscript[N, o])/(Subscript[k, 2] -
          Subscript[k, 
         1])*((E^-(Subscript[k, 1]*t)) - (E^-(Subscript[k, 2]*
            t))))), {Subscript[N, 0], Subscript[k, 1], Subscript[k, 
   2]}, t]  

Edit

Thanks for your suggestions. First off, here are my data points:

dtptsC = {{1.0, 1.0}, {5.0, 80.0}, {10.0, 80.0}, {20.0, 63.0}, {30.0, 
    50.0}, {40.0, 38.0}, {60.0, 24.0}, {75.0, 16.0}, {95.0, 10.0}}; 

And here is the corrected version of my equation which still returns error messages about division by zero:

NnLnMdlFt = 
 NonlinearModelFit[
  dtptsC, (100.0*((j*m)/(k - j)*((E^-(j*t)) - (E^-(k*t))))), {m, j, 
   k}, t] 
$\endgroup$
  • 1
    $\begingroup$ Without a definition of dtptsC I don't see how we can help. Also, it's worth noting that you shouldn't use capital N as a variable name, as it is a function in Mathematica. $\endgroup$ – dr.blochwave Jun 27 '15 at 17:02
  • $\begingroup$ Also, the use of Subscripted variables is discouraged. $\endgroup$ – shrx Jun 27 '15 at 18:39
  • 1
    $\begingroup$ Using N,o in one place and N,0 in the other might be a problem. $\endgroup$ – Bill Jun 27 '15 at 19:09
  • $\begingroup$ Gentlemen. Thanks for responding to my query. First off I should say when I replace the variables with numerical quantities I have no problems plotting a graph of the function against my set of data points. I changed the names of my variables, but I'm still getting the same error message about division by zero. Here is my new, revised expression:NnLnMdlFt = NonlinearModelFit[ dtptsC, (100.0*((jm)/(k - j)*((E^-(jt)) - (E^-(k*t))))), {m, j, k}, t] $\endgroup$ – HenryB Jun 27 '15 at 20:17
  • $\begingroup$ Please edit your question to make the suggested corrections. Only then can readers effectively run your code and make further suggestions. Thanks. $\endgroup$ – bbgodfrey Jun 27 '15 at 20:26
3
$\begingroup$

You should (always?) evaluate your model on your data, or at the least, do it when you get errors:

model = (100.0*((Subscript[k, 1]*n)/(Subscript[k, 2] - 
        Subscript[k, 1])*((E^-(Subscript[k, 1]*t)) - (E^-(Subscript[k, 2]*t)))));

dtptsC = {{1.0, 1.0}, {5.0, 80.0}, {10.0, 80.0}, {20.0, 63.0}, {30.0, 
    50.0}, {40.0, 38.0}, {60.0, 24.0}, {75.0, 16.0}, {95.0, 10.0}};

model /. Thread[{Subscript[k, 1], Subscript[k, 2]} -> 
   Transpose[dtptsC]]

Power::infy: Infinite expression 1/0. encountered. >>
Infinity::indet: Indeterminate expression 0. n ComplexInfinity encountered. >>

{Indeterminate, 6.66667 (-E^(-80. t) + E^(-5. t)) n, 
 14.2857 (-E^(-80. t) + E^(-10. t)) n, 
 46.5116 (-E^(-63. t) + E^(-20. t)) n, 
 150. (-E^(-50. t) + E^(-30. t)) n, -2000. (E^(-40. t) - 
    E^(-38. t)) n, -166.667 (E^(-60. t) - 
    E^(-24. t)) n, -127.119 (E^(-75. t) - 
    E^(-16. t)) n, -111.765 (E^(-95. t) - E^(-10. t)) n}

The Indeterminate (or Infinity or ComplexInfinity if they existed) indicate which data points cause the model to fail.

I have no suggestions for fixes. The model could be wrong. The data could be wrong. That is the researcher's call.

Note: I replaced N-sub-0 with just n. N is a built-in function, and it is not a good idea to use it as a variable, probably even inside Subscript.

$\endgroup$
  • 1
    $\begingroup$ "You should (always?) evaluate your model on your data..." - since FindFit[] will be doing that anyway, quite likely many times, you will want to do this sanity check first so that FindFit[] won't waste your time. $\endgroup$ – J. M. will be back soon Jun 28 '15 at 0:32
  • $\begingroup$ Its Working! Turns out I had to give Mathematica initial values: {{:m,22},{j,0.02},{k,0.45},t} Much Happiness! Thanks for the input. $\endgroup$ – HenryB Jun 28 '15 at 1:30
  • $\begingroup$ @HenryB You should post your solution as an answer (though you might have to wait a day). I see now that the model approaches a finite limit at the first data point, and so it can be "rescued." Giving good initial values has come up before, as I recall, but I couldn't find it just now. $\endgroup$ – Michael E2 Jun 28 '15 at 3:18
2
$\begingroup$

Change of variable: $s=k-j$. Manipulate is helpful for starting values:

lp = ListPlot[dtptsC, PlotStyle -> Red, 
   PlotMarkers -> {Automatic, 10}];
f[j_, s_, n_, t_] := 100 n/s ( Exp[-j t] - Exp[-(j + s) t])
Manipulate[
 Show[Plot[f[j, s, n, t], {t, 1, 95}, PlotRange -> {0, 100}], lp], {j,
   0.01, 0.3, Appearance -> "Labeled"}, {s, 0.0001, 0.1, 
  Appearance -> "Labeled"}, {n, 0.01, 0.5, Appearance -> "Labeled"}]

enter image description here

Model:

nlm = NonlinearModelFit[dtptsC, 
  f[a, b, c, t], {{a, 0.04}, {b, 0.1}, {c, 0.2}}, t]
Normal@nlm
nlm["AdjustedRSquared"]
{je, ke, ne} = {a, a + b, c} /. nlm["BestFitParameters"]

The model: 120.835 (-E^(-0.239644 t) + E^(-0.0284104 t))

Adjusted R^2: 0.956727

Reverting to original $\{\hat{j},\hat{k},\hat{n}\}$={0.0284104, 0.239644, 0.255244}

Visualizing:

Show[Plot[nlm[t], {t, 0, 95}], lp]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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