Re.Iterations.. (function composition)
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re.Iterations.. (function composition)
- From: gaylord at ux1.cso.uiuc.edu
- Date: Thu, 23 Apr 1992 16:40:05 -0500
spagiola at leland.stanford.edu writes:
I'm looking for a way to achieve the following result a little more
elegantly:
a[s]
g[%]
a[%]
g[%]
a[%]
g[%]
...
You get the idea. I have a function a[] that takes an initial value
of s as its argument. A second function g[] takes the result of a[]
and modifies the value of s, which then passes back to a[], etc. (If
you're curious, this is for a natural resource model in which current
actions affect the resource stock, which in turn affects future
actions.)
I'm sure this is quite simple and that I'm just missing something
obvious, but after spending several hours playing with Do[],
NestList[], FoldList[] etc, I cannot seem to get what I want.
I also need to recover every value, so that i can plot a[] and s[]
(separately). I'd also like to be able to either specify the number
of iterations or let it converge to a point where it no longer
changes.
=================================
it looks like you want to compose a and g (a functional programming thing).
eg.,
Nest[Composition[g, a], s, 3]
g[a[g[a[g[a[s]]]]]]
NestList[Composition[g, a], s, 3]
{s, g[a[s]], g[a[g[a[s]]]], g[a[g[a[g[a[s]]]]]]}
note that this gives g[a[g[a[s]]]] but not a[g[a[s]]] but it doesn't look
too hard to get the latter (i would try it now but i will be killed if i
don't come to dinner Immediately)
see p.213 in the bible for more detail (it might be useful to read a book
on functional programming as well ( eg., Chris Reade).
btw - for convergence, you can of course use FixedPointList in the same
construction.
got to go or my wife will not only name my copy of Mathematica as
corespondent in the divorce but probably ask for custody of both it and my
Mac.