# Cobweb diagram has offset iterates / modifying a working piecewise functions results in chaos

I have a problem with the following tikz code (maybe, when reading this, skip at first the code and read the question which is at the end), that creates cobweb-diagrams:

 \documentclass{article}
\usepackage{tikz, fullpage}
\usetikzlibrary{arrows}
\begin{document}
\thispagestyle{empty}
\begin{figure}[htbp]
\centering
\newcounter{j}

\pgfmathdeclarefunction{p}{1}{
\pgfmathparse{#1 > 0.5 ? 1 : ( #1 >0.25 ? 0.2 : 2* #1 )}   %change
}

\begin{tikzpicture}[>=latex',scale=10]
\draw[color=blue,style=thick,domain=0:1] plot (\x,{p(\x)});   %change
\draw[color=green](0,0)--(1,1);    %change
\draw[->](0,0)--(0,1) node[above]{$y$};
\draw[->](0,0)--(1,0) node[right]{$x$};
\newcounter{cnt}
\newcommand{\x}{.3}    %change
\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{description} %change
\end{figure}
\pgfmathdeclarefunction{p}{1}{
\pgfmathparse{(and(#1>0, #1<1))}
}

\end{document}


I asked a while ago this question, about how to create such diagrams and I got some very good answers out of which I accepted one which uses tikz to do that. Now the problem is: If I compile the code of the piecewise diagram, of the accepted answer it looks fine (just like the graphic of the corresponding code from the accepted answer). But if I change that code only slightly (I wrote %change to the lines, where I did changes; please notice that I know almost nothing about tikz, so I just changed that code at various places, observed what that did and thus shaped that code into the present one to suit my needs) the output looks broken/terrible:

The most sensible change I believe is the one, where I'm changing my piecewise function (since all the other changes involve only changing the domain of the plot, the number of iterations of the "cobwebs" and the point of the domain at which the iterations start) from

 \pgfmathdeclarefunction{p}{1}{%
\pgfmathparse{#1>0.5 ? 1 : 2*#1 }%
}


to

 \pgfmathdeclarefunction{p}{1}{
\pgfmathparse{#1 > 0.5 ? 1 : ( #1 >0.25 ? 0.2 : 2* #1 )}
}


since all the other changes of the original code from here worked with the original piecewise function.

The piecewise function I define still looks ok in the above plot (meaning the definition of the function corresponds to its plot), but the problem are the magenta iterates together with the orange and blue projections, which are somehow offset - and the plot itself isn't centered anymore. Please help me with the question: How can I make the iterates/the plot look ok again ?

(Interestingly, if I compile the document such that a *.ps one comes out instead a *.pdf one (I'm using texmaker -> the above output was compiled using "pdflatex" and then -"view pdf") the iterations aren't offest anymore and the plot is centered, but all the projections are suddenly very much offset to the right)

-
There are several problems. 1) your definition of the piecewise function is strange #1 > 0.5 ? 1 : ( #1 >0.25 ... 2) more important when we draw the function, the graph seems to be outside the bounding box. Actually I don't have an answer to this problem :( I update my code in my other answer because I keep some useless code . –  Alain Matthes Mar 24 '12 at 10:41

Your problem comes from the definition of p. You forgot a % after the brace and I think it's #1 < 0.25 instead of #1 > 0.25 but perhaps you can give the mathematical definition. If you take the next definition, you will get the same graph.

 \pgfmathdeclarefunction{p}{1}{%
\pgfmathparse{#1 > 0.5 ? 1 : ( #1 < 0.25 ? 0.2 : 2* #1 )}   %change
}


Perhaps a solution

\documentclass{article}
\usepackage{tikz, fullpage}
\usetikzlibrary{arrows}
\begin{document}
\thispagestyle{empty}

\newcounter{j}

\begin{tikzpicture}[>=latex',scale=10,%
declare function={%
p(\t)=  greater(\t,0.5)  ? 1 : ( less(\t,0.25) ? 0.2 : 2* \t );}]
\draw[color=blue,style=thick,domain=0:1] plot (\x,{p(\x)});   %change
\draw[color=green](0,0)--(1,1);    %change
\draw[->](0,0)--(0,1) node[above]{$y$};
\draw[->](0,0)--(1,0) node[right]{$x$};
\newcounter{cnt}
\newcommand{\x}{.3}    %change
\foreach \i in {1,...,2}{
\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}

\end{document}


-
Thanks, your a lifesaver!! One thing I still don't understand though: When I defined the function #1 > 0.5 ? 1 : ( #1 >0.25 ? 0.2 : 2* #1 )} I didn't think much about it: I just entered it and looked at the plot. But now it occured to me, that this function isn't even continuous. Even the way you described the function doesn't make it continous. How come that tikz makes it continuous by creating connecting lines ? Shouldn't in the function above be a discontinuous point at 0.25 where the functions jumps from 0.2 to 0.5 ? Would there be a way to supress this ? –  user10324 Mar 24 '12 at 12:19
I know there is some problems with discontinuous functions but I think there is a solution. Perhaps on tex.stackexchange or on the web ,I don't have enough time actually to search that but I can search an answer. What is exactly the function that you want draw ? I prefer a mathematical definition ! –  Alain Matthes Mar 24 '12 at 12:35
I don't have a specific function in mind, since I don't need to plot discontinuous functions right now. I just asked out of mere curiosity. From what you are sying it doesn't seems that I can just switch your function, with one I someday would like to plot, so there is no point right now, I think, to ask you how to do that (conversely,, if I can just switch the function you plotted with a different one, I would like to see the floor funtion (as described on wikipedia)). If I do have to plot a discontinuous function sometime, I'll post a question. –  user10324 Mar 24 '12 at 15:26