[Tex/LaTex] How to plot a bifurcation graph

graphstikz-pgf

I am trying to make just a general picture that looks like this one:

enter image description here

It doesn't have to look exactly like this, but just look like a general bifurcation in similar spirit to this one.

And here is the code I tried:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{luacode}
\begin{luacode*}
  function logistic()

    local function map(r,x)
      return r*x*(1-x)
    end

    for r = 2.5,4,0.005 do
      x = 0.1
      for i = 1, 200 do
        x = map(r,x)
      end
      for i = 1, 250 do
        x = map(r,x)
        tex.sprint("("..r..","..x..")")
      end
    end
  end
\end{luacode*}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[tick label style={font=\tiny}]
    \edef\logisticplot{\noexpand\addplot [color=black!10, mesh, only marks,
      mark size = 0.05pt, opacity = 0.1] coordinates{ \directlua{logistic()} };}
    \logisticplot
  \end{axis}
\end{tikzpicture}
\end{document}

I found the code on this website, but I don't understand this language so I can't figure out why the code isn't working: http://texwelt.de/wissen/fragen/7097/wie-kann-man-einen-iterativen-plot-eleganter-schreiben

Best Answer

Here is solution provided with a bit of research, minor code modification, and implemented using R and knitr with LaTeX.

\documentclass{standalone}

\begin{document}
%Reference: \verb+http://www.magesblog.com/2012/03/logistic-map-feigenbaum-diagram.html+

<<echo=FALSE,out.width='6in'>>=
logistic.map <- function(r, x, N, M){
  ## r: bifurcation parameter
  ## x: initial value
  ## N: number of iteration
  ## M: number of iteration points to be returned
  z <- 1:N
  z[1] <- x
  for(i in c(1:(N-1))){
    z[i+1] <- r *z[i]  * (1 - z[i])
  }
  ## Return the last M iterations 
  z[c((N-M):N)]
}

## Set scanning range for bifurcation parameter r
my.r <- seq(2.5, 4, by=0.003)
Orbit <- sapply(my.r, logistic.map,  x=0.1, N=1000, M=300)

Orbit <- as.vector(Orbit)
r <- sort(rep(my.r, 301))

plot(Orbit ~ r, pch=".")
@
\end{document}

enter image description here

Related Question