Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
FixedPoint[If[# <= 0, 0, # - 1]&, 2 x]

Causes an infinite loop because of 'x'. How do i automatically prevent evaluation until 2 x is known? I have a similar function being fed into Plot[] and it has control over when x is known.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

This question leaves us all guessing, but my guess is that you want a combination of the answers already given. I think you want to test the second argument of FixedPoint and only evaluate when this is numeric. You could use:

fp[function_, start_?NumericQ] := FixedPoint[function, start]

Now this halts evaluation:

expr = fp[If[# <= 0, 0, # - 1] &, 2 x]
fp[If[#1 <= 0, 0, #1 - 1] &, 2 x]

But after x is given a numeric value:

x = 5;
expr
0

Interaction with Plot with a modified function:

Plot[fp[If[# <= 0, #, # - 1] &, 2 z], {z, 0, 3}]

enter image description here

share|improve this answer
add comment

Your use-case is not really clear. When you simply define a function from your expression, you can control that it is called with any expression which makes sense

myFixedPoint[x_] := FixedPoint[If[# <= 0, 0, # - 1] &, 2 x]

Let's say now you have an expression for x which doesn't end in an infinite loop, you can call

y = 3;
myFixedPoint[7 y]

(* 0 *)
share|improve this answer
add comment

How about

ClearAll[x]
f[x_] := FixedPoint[If[# <= 0, 0, # - 1] &, 2 x ];
myFixedPoint = Function[x, If[NumberQ[x], f[x], Undefined]];

myFixedPoint[x]
myFixedPoint[123.4]
x = 123.4
myFixedPoint[x]

with output

Undefined
0
123.4
0
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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