-1
$\begingroup$

I have a function, not very complicated, somewhat similar to an infinite, converging Geometric Progression. Here is my function: Y = n^t / t!. Nonlinear Model Fit works not at all with this expression. Find Fit works but the fit to my data is pretty much worthless. I have tried using Method->"Automatic", "QuasiNewton", and "NMinimize". Advice? Suggestions?

NonlinearModelFit[data1,(a n^x) / (m x!)[x, a, n, m], {{a,3},{n,5},{m,2}},x, Method->"Automatic"]
$\endgroup$
  • 2
    $\begingroup$ Please, post your actual Mathematica code. $\endgroup$ – Anton Antonov Oct 27 at 19:27
  • 1
    $\begingroup$ I added OP's code from an OP comment to my first answer. $\endgroup$ – Anton Antonov Oct 28 at 12:43
3
$\begingroup$

My code, nowhere near as sophisticated as your code: NonlinearModelFit[data1,(a n^x) / (m x!)[x, a, n, m], {{a,3},{n,5},{m,2}},x, Method->"Automatic"]

In Mathematica you cannot define a function and invoke it in this way:

(a n^x) / (m x!)[x, a, n, m] 

You have to define a function:

Y[x_, a_, n_, m_] := (a n^x) / (m x!);
Y[x, a, n, m] 

or use a pure function, like this:

(#1 #3^#1)/(#4 #1!) &[x, a, n, m]

or like this:

Function[{x, a, n, m}, (a n^x)/(m x!)][x, a, n, m]

Here is complete code using your (not working) NonlinearModelFit code:

SeedRandom[232];
Block[{c = 20, n = 6},
  data1 = 
   Table[{t, (n + RandomReal[{-0.01, 0.01}]*n)^t/t!}, {t, 0, c, 0.2}]
  ];

ListPlot[data1]

Clear[Y];
Y[x_, a_, n_, m_] := (a n^x) / (m x!);

nlm = NonlinearModelFit[data1, 
  Y[x, a, n, m], {{a, 3}, {n, 5}, {m, 2}}, x, Method -> "Automatic"]

Plot[nlm[x], {x, 0, 20}, Epilog :> Point[data1], 
 PlotStyle -> {Orange, Thick}, PlotRange -> All]

enter image description here

First answer

Is this something you are looking for:

nlm =
 Module[{c = 20, n = 3, k = 12, a, as, tbl, nlm},
  as = Array[a, k];
  tbl = Table[{t, n^t/t!}, {t, 0, c, 0.2}];
  nlm = NonlinearModelFit[tbl, Table[t^i, {i, k}].as, as, t];
  Echo@Plot[nlm[x], {x, 0, 10}, Epilog :> Point[tbl], PlotStyle -> {Orange, Thick}, PlotRange -> All];
  nlm
 ]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ My code, nowhere near as sophisticated as your code: NonlinearModelFit[data1,(a n^x) / (m x!)[x, a, n, m], {{a,3},{n,5},{m,2}},x, Method->"Automatic"] $\endgroup$ – OKCarl Oct 27 at 20:35

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.