3

Directly going to the issue, my problem is how to use a RecurrenceTable when it have inputs from a vector defined previously. For instance I have:

p={1,10,100,1000,10000};

And the recursive relation I want to solve is:

RecurrenceTable[{a[n + 1] == 2 a[n] + p[[n]], a[1] == 1}, a, {n, 1, 5}]

which must have an answer as:

{1,3,16,132,1264}

But I face with this error:

Part::pkspec1: The expression n cannot be used as a part specification.
Part::pkspec1: The expression #1 cannot be used as a part specification.

For avoiding part specification I tried the following:

z = 1;
RecurrenceTable[{a[n + 1] == 2 a[n] + p[[z++]], a[1] == 1}, a, {n, 1, 5}]

But the answer is wrong:

{1, 3, 7, 15, 31}

1 Answers1

6

Try Indexed:

p = {1, 10, 100, 1000, 10000};

RecurrenceTable[{a[n + 1] == 2 a[n] + Indexed[p, n], a[1] == 1}, a, {n, 1, 5}]
{1, 3, 16, 132, 1264}

Alternatives:

RecurrenceTable[{a[n + 1] == 2 a[n] + Unevaluated[p[[n]]], a[1] == 1}, a, {n, 1, 5}]
{1, 3, 16, 132, 1264}
part[x_, n_Integer] := x[[n]]

RecurrenceTable[{a[n + 1] == 2 a[n] + part[p, n], a[1] == 1}, a, {n, 1, 5}]
{1, 3, 16, 132, 1264}

Of course you could just Quiet the error and ignore it:

RecurrenceTable[{a[n + 1] == 2 a[n] + p[[n]], a[1] == 1}, a, {n, 1, 5}] // Quiet
{1, 3, 16, 132, 1264}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371