Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Strange Nest behaviour with functions

  • To: mathgroup at
  • Subject: [mg114130] Re: Strange Nest behaviour with functions
  • From: Leonid Shifrin <lshifr at>
  • Date: Wed, 24 Nov 2010 07:01:01 -0500 (EST)

Hi Istvan,

Nest auto-compiles after a certain number of iterations, when it can. This
is governed by "NestCompileLength"
sub - option in "CompileOptions" in SystemOptions. The default happens to be
exactly 100. Pure function is
compilable, while rule-based is not.  If you reset "NestCompileLength" so
that Nest does not Compile, you get
the same results

In[156]:== SetSystemOptions["CompileOptions"->{"NestCompileLength"->101}];
d1==Nest[r # (1-#)&,Range[0,1,0.2],100]

Out[158]== {0.,0.899672,0.456409,0.456409,0.899672,0.}
Out[159]== {0.,0.899672,0.456409,0.456409,0.899672,0.}
Out[160]//NumberForm== 16.0005053845166
Out[161]//NumberForm== 16.0005053845166
Out[162]== True

Notice that the accuracies are the same here, which gives the hint to why
results are the same. Your problem is numerically unstable, which can be
by using the extended precision for it (I returned back to the defaults for
 "NestCompileLength" ):

In[173]:== SetSystemOptions["CompileOptions"->{"NestCompileLength"->100}];
r0 == 36/10;
d1==Nest[r # (1-#)&,Range[0,1,0.2],100]
d3 == Nest[func[N[r0,50],#]&,N[Range[0,1,2/10],50],100]
d4 == Nest[func[N[r0,80],#]&,N[Range[0,1,2/10],80],100]
Out[176]== {0.,0.899672,0.456407,0.456408,0.899672,0.}
Out[177]== {0.,0.899672,0.456409,0.456409,0.899672,0.}
Out[178]== {0,0.*10^377,0.*10^494,0.*10^550,0.*10^656,0.*10^637}
Out[180]//NumberForm== 16.0005053848732
Out[181]//NumberForm== 16.0005053845166
Out[182]== False

You can see that now the accuracies of d1 and d2 are not exactly the same.

Hope this helps.


2010/11/23 Istv=E1n Zachar <zac at>

> Dear Group,
> could someone please explain why Nest yields different results for
> pure and non pure functions for certain parameter values? The first
> example (with r == 3.5) produces the same lists, while the second (with
> r == 3.6) fails to do that. Should pure and non-pure functions differ
> in this aspect? For larger values of r, the divergence is even more
> pronounced.
> In[60]:== func[r_,x_]:==r x (1-x);
>                r==3.5;
>                d1==Nest[r # (1-#)&,Range[0,1,0.2],100]
>                d2==Nest[func[r,#]&,Range[0,1,0.2],100]
>                d1====d2
> Out[62]== {0.,0.826941,0.38282,0.38282,0.826941,0.}
> Out[63]== {0.,0.826941,0.38282,0.38282,0.826941,0.}
> Out[64]== True
> In[65]:== r==3.6;
>                d1==Nest[r # (1-#)&,Range[0,1,0.2],100]
>                d2==Nest[func[r,#]&,Range[0,1,0.2],100]
>                d1====d2
> Out[66]== {0.,0.899672,0.456407,0.456408,0.899672,0.}
> Out[67]== {0.,0.899672,0.456409,0.456409,0.899672,0.}
> Out[68]== False

  • Prev by Date: Re: Finding shared element in two lists
  • Next by Date: Re: Plot function at specific points
  • Previous by thread: Strange Nest behaviour with functions
  • Next by thread: Documentation Center cannot be launched