1

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] 
dr.blochwave
  • 8,768
  • 3
  • 42
  • 76
HenryB
  • 81
  • 3
  • 1
    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. – dr.blochwave Jun 27 '15 at 17:02
  • Also, the use of Subscripted variables is discouraged. – shrx Jun 27 '15 at 18:39
  • 1
    Using N,o in one place and N,0 in the other might be a problem. – Bill Jun 27 '15 at 19:09
  • 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] – HenryB Jun 27 '15 at 20:17
  • Please edit your question to make the suggested corrections. Only then can readers effectively run your code and make further suggestions. Thanks. – bbgodfrey Jun 27 '15 at 20:26

2 Answers2

3

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.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 1
    "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. – J. M.'s missing motivation Jun 28 '15 at 00:32
  • 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. – HenryB Jun 28 '15 at 01:30
  • @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. – Michael E2 Jun 28 '15 at 03:18
2

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

ubpdqn
  • 60,617
  • 3
  • 59
  • 148