# How to draw a path with coordinates defined by f(x)

I'm writing about fixed point and I would like to do this image in tikz without the labels. Is there any way to define a function f such x --> x*(4-x) and a recursion

\begin{tikzpicture}
\draw (0,4) -- (0,0) -- (4,0);
\draw[dashed] (0,0) -- (4,4);
\draw[smooth,samples=100,domain=0:4] plot(\x,{(\x)*(4.0-(\x))});
\draw[dotted,->] (0.04,0) -- (0.04,f(0.04));
\foreach n \in {1,2,3,4,5}
\draw[dotted,->] (f^n(0.04),f^{n+1}(0.04))--(f^{n+1}(0.04),f^{n+1}(0.04));
\end{tikzpicture}


Edit Thanks to Andrew for the correction in the function f.

## 1 Answer

You can use \pgfmathparse (or \pgfmathsetmacro as below) to recursively compute your function as you run through the pgf \foreach loop. of course, you need to "save" the previous value as you go.

Your question seems to be using both the functions x(4-x) and x(4-x)/2, so I have stuck with the first one. It's very sensitive on the initial value of course: Here is the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,decorations.markings}
\tikzset{% style to put arrow head in middle of line
->-/.style={decoration={markings, mark=at position 0.5 with {\arrow{stealth}}},
postaction={decorate}},
}
\begin{document}

\begin{tikzpicture}
\draw[thin,->] (0,0) -- (4.5,0);
\draw[thin,->] (0,0) -- (0,4.5);
\draw[dashed] (0,0) -- (4,4);
\draw[blue,thick,smooth,samples=100,domain=0:4] plot(\x,{(\x)*(4.0-(\x))});
\def\fn{0.2}% initial value
\draw[dashed](\fn,0)node[below=1mm]{$\fn$}--(\fn,\fn);
\foreach \n in {1,...,8} {
\pgfmathsetmacro\fnn{\fn*(4-\fn)}% compute the next value
\draw[dotted,->-](\fn,\fn)--(\fn,\fnn);
\draw[dotted,->-](\fn,\fnn)--(\fnn,\fnn);
\xdef\fn{\fnn}% save value - need \xdef to force expansion
}
\end{tikzpicture}

\end{document}

• oh, so sorry, I'm going to edit, it is only x*(4-x) – Luis Felipe Oct 16 '16 at 22:35
• @LuisFelipe I just noticed your arrow heads were centered so I have added this. – user30471 Oct 16 '16 at 22:43
• wirh some math, the image is better for f(x)=2.7*x*(1-x) for 0<x<1 and for fn=0.04 – Luis Felipe Oct 16 '16 at 22:45