(*Adapted by Rafael Luís: rafaelluis@netmadeira.com
 2016-09-11: adapted for Mathematica 10 by Ross Moore *)

(*Cobweb diagram for Tent Map*)

(*Function and parameters*)

Clear[c,x,x0,iterates];
f[x_,c_]:=Which[x<0,0,(x >= 0 && x<=1/2), 2*c*x, (x >1/2&& x<=1),2*c*(1-x),x>1,0];(*Function*)
x0=0.05;     (*initial values*)
c=1;      (*parameter*)
iterates=10;

TentSegments[c_,x0_,its_]:=Module[
{l=NestList[(Which[#<0,0,(# >= 0 && #<=1/2), 2*c*#, (# >1/2&& #<=1),2*c*(1-#),#>1,0])&,x0,its]},
Partition[Flatten[Transpose[{l,l}]],2,1]
]

p2=Partition[TentSegments[c,x0,iterates],2,1];

Manipulate[Plot[{f[x,c],x},{x,0,1.1},PlotStyle->{{},Dashing[{.02}]},
Prolog->MapIndexed[{Hue[Length[#]/Length[p2]],Line[#]}&,Take[p2,2i]],
AspectRatio->Automatic,
AxesLabel->TraditionalForm/@{(Subscript[x, n]),(Subscript[x, n+1])}
],{{i,5,"iterates"},Range[Length[p2]/2]}]