I'm not sure to understand the question. You want something dynamic or something like that :
(I've another version with fp and it's possible to use gnuplot with tikz)
update I clean the code
\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,fullpage}
\usetikzlibrary{arrows}
\begin{document}
\thispagestyle{empty}
\begin{figure}[htbp]
\centering
\newcounter{j}
\begin{tikzpicture}[scale=10,>=latex']
\draw[color=blue,samples at={0,0.01,...,1.07}] plot (\x,{cos(\x r)});
\draw[color=green](0,0)--(1,1);
\draw[->](0,0)--(0,1) node[above]{$y$};
\draw[->](0,0)--(1,0) node[right]{$x$};
\newcommand{\x}{.2}
\foreach \i in {1,...,7}{%
\pgfmathcos{\x r}
\let\y\pgfmathresult
\draw[color=magenta](\x,\x)--(\x,\y)--(\y,\y);
\draw[color=orange,dotted,line width=0.8pt]%
(\x,\x)--(\x,0) node[below=8pt]{$u_\i$};
\pgfmathsetcounter{j}{\i+1}
\draw[color=blue,dotted,line width=0.8pt]%
(\x,\y)--(0,\y) node[left=8pt] {$u_\thej$};
\global\let\x\y}
\end{tikzpicture}
\caption{$f(x)=\cos (x)$}
\end{figure}
\end{document}

with piecewise
I updated my solution because I discovered a big problem with pgfmathdeclarefunction. The problem was simple : Bad syntax :
\pgfmathdeclarefunction{p}{1}{% % I keep blank spaces between the brace and %
\pgfmathparse{#1>0.5 ? 1 : 2*#1 }}
I found another solution with declare function but with the correction the code is fine with pgfmathdeclarefunction.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\thispagestyle{empty}
\begin{figure}[htbp]
\centering
\newcounter{j}
\begin{tikzpicture}[>=latex',scale=10,
declare function={%
p(\t)= greater(\t,0.5) ? 1 : 2* \t ;}]
\draw[color=blue,domain=0:1.2] plot (\x,{p(\x)});
\draw[color=green](0,0)--(1.2,1.2);
\draw[->](0,0)--(0,1) node[above]{$y$};
\draw[->](0,0)--(1,0) node[right]{$x$};
\newcommand{\x}{.1}
\foreach \i in {1,...,4}{%
\pgfmathparse{(p(\x)}
\let\y\pgfmathresult
\draw[color=magenta](\x,\x)--(\x,\y)--(\y,\y);
\draw[color=orange,dotted,line width=0.8pt]%
(\x,\x)--(\x,0) node[below=8pt]{$u_\i$};
\pgfmathsetcounter{j}{\i+1}
\draw[color=blue,dotted,line width=0.8pt]%
(\x,\y)--(0,\y) node[left=8pt] {$u_\thej$};
\global\let\x\y}
\end{tikzpicture}
\caption{if $x\leq 1$ then $ f(x)= 2x$ else $ f(x)= 1$}
\end{figure}
\end{document}
lua*because I've never used it. IIRC Metapost is also very sensitive to rounding errors. – Mar 24 '12 at 11:43