Saturday, March 10, 2012

RecurrenceTable and NestList


RecurrenceTable and NestList do the same thing—build a list of values by repeatedly applying a function to an initial expression. However NestList is the more general function and RecurrenceTable is specialized to handle recurrence relations, and the syntax of each reflects this. Here is a simple comparison. See the examples in the Doc Center for RecurrenceTable for more sophisticated examples.

In[1]:= NestList[Sqrt, 100.0, 5]

Out[1]= {100., 10., 3.16228, 1.77828, 1.33352, 1.15478}

In[2]:= NestList[#^2 &, Last@%, 5]

Out[2]= {1.15478, 1.33352, 1.77828, 3.16228, 10., 100.}

In[3]:= RecurrenceTable[{a[n + 1] == Sqrt@a@n, a[1] == 100}, a, {n, 1, 5}] // N

Out[3]= {100., 10., 3.16228, 1.77828, 1.33352}

In[4]:= RecurrenceTable[{a[n + 1] == a[n]^2, a[1] == Last@%}, a, {n, 1, 5}]

Out[4]= {1.33352, 1.77828, 3.16228, 10., 100.}

No comments:

Post a Comment