1
$\begingroup$

When I use RecurrenceTable to solve a recursion relations, I encounter a problem, which is perhaps simple, but I do not understand why it is complaining. Here is a simple example. First, let's create a list

alist = Table[2^i, {i, 1, 100}]

and next evaluate

RecurrenceTable[{b[n] == alist[[n]] + b[n - 1], b[1] == 2}, b, {n, 1, 10}]

Then the results are

Part::pkspec1: The expression n cannot be used as a part specification. >>
Part::pkspec1: The expression 1+n cannot be used as a part specification. >>
Part::pkspec1: The expression 1+#1 cannot be used as a part specification. >>
General::stop: Further output of Part::pkspec1 will be suppressed during this calculation. >>

{2, 6, 14, 30, 62, 126, 254, 510, 1022, 2046}

Does anyone know where things go wrong and how to solve it?

In general I would like to know how to use the ReccurenceTable when the recursion relation depends on values from another precomputed list.

$\endgroup$
1
$\begingroup$

It appears that RecurrenceTable does some symbolic processing that leads to the unevaluated symbol n being given to Part as its 2nd argument. But Part only accepts integers as its 2nd argument. A work-around is:

Do[a[i] = 2^i, {i, 1, 10}]
RecurrenceTable[{b[n] == a[n] + b[n - 1], b[1] == 2}, b, {n, 1, 10}]

{2, 6, 14, 30, 62, 126, 254, 510, 1022, 2046}

$\endgroup$
  • $\begingroup$ Thank you. Maybe I am using a earlier version of Mathematica. When I evaluate the code in your answer, it has the same problem! $\endgroup$ – Wenzhe May 11 '19 at 1:40
  • $\begingroup$ @Wenzhe. What do you by "it has the same problem"?. There is no way my cod can produce an error message from Part. It make no use of Part. $\endgroup$ – m_goldberg May 11 '19 at 3:31
  • $\begingroup$ Thank you. I shut down Mathematica and restart it, and the problem disappears. It works now. $\endgroup$ – Wenzhe May 11 '19 at 3:52
  • $\begingroup$ Table[2^n - 2, {n, 2, 11}] $\endgroup$ – Gilmar Rodriguez Pierluissi Oct 25 '19 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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