3
$\begingroup$

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}

$\endgroup$
1
  • $\begingroup$ Related: (14645) $\endgroup$
    – Mr.Wizard
    Commented Apr 27, 2019 at 10:27

1 Answer 1

6
$\begingroup$

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}
$\endgroup$
1
  • $\begingroup$ Thanks a bunch @Mr.Wizard♦ $\endgroup$ Commented Apr 27, 2019 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.