Message Boards Message Boards

Why doesn't FixedPointList[ ] work when starting with integer?

Posted 2 years ago

Hello,

These codes can't run in Mathematica successfully:

FixedPointList[(# + 2/#)/2 &, 1]

But these codes can:

FixedPointList[(# + 2/#)/2 &, 1.0]

May you tell me the reason?

POSTED BY: Zhenyu Zeng
10 Replies

FixedPoint is a procedural rather than mathematical function. This is quite clear from the documentation by the way, so that should have been the first place to check. As for the mathematical way to determine the exact value, Solve can be used for that.

POSTED BY: Daniel Lichtblau
Posted 2 years ago

I still don't know what is the difference between them.

POSTED BY: Zhenyu Zeng
Posted 2 years ago

WL means Wolfram Language. Mathematica is an environment for running code written in the Wolfram Language.

I'll try being even more explicit. FixedPointList repeatedly evaluates an expression, starting with a given initial value (in one case you start with 1 and in the other with 1.0), until the output starts repeating itself, i.e. it no longer changes.

Mathematica performs calculations with "infinite precision" unless something triggers it to do otherwise. It treats expressions as "exact" unless it's told to approximate them, i.e. to use finite precision. So, the expression 1/2 does not get "computed", it just sits there as a perfect representation of "one half". As soon as you indicate finite precision, your whole expression becomes "dirty" and everything gets "computed" with finite precision. So, the expression 1./2 becomes 0.5. The period adjacent to the 1 tells Mathematica to turn that 1 into a floating point representation of 1 using the standard precision. You can also do this with a backtick, so 1`20 becomes a floating point representation with 20 digits of precision.

So, try computing these examples:

1/3 // FullForm
(*should return Rational[1, 3]*)

1./3 // FullForm
(*should return 0.3333333333333333`*)

1`20/3 // FullForm
(*should return 0.33333333333333333333333333333333333333`20.*)

With floating point representations, you might "run out of digits", and so two values that "should be" different if they had exact representations can actually be Equal as floating point numbers.

Okay, so let's look at this:

FixedPointList[(# + 2/#)/2 &, 1, 4] // InputForm
(*should return {1, 3/2, 17/12, 577/408, 665857/470832}, I used InputForm so the fractions look better as plaintext, but it might be instructive to try FullForm as well*)

Notice that this sequence will never repeat. You should be able to guess that just by looking at the function. So, your expression would go into an infinite loop without the 4 which I added as the third argument (which sets a maximum iteration limit and is described in the documentation).

On the other hand, if you use floating point numbers:

FixedPointList[(# + 2/#)/2 &, 1.0] // InputForm
(*should return {1., 1.5, 1.4166666666666665, 1.4142156862745097, 1.4142135623746899, 1.414213562373095, 1.414213562373095}*)

reaches a point where the floating point representations can no longer be distinguished, as you can see if you look at the final two results.

It doesn't matter where/when the finite precision gets triggered. You get the same result with this expression:

FixedPointList[(# + 2./#)/2 &, 1]

Furthermore, this will terminate sooner:

FixedPointList[(# + 2/#)/2 &, 1`5]

And this will take longer to terminate:

FixedPointList[(# + 2/#)/2 &, 1`50]
POSTED BY: Eric Rimbey
Posted 2 years ago

Thanks for your reply. I know now. Have a nice day!

POSTED BY: Zhenyu Zeng
Posted 2 years ago

Eric, that is a very good explanation.

One could wish that the WL documentation had more of this nuts and bolts approach.

POSTED BY: Hans Milton
Posted 2 years ago

Hi Zhenyu

When you provide exact numerical input WL evaluates exact output. Compare

NestList[(# + 2/#)/2 &, 1, 6]

and

NestList[(# + 2/#)/2 &, 1., 6]

The first one will never terminate.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Hi Rohit,

May you tell me what's the WL evaluates

And may you tell me why the first wouldn't terminate?

POSTED BY: Zhenyu Zeng

(1) A function that keeps changing after every application is not going to reach a fixed point. If the evaluations produce exact results then no tolerance specification will be used.

(2) You really need to read the documentation.

(3) Also check the documentation for RSolve. It is the function to try first for determining the exact limiting value.

POSTED BY: Daniel Lichtblau
Posted 2 years ago

But why the second will reach a fixed point?

POSTED BY: Zhenyu Zeng

Check the documentation for SameQ, in particular the first item under Details and Options.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract