# A more efficient way of fitting a non-linear model that returns an entire list?

I have a function that takes two parameters, del and nu, and returns a list npoints long with a bin-width of dt:

ktDynaList[npoints_?NumericQ, dt_?NumericQ, del_?NumericQ, nu_?NumericQ] :=
alphaN = 10;
tt = Table[dt i, {i, 0, Npad - 1}];
expwei = Exp[-(alphaN/(npoints*dt))*tt];
gg = (1/3)*(1 + 2*(1 - del^2*tt^2)*Exp[-0.5 del^2 *tt^2]);
ff = Fourier[padgg*expwei*Exp[-nu*tt], FourierParameters -> {1, 1}];
FF = Exp[-nu dt]*ff/(1 - (1 - Exp[-nu dt])*ff);
dkt = Take[InverseFourier[FF, FourierParameters -> {1, 1}]/expwei, npoints];
dkt
]


I want to fit this function to a dataset using NonlinearModelFit, so I have an edited version that returns one value from this array at a particular abscissa t:

ktDyna[npoints_?NumericQ, dt_?NumericQ, del_?NumericQ, nu_?NumericQ, t_?NumericQ] :=
alphaN = 10;
tt = Table[dt i, {i, 0, Npad - 1}];
expwei = Exp[-(alphaN/(npoints*dt))*tt];
gg = (1/3)*(1 + 2*(1 - del^2*tt^2)*Exp[-0.5 del^2 *tt^2]);
ff = Fourier[padgg*expwei*Exp[-nu*tt], FourierParameters -> {1, 1}];
FF = Exp[-nu dt]*ff/(1 - (1 - Exp[-nu dt])*ff);
dkt = Take[InverseFourier[FF, FourierParameters -> {1, 1}]/expwei, npoints];
dkt[[ Round[t/dt] ]]
]


Now, this can be passed to NonlinearModelFit, and will fit a dataset rather successfully. However, the problem is that in its current state it is incredibly inefficient, as the dkt list is recalculated for every single abscissa.

I'm trying to find a way of storing the dkt array such that it does not need to be fully recalculated for each t value, and am wondering if anyone in the community has encountered a problem of this type before? My current thoughts are either

1. An If statement at the beginning of the function which checks if the parameters del or nu have been varied since the last iteration - if they haven't then one can just return the list element from the previously calculated dkt list and avoid a lot of calculation steps. My problem here is that Mathematica doesn't appear to have static variables (that I know of). Is there any way around this?

2. Some sort of flag system that can be passed between function calls which indicates if the full calculation needs to be performed, or if a previously stored list element can be returned.

Or is there some other solution that doesn't require the use of NonlinearModelFit?

-
Without reading in detail, this might help: NonlinearModelFit is based on FindFit, which in turn is based on FindMinimum (FindMaximum) or NMinimize. Sometimes going directly to FindMinimum proves helpful. – Szabolcs Jun 3 '14 at 14:50
You're missing a ? in the last argument of ktDyna. I won't correct it in the post because I don't know if you also have the mistake in your actual code. – Szabolcs Jun 3 '14 at 14:51
Good spot, thanks. Also (for future reference) do I just need to tab indent my code for it to be formatted properly? – Baxter Jun 3 '14 at 14:57
Yes, you can indent with four spaces. This is a lot of work to do by hand, so you can just paste the code, select it, then use the toolbar button that looks like {}. Since you're new here, the site should display editing help on the right of the edit box (if it doesn't, press the ? button for detailed help). If you're browser supports it, you can use the keyboard shortcut Control/Command-K instead of the {} button (I use that). Multi-line code will be indented, inline code will be surrounded using backtick signs. – Szabolcs Jun 3 '14 at 15:18
Here's the editig help. This notation is called Markdown. – Szabolcs Jun 3 '14 at 15:22