# How to plot a bifurcation graph

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

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}]
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

• which engine did you compile the code with? – percusse May 16 '15 at 20:31
• @percusse Sorry, I don't know what "engine" means. I currently use MiKTeX and TeXnicCenter, and I compiled with LaTeX => PDF – mr eyeglasses May 16 '15 at 21:13
• You need to run it with LuaLaTeX -> PDF. And better decrease the loop numbers because it will take a lot of time. Start with 10,20 instead of 200,250 – percusse May 16 '15 at 22:00
• @ᴇʏᴇs you should use LuaLaTex in order to compile the tikzpicture, since it's written in Lua, the programming language. – Airman01 May 16 '15 at 22:00

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}