I have a system of delay-difference equations which I would like to iterate in an efficient fashion. Just using Table[] will iterate the system but as the size of the system grows the execution time gets long. I think the inefficiency comes from re-computing values multiple times instead of re-using the results. I thought RecurrenceTable[] would be more efficient, but I can’t seem to find the correct usage that doesn’t result in me getting an error message. If you have suggestion(s) I would be happy to listen.
Thanks,
Robert Buchanan
Hello, Robert, interesting question!
I propose solution with s.c. memoization for recursion (accumulating all results).
What about RecurrenceTable I ran into some problems and will answer later.
Clear[k1, k2, k3, s, maxj, maxn, data, recFun];
(*This is numerical coeffs*)
k1 = 0.15 (11 Exp[1]/3);
k2 = 11./3;
k3 = 13./1056;
s = 12;
(*Dimensions of data*)
maxj = 3; maxn = 80;
(*Prepare array with initial values and some non-numerical value as \
default*)
data = SparseArray[Table[{1, n} -> k1 ((n + 8) Exp[-n/8] - 8),
{n, maxn}], {maxj, maxn}, _];
(*Define recursive function*)
recFun[j_, n_] := Which[
(*If n<1 immediately return 0*)
n < 1, 0,
(*If data always defined, return it*)
NumericQ@data[[j, n]], data[[j, n]],
(*In other cases calculate value, set it to data and return*)
True, (data[[j, n]] = recFun[j, n - 1] +
k2 Log[1 + k3 (recFun[j - 1, n - s - 1] - recFun[j, n - s - 1])])
];
Run function for maxj,maxn; it takes fraction of second:
recFun[maxj, maxn]
Now all needed values are stored in data, and you can do whatever you want to with them,
eg
ListLinePlot[data]
Denis,
Thanks for your suggestion. It works wonderfully and fast. I tried memoization of the form:
\[CapitalPhi][j_Integer,n_Integer]:=\[CapitalPhi][j,n]=\[CapitalPhi][j,n-1]+(11/3)Log[1+(13/1056)(\[CapitalPhi][j-1,n-\[Sigma]-1]-\[CapitalPhi][j,n-\[Sigma]-1])]/;And[n>=0,j>=2]
but I kept getting a MaxRecursion error message. I don’t know why. Anyhow, thanks again. You really helped me out.
Bob
Hello again! Your main idea is OK, please double check your code!
This works fine: