# Creating cobweb diagrams of some functions with tikz/pstrick etc

I want to create cobweb diagrams with tikz (if that is possible - but reading in the internet about all the things tikz can do, I would believe it is) - or pstricks or some other graphing program - of some fairly simple functions. For an illustration of what I have in mind, see the pictures from this site. My problem is that my approach to this problem was totally wrong: I did all my cobweb diagrams in GeoGebra, because from there I can export them to tikz/pstricks - but after doing that I realized that that doesn't work very well: The conversion from the GeoGebra plot to tikz/pstricks often contains glitches.

So now I did all the work for nothing - and on top I don't even know tikz good enough to do that just by code. So could you please give me on example of how to code one cobweb diagram of one function (for simplicities sake, lets take x^2) that I can afterwards adapt for my other functions ? It should have to parameters: One with which I can tell it at which point of the function it tikz how many iterations it should do from that point on.

-
It's usually a good idea to show what you have tried so far, however small that might be –  cmhughes Feb 24 '12 at 20:35
I'm not sure if TikZ is the right hammer for this nail in general. Even for simple calculations you get rounding errors, which multiply themselves after repeated applications. It seems this is something you'd want to avoid for applications like this. –  Marc van Dongen Feb 24 '12 at 22:07
@MarcvanDongen: +1. Maybe ConTeXt/Metapost/lua would be a better tool here? (Or even LuaLaTeX + tikz?) –  mbork Mar 24 '12 at 11:00
@mbork I don't know about lua* because I've never used it. IIRC Metapost is also very sensitive to rounding errors. –  Marc van Dongen Mar 24 '12 at 11:43
@MarcvanDongen: lua is a "normal" programming language, and has a "double" float type. Also, Metapost will soon have a reasonable numeric engine. –  mbork Mar 24 '12 at 15:14

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}

-
Thanks; I even found this example on your site ;) You can do really cool things with tikz...Can you give me a last hint, how use that code with a piecewise defined function ? I managed to find all the parameters in the code, that modify how many iterations it does, which function is the blue one etc., but I don't know how to define the blue function as a piecewise one ( for example 2x on [0,0.5) and 1 on [0.5,1] ). –  user10324 Feb 25 '12 at 18:25
@user10324 Finally I had some minutes to do this ... –  Alain Matthes Feb 27 '12 at 8:42

Try looking into Sage, specifically the Sage Interact manipulatives. Someone has posted a cobweb example here. It allows you to enter the functions you want, choose the iterations and so on. I've modified the code on the link above so that it outputs to a PDF file.

def cobweb(a_function, start, mask = 0, iterations = 20, xmin = 0, xmax = 1):
'''
Returns a graphics object of a plot of the function and a cobweb trajectory starting from the value start.

INPUT:
a_function: a function of one variable
start: the starting value of the iteration
mask: (optional) the number of initial iterates to ignore
iterations: (optional) the number of iterations to draw, following the masked iterations
xmin: (optional) the lower end of the plotted interval
xmax: (optional) the upper end of the plotted interval

EXAMPLES:
sage: f = lambda x: 3.9*x*(1-x)
sage: show(cobweb(f,.01,iterations=200), xmin = 0, xmax = 1, ymin=0)

'''
basic_plot = plot(a_function, xmin = xmin, xmax = xmax)
id_plot = plot(lambda x: x, xmin = xmin, xmax = xmax)
iter_list = []
current = start
current = a_function(current)
for i in range(iterations):
iter_list.append([current,a_function(current)])
current = a_function(current)
iter_list.append([current,current])
cobweb = line(iter_list, rgbcolor = (1,0,0))
return basic_plot + id_plot + cobweb
var('x')
@interact
def cobwebber(f_text = input_box(default = "3.8*x*(1-x)",label = "function",   type=str), start_val = slider(0,1,.01,.5,label = 'start value'), its = slider([2^i for i in range(0,12)],default = 16, label="iterations")):
C = Graphics()
def f(x):
return eval(f_text)
C = cobweb(f, start_val, iterations = its)
C.show()
C.save("Cobweb.pdf")


You can set up a free account for Sage here. Copy and paste the code into a cell, press Shift-Enter at the same time and your manipulative is created. You can play around (interact) with the parameters until you get the diagram you want. The output from running the code was this:

Having a manipulative to create your graphics will dramatically speed up the process of creating cobweb graphs.

-