# Recursion with two variables

Consider the recursion formular

$$p_n(k) = (k+1) \,p_{n-1}(k)+(n-k)\,p_{n-1}(k-1)\qquad n\geq 3,$$

for $k=1,2,...,n-2$, with the conditions

$$p_n(0)=p_n(n-1)=1\qquad n\geq 2$$

How can I get numerical solutions of that system with help of RSolve or RecurrenceTable?

Edit 2: After restarting the kernel I got Edit 3: Sorry, here it is

 RecurrenceTable[{p[n,k] == (k+1)p[n-1,k ]+(n-k)p[n -1,k-1],
p[2,0]==1,p[2,1]==1,p[n,0]==1,p[n,n-1]==1}, p,{n,3,10},{k,1,n-2}]


Edit 4: The first few iterations are

$$[1, 1]$$

$$[1, 4, 1]$$

$$[1, 11, 11, 1]$$

$$[1, 26, 66, 26, 1]$$

$$[1, 57, 302, 302, 57, 1]$$

$$[1, 120, 1191, 2416, 1191, 120, 1]$$

$$[1, 247, 4293, 15619, 15619, 4293, 247, 1]$$

$$[1, 502, 14608, 88234, 156190, 88234, 14608, 502, 1]$$

• Please show what you tried with RSolve and what it returned. The documentation has examples with two variables. Search for "partial" within the RSolve doc page. – Szabolcs Apr 14 '17 at 19:58
• Re update: p is black, which tells you that it has a value. Restart your kernel (Quit[]) and try again. Or just Clear[p]. – Szabolcs Apr 14 '17 at 20:02
• It might be a problem that $k$ runs from 1 to $n-2$ and not to a fixed number. – kaffeeauf Apr 14 '17 at 20:05
• Please post copyable code too, not only the screenshot. I'm off for today, sorry, but you have a better chance for an answer if people don't have to retype your code. – Szabolcs Apr 14 '17 at 20:06
• RSolve[] and RecurrenceTable[] are sadly not that very good at handling partial difference equations. – J. M. will be back soon Jul 31 '17 at 3:09

Purely for convenience, let us shift all indices by one, so that the smallest indices are 1 instead of 0. Then, the code in the question becomes

RecurrenceTable[{p[n, k] == (k + 1) p[n - 1, k] + (n - k) p[n - 1, k - 1], p[3, 1] == 1,
p[3, 2] == 1, p[n, 1] == 1, p[n, n - 1] == 1}, p, {n, 4, 10}, {k, 2, n - 2}]


It is not clear to me that RecurrenceTable is able to accept either the initial condition p[n, n - 1] == 1 or the limits {k, 2, n - 2}, although I could be mistaken. In any case, an alternative approach is to define the iterative function.

p[3, 1] = 1;
p[3, 2] = 1;
p[n_, 1] = 1;
p[n_, k_] := 1 /; k == n - 1
p[n_, k_] := (k + 1) p[n - 1, k] + (n - k) p[n - 1, k - 1]


Note that p[n_, k_] := 1 /; k == n - 1 sets p[k, n] to 1 only if k == n - 1, reproducing the condition p[n, n - 1] == 1 in the question.

ptab = Table[p[n, k], {n, 1, 10}, {k, 1, Max[n - 1, 1]}];
Grid[ptab] n varies from 1 to 10, top to bottom, and k from 1 to 10, left to right.

Just for something different:

f[a_] := {1}~Join~
MapIndexed[{Length@a - #2[] + 1, #2[] + 2}.#1 &,
Partition[a, 2, 1]]~Join~{1}
r := {1}
r := {1}
r[n_] := Nest[f, {1, 1}, n - 1]


Visualizing:

Column[Row[r@#, " | "] & /@ Range[0, 10], Alignment -> Center] 