Re: Creating List of Sequence Iterates
- To: mathgroup at smc.vnet.net
- Subject: [mg33407] Re: [mg33385] Creating List of Sequence Iterates
- From: Yas <y.tesiram at pgrad.unimelb.edu.au>
- Date: Wed, 20 Mar 2002 01:53:16 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
G'day Bob,
Use NestList. I have pasted a copy of my notebook text below. You will
also find further examples on the variants of NestList in the
Mathematica Help menu under Built-In-Functions. The book is on-line as
well.
Cheers
Yas
In[1]:= ?NestList
From In[1]:=
"NestList[f, expr, n] gives a list of the results of applying f to expr
0 \
through n times."
In[2]:= ex1 = NestList[f, x, 4]
Out[2]= {x, f[x], f[f[x]], f[f[f[x]]], f[f[f[f[x]]]]}
In[3]:= f[x_] := 3 x + 2
In[4]:= f[1]
Out[4]= 5
In[5]:= f[f[1]]
Out[5]= 17
In[6]:= f[f[f[1]]]
Out[6]= 53
In[8]:= ex2 = NestList[f, 1, 20]
Out[8]= {1, 5, 17, 53, 161, 485, 1457, 4373, 13121, 39365, 118097,
354293, 1062881, \
3188645, 9565937, 28697813, 86093441, 258280325, 774840977, 2324522933, \
6973568801}
On Tuesday, March 19, 2002, at 03:38 PM, Bob Harris wrote:
> Howdy,
>
> I'm trying to figure out how, with a single expression, I can create a
> list
> of iterates of some sequence.
>
> For example, suppose I have defined F[x_] := 3x+2 and I want to get a
> list
> that contains F[1], F[F[1]], F[F[F[1]]], etc. It seems like I could do
> something like
>
> iterates = Table [F[ iterates[[n-1]] ] , {n,1,20}]
>
> to get a list of the first 20 iterations of this function (assuming I
> had
> some way to define interates[[1]]).
>
> Anyone know how I can do that, short of writing the following function?
>
> Iterate[func_ , first_ , iterations_] := Module[ {v, ix},
> v = Table[0, {n, 1, iterations}];
> v[[1]] = first;
>
> For[ix=2, ix<=iterations, ix++, v[[ix]] = func[v[[ix-1]]] ];
>
> v
> ]
>
> Any help would be appreciated. I'm mostly interested in being pointed
> in
> the right direction.
>
> Thanks,
> Bob H
>