Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
Would NestList[{1, 2, 3} # &, {1, 2, 3}, 3] be acceptable ? –  b.gatessucks Jan 8 '13 at 19:00
    
Try to write down your recurrence formulae ... –  belisarius Jan 8 '13 at 19:12
    
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.) –  whuber Jan 8 '13 at 21:00
add comment

1 Answer 1

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}} *)
share|improve this answer
    
(+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. –  whuber Jan 8 '13 at 20:51
1  
@whuber That was the whole trick ! –  b.gatessucks Jan 8 '13 at 21:00
    
And a nice trick it is. :-) –  Mr.Wizard Jan 8 '13 at 22:18
    
Thanks. All the comments have been very helpful! –  Alex Jan 9 '13 at 15:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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