1
$\begingroup$

This question already has an answer here:

These two methods should give the same result, but one terminates when the result is unchanged (to machine precision) while the other continues for the full 100 allowed cycles. Why?

x = -.01;
FixedPointList[(#^2 + x) &, x, 100]

(*  {-0.01, -0.0099, -0.00990199, -0.00990195, -0.00990195, -0.00990195, 
     -0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195}  *)

FixedPointList[(#^2 + y) &, y, 100] /. y -> -.01

(* {-0.01, -0.0099, -0.00990199, -0.00990195, -0.00990195, -0.00990195, 
    -0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195, 
    -0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195, 
    ...
    -0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195, 
    -0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195}*)
$\endgroup$

marked as duplicate by Mr.Wizard Feb 19 '16 at 5:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    $\begingroup$ No they should not. The order of evaluation is not the same. In your second example, the specific numerical value for y gets substituted only at the very end, and this leads to FixedPointList being evaluated completely differently. I described a similar issue here. $\endgroup$ – Leonid Shifrin Feb 18 '16 at 22:49
  • $\begingroup$ This is a bit like asking why putting your socks on after your shoes looks different from the usual. $\endgroup$ – J. M. is away Feb 19 '16 at 4:00
  • $\begingroup$ This question did not arise from a syntax, capitalization, spelling, or typographical error. The answer is only easy to find in the documentation if you already know the answer is "order of operations" and search on that. $\endgroup$ – Jerry Guern Feb 19 '16 at 4:37
  • $\begingroup$ This is a reasonable even if beginner-ish question. Others will have this question, and @Edmund gave a great succinct explanation in his answer below that they'll find helpful. $\endgroup$ – Jerry Guern Feb 19 '16 at 4:37
6
$\begingroup$

Have a look at the Outline of operators in order of decreasing precedence table in The Syntax of the Wolfram Language guide. There you will see that Pattern and rule operators are much further down the list than Function application variants. Therefore, FixedPointList is evaluated before ReplaceAll (/.) which leads to the 100 entries. Consider the steps that follow.

Run the following:

FixedPointList[(#^2 + y) &, y, 15]

This evaluates to the list of 15 iterations of the function such that

FixedPointList[(#^2 + y) &, y, 15] /. y -> -.01

is replacing the ys in the list.

Now run the following:

exp = Hold[FixedPointList][(#^2 + y) &, y, 15] /. y -> -.01

Here FixedPointList is stopped from executing before the replacement such that

ReleaseHold@exp

runs the FixedPointList with y replaced.

Also with Inactive and Activate.

exp = Inactive[FixedPointList][(#^2 + y) &, y, 15] /. y -> -.01

then

Activate@exp

Hope this helps.

$\endgroup$
  • $\begingroup$ Thank you! I never actually understood what the heck anyone would use Hold[] for, now I see I need it. $\endgroup$ – Jerry Guern Feb 19 '16 at 4:41
  • $\begingroup$ @JerryGuern Unevaluated is usually a cleaner way to handle this case, e.g. (30193) as well as the question now linked in the "already has an answer" header above yours. $\endgroup$ – Mr.Wizard Feb 19 '16 at 5:34
  • $\begingroup$ @Mr.Wizard Yes, I see your point that the other question has the same answer as mine, but I don't think that makes the questions "duplicates". A non-expert with MY question would not realize (based on the title) that 30193 has his answer. I'm not just defending my precious question here, I'm trying to keep this site accessible to bewildered noobs. $\endgroup$ – Jerry Guern Feb 19 '16 at 5:46
  • 1
    $\begingroup$ @Jerry This is actually how the "duplicate" system is supposed to work; your question provides a way for people to find (through searching, etc.) a question similar to their own, then it links them to an "original" with multiple hopefully-useful answers. "Duplicate" doesn't mean "stupid noob, search harder next time" but rather "here is another question that was hard to find that has answers in common; now your question will make it easier to find for everybody else." :-) $\endgroup$ – Mr.Wizard Feb 19 '16 at 6:11
  • $\begingroup$ @Mr.Wizard Hm. Ok, well thank you for that explanation. $\endgroup$ – Jerry Guern Feb 19 '16 at 6:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.