# Iterating a function

I have a piecewise map of the form

$M\left ( x \right )=\left\{\begin{matrix} 2x & x<0.5 \\ 2-2x& x>0.5 \end{matrix}\right.$

The domain is the closed interval from 0 to 1.

I would like to iterated this map such that at some x value, f(x)=x. The result is then printed and the iteration stops.

• Why don't you just use Solve? I.e. f[x_] := Piecewise[{{2 x, x < 0.5}, {2 - 2 x, x >= 0.5}}]; Solve[ f[x] == x, x] Commented May 21, 2017 at 8:00

I think what you need is NestWhileList

f[x_] = Piecewise[{{2 x, x < 0.5}, {2 - 2 x, x >= 0.5}}, {x, 0, 1}];
fc[n_] := NestWhileList[f, n, # != f[#] &]


For n = 0.4, fc[0.4] returns

{0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, \ 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, \ 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.8, 0.4, 0.799999, 0.400002, 0.800003, \ 0.399994, 0.799988, 0.400024, 0.800049, 0.399902, 0.799805, 0.400391, \ 0.800781, 0.398438, 0.796875, 0.40625, 0.8125, 0.375, 0.75, 0.5, 1., \ 0.}

Do you need the result or the process of iterations? If the first, then:

f = Piecewise[{{2 x, x < 0.5}, {2 - 2 x, x >= 0.5}}, {x, 0, 1}];
FindInstance[f - x == 0 && 0 <= x <= 1, x, 5]
{{x -> 0}, {x -> 2/3}}


Otherwise you need look how to use the EvaluationMonitor with FindInstance

• By "process of iteration", do you mean the Print function? If, yes, I could do that. As to your proposed solution, would using Conditionals and Loops be more handy? Edit: I think I see what you're asking. I can see that x=2/3 is a fixed point on a map. But what I want to achieve is to start with a value x, find the image of x under the function, put that image value back into the function and repeat until f(x)=x, at which point the loop breaks. Commented May 21, 2017 at 6:14
• My solution directly gives you the points where f(x)=x with \$MachinePrecision. There are two such points in the range [0,1] as you can see. Any hand-made iterative solutions need to play with x-step adjustment. I asked you about what exactly do you need namely to understand what is your goal - to realize the process of iterations or to find the stop-point. Commented May 21, 2017 at 10:46