# Iteration of a function

I have a function $f(x)=x^7+k$ I want to iterate $xn+1=g(xn)$, 50 times starting with $x0=1$ and keep the last 10 values

I want to assign these values to a function y and be able to use y to calculate this iteration for any input constant $k$, eg $k=0$, $k=0.1$, then store all the corresponding 10 values I have extracted for $k=0$, $k=0.1$, up to 1 in a table. Is this possible? I can't figure it out.

I have only done this and I am stuck: Take[NestList[f, 0, 50], -10]

-
That thing you have overflows. Are you sure you want to iterate that? – J. M. May 4 '12 at 3:23

J.M.'s comment points you in the direction of why this doesn't work. Iterating $x^7$ 50 times (even if $k=0$) is $(x^7)^{50}$.

(x^7^50)

(* x^1798465042647412146620280340569649349251249 *)

That exceeds the maximum number representable in Mathematica:

In[1]:= $MaxNumber Out[1]= 5.297557459040040*10^323228467 Even if we considered$x^2 + k$instead of$x^7 + k$, it will still overflows. ff[x0_, k_] := NestList[ #1^2 + k &, x0, 50] In[10]:= ff[1., 0.1] During evaluation of In[10]:= General::ovfl: Overflow occurred in computation. >> Out[10]= {1., 1.1, 1.31, 1.8161, 3.39822, 11.6479, 135.773, 18434.5, 3.39832*10^8, 1.15486*10^17, 1.33369*10^34, 1.77873*10^68, 3.16389*10^136, 1.00102*10^273, 1.002046001003433*10^546, 1.004096188126972*10^1092, 1.008209155011116*10^2184, 1.016485700248229*10^4368, 1.033243178809133*10^8736, 1.067591466555603*10^17472, 1.139751539462343*10^34944, 1.299033571706781*10^69888, 1.687488220421276*10^139776, 2.847616494060564*10^279552, 8.108919697245777*10^559104, 6.575457865638055*10^1118209, 4.323664614278137*10^2236419, 1.869407569676091*10^4472839, 3.494684661562269*10^8945678, 1.221282088375859*10^17891357, 1.491529939387699*10^35782714, 2.224661560089872*10^71565428, 4.949119056941505*10^143130856, 2.449377943978157*10^286261713, Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[], Overflow[]} Of course, if you have a starting value$x0<1\$, your original function is fine, and converges quickly.

In[12]:= ff7[x0_, k_] := NestList[ #1^7 + k &, x0, 50]

In[13]:= ff7[0.5, 0.1]

Out[13]= {0.5, 0.107813, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, \
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, \
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, \
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1}

Power values below 1 are also fine:

In[16]:= ff2[x0_, k_] := NestList[ #1^0.8 + k &, x0, 50]

In[17]:= ff2[1., 0.8]

Out[17]= {1., 1.8, 2.40036, 2.81475, 3.08851, 3.2649, 3.37689, \
3.44736, 3.49147, 3.51898, 3.53611, 3.54676, 3.55338, 3.55748, \
3.56003, 3.56162, 3.5626, 3.56321, 3.56359, 3.56382, 3.56397, \
3.56406, 3.56411, 3.56415, 3.56417, 3.56418, 3.56419, 3.56419, \
3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642, \
3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642, \
3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642, 3.5642}

But in this case, I question why keeping the last ten iterations makes any sense. The function will have converged by then. It is behaviour of the first ten iterations that is more interesting.

-
On an additional note: if you really, truly, genuinely want the last ten iterates of a fifty iterate list, here's a way to do it that is rather kludgy, but has modest memory consumption: NestList[f, Nest[f, x, 40], 10]. – J. M. May 4 '12 at 4:37