5
$\begingroup$

Mathematica can do a RecurrenceTable with a vector, here is a simple example:

RecurrenceTable[{x[n + 1] == 2* x[n], x[0] == {1, 2, 3}}, x, {n,  3}]

with output

{{1, 2, 3}, {2, 4, 6}, {4, 8, 12}, {8, 16, 24}}

I want to do something similar but instead of multiplying by 2 I want to multiple each element in x[n] by a different number. For example, one would think that this would work:

RecurrenceTable[{x[n + 1] == {1,2,3}* x[n], x[0] == {1, 2, 3}}, x, {n,  3}]

since Times is listable but I get an error

RecurrenceTable::excptn: Value {1,2,3,2,4,6,3,6,9} is a numerical exception. >>

I get the same result using MapThread etc. Thoughts?

$\endgroup$
  • 1
    $\begingroup$ Would NestList[{1, 2, 3} # &, {1, 2, 3}, 3] be acceptable ? $\endgroup$ – b.gates.you.know.what Jan 8 '13 at 19:00
  • $\begingroup$ Try to write down your recurrence formulae ... $\endgroup$ – Dr. belisarius Jan 8 '13 at 19:12
  • $\begingroup$ Although you say you see the same problem "using MapThread", in MMA 8 this succeeds: RecurrenceTable[{x[n + 1] == MapThread[Times, {{1, 2, 3}, x[n]}], x[0] == {1, 2, 3}}, x, {n, 3}]. (This expression was prompted by @b.gatessucks' answer.) $\endgroup$ – whuber Jan 8 '13 at 21:00
6
$\begingroup$

The trick below seems to work in this simple case :

g[input_List] := {1, 2, 3} input 

RecurrenceTable[{x[n + 1] == g[x[n]], x[0] == {1, 2, 3}}, x, {n, 3}]
(* {{1, 2, 3}, {1, 4, 9}, {1, 8, 27}, {1, 16, 81}} *)
$\endgroup$
  • $\begingroup$ (+1) The truly curious thing about this work-around is if you merely leave List out of the definition of g, the RecurrenceTable expression does not work (it returns an empty list). Tracing the evaluation reveals the problem: {1,2,3}*x[n] is expanded as {x[n], 2 x[n], 3 x[n]}, because MMA does not (yet) know that x[n] is a list at the time of expansion. $\endgroup$ – whuber Jan 8 '13 at 20:51
  • 1
    $\begingroup$ @whuber That was the whole trick ! $\endgroup$ – b.gates.you.know.what Jan 8 '13 at 21:00
  • $\begingroup$ And a nice trick it is. :-) $\endgroup$ – Mr.Wizard Jan 8 '13 at 22:18
  • $\begingroup$ Thanks. All the comments have been very helpful! $\endgroup$ – Alex Jan 9 '13 at 15:18
1
$\begingroup$

Not sure what the policy is on reviving dead threads, but I came across this after a similar problem and realized that RecurrenceTable works with vectors if you treat the equations with proper matrix calculations. e.g.

RecurrenceTable[{
     x[n + 1] == {{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}.x[n], 
     x[0] == {1, 2, 3}
    }, x, {n, 3}]

gives the correct result without any intermediate functions:

{{1, 2, 3}, {1, 4, 9}, {1, 8, 27}, {1, 16, 81}}
$\endgroup$

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.