2

I am trying to show the graph of the sequence u(n+1)=f(u(n)) for some function f. More specifically the goal is a graph like the following one :

reccurence graph

So far I have most of it, but I'm not sure how to properly define the different coordinates other calculating each term by hand to plot it, but other than that I'm not sure what to do. So here's what i got :

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfmathdeclarefunction{function}{1}{\pgfmathparse{1+1/(#1)}}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin = 0,
        ymax = 4,
        xmin = 0,
        xmax = 5,
        axis x line=bottom,
        axis y line = left,
        axis line style={->},
        xtick = {0,1,2,3,4},
        ytick = {1,2,3},
        ylabel = $y$,
        xlabel = $x$
        ]

    \addplot[domain=0:5, samples = 250, color=red]{function(\x)}
        node [pos=0.97, above] {$f(x)$};
    \addplot[domain=0:5, samples = 250]{\x}
        node [pos=0.6, below right] {$y=x$};
    \end{axis}
\end{tikzpicture}
\end{document}

Which looks like this :

enter image description here

So I'd appreciate any help on how to add the relevant points to make the spirally figure on the first pic.

2 Answers 2

2

It is very easy to recursively define functions. This can be done with the math library, and a prominent example is the one of Fibonacci numbers (p. 704 of pgfmanual v3.1.5). This has been used in this answer. When I implement your function in the recursion I get a somewhat different output, though.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{math}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[evaluate={function myfun(\n) {
    if \n == 1 then { return 1;
    } else {
    return 1+1/myfun(\n-1);
    }; };}]
    \begin{axis}[
        ymin = 0,
        ymax = 4,
        xmin = 0,
        xmax = 5,
        axis x line=bottom,
        axis y line = left,
        axis line style={->},
        xtick = {0,1,2,3,4},
        ytick = {1,2,3},
        ylabel = $y$,
        xlabel = $x$
        ]

    \addplot[domain=0:5, samples = 250]{\x}
        node [pos=0.6, below right] {$y=x$};
    \addplot[domain=0:5, samples at={1,2,...,4}, color=red] {myfun(\x)}
        node [pos=0.97, above] {$f(x)$};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

EDIT: After seeing your own answer I think I got it.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{math}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[evaluate={function myfun(\n) {
    if \n == 1 then { return 1;
    } else {
    return 1+1/myfun(\n-1);
    }; };}]
    \begin{axis}[
        ymin = 0,
        ymax = 4,
        xmin = 0,
        xmax = 5,
        axis x line=bottom,
        axis y line = left,
        axis line style={->},
        xtick = {0,1,2,3,4},
        ytick = {1,2,3},
        ylabel = $y$,
        xlabel = $x$
        ]

    \addplot[domain=0:5, samples = 250]{\x}
        node [pos=0.6, below right] {$y=x$};
    \addplot[domain=0:5, samples at={1,2,...,10}, color=red] 
    ({myfun(int(\x/2+1/2))},{myfun(int(\x/2+1))})
        node [pos=0.97, above] {$f(x)$};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • Yes, indeed that's what I was going for, thanks!
    – t_kln
    Mar 8, 2020 at 22:05
1

Ok, so after a while I came up with a solution, using pgfmathsetmacro to set the coordinates. So here is the solution :

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfmathdeclarefunction{function}{1}{\pgfmathparse{1+1/(#1)}}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
     ymin = 0,
ymax = 3,
xmin = 0,
xmax = 3,
axis x line=bottom,
axis y line = left,
axis line style={->},
xtick = {0,1,2},
ytick = {1,2},
ylabel = $y$,
xlabel = $x$
]
    \addplot[domain=0:3, samples = 250, color=blue]{function(\x)}
        node [pos=0.991, above right] {$f(x)$};
    \addplot[domain=0:3, samples = 250]{\x}
        node [pos=0.8, above left] {$y=x$};

    \def\xa{0.55}
    \pgfmathsetmacro{\ya}{function(\xa)}
    \path (axis cs:\xa, \ya) coordinate (0);
    \path (axis cs:\ya, \ya) coordinate (1);
    \path (axis cs:\xa, 0) coordinate (2);

    \pgfmathsetmacro{\yb}{function(\ya)}
    \path (axis cs: \ya, \yb) coordinate (3);
    \path (axis cs: \yb, \yb) coordinate (4);
    \path (axis cs: \ya, 0) coordinate (10);

    \pgfmathsetmacro{\yc}{function(\yb)}
    \path (axis cs: \yb, \yc) coordinate (5);
    \path (axis cs: \yc, \yc) coordinate (6);
    \path (axis cs: \yb, 0) coordinate (7);

    \pgfmathsetmacro{\yd}{function(\yc)}
    \path (axis cs: \yc,\yd) coordinate (8);
    \path (axis cs: \yd,\yd) coordinate (9);
    \path (axis cs: \yc,0) coordinate (11);

\end{axis}
    \draw [color=red] (2) -- (0) -- (1) -- (3) -- (4) -- (5) -- (6) -- (8) -- (9);
    \draw [dashed, color=red] (0) -- (2) node[below] {$u_0$} (3) -- (10) node[below] {$u_1$};
    \draw [dashed, color=red] (5) -- (7) node[below] {$u_2$} (8) -- (11) node[below] {$u_3$}; 
\end{tikzpicture}
\end{document}

EDIT: I added the last two lines of the code above, I forgot to paste them the first time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .