Re: question about nest
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1950] Re: [mg1940] question about nest
- From: Tom Wickham-Jones <twj>
- Date: Sat, 26 Aug 1995 00:09:35 -0400
>Dear Wizards
> I have a function with two variables
>f[x_,a_]:=a*x*(1-x)
>And I need the n-compose function
>Nest[f,x,n]
>But Mathematica look for a function f[x] and not f[x,a]
>Is this possible to use Nest with this two variable function?
>For example, I need
>f[x,a], f[f[x,a],a], f[f[f[x,a],a],a] .
>Later I will put many values for "a" .
>Regards
>Orlando Peres
This is exactly what Fold and FoldList do.
In[7]:= FoldList[ f, x, {1,2,3,4}]
Out[7]= {x, f[x, 1], f[f[x, 1], 2], f[f[f[x, 1], 2], 3],
> f[f[f[f[x, 1], 2], 3], 4]}
Here is a random walk generated with FoldList.
In[8]:= FoldList[ Plus, {0,0}, Table[ Random[]-0.5, {10},{2}]]
Out[8]= {{0, 0}, {0.458786, -0.383977}, {0.032343, -0.554805},
> {-0.369194, -0.792845}, {-0.0154305, -0.880452}, {-0.259649, -1.08451},
> {0.0299646, -0.716247}, {-0.242232, -0.227537}, {-0.441665, 0.241432},
> {0.034376, 0.0249576}, {-0.0614994, -0.211328}}
In[9]:= ListPlot[%, PlotJoined -> True]
Out[9]= -Graphics-
Tom Wickham-Jones
WRI