# bifurc_logmap.R 
#
#  Display the bifurcation diagram for a logistic map.
#
# Copyright (c) 2011--2014 Krishna Myneni, Creative Consulting for
#   Research & Education, krishna.myneni@ccreweb.org
#
# This code is released under the GNU Lesser GPL (LGPL).
#
# Revisions:
#   2014-08-11  km  ported from Forth version
#
# References:
#
# 1. E. Ott, Chaos in Dynamical Systems, 1994, Cambridge Univ. Press,
#    see sec. 2.2, p. 32 ;


Ntr = 500      # number of transient values
Nseq = 100    # number of values in sequence to plot

r_min = 2.5 
r_max = 4.0
r_delta = 0.001

r = r_min

## logistic map
L <- function( x ) { return( r*x*(1-x) ) }

## Generate map sequence for current r, discarding transient
gen_L_s <- function( ) {
	L_s = 1:Nseq
	x = 0.5
	for (i in 1:Ntr) { x = L(x) } # first Ntr iterations are discarded
	for (i in 1:Nseq) { x = L(x); L_s[i] = x; }
	return( L_s )
}

## Draw the bifurcation diagram for the logistic map
plot(0, 0, xlim=c(r_min, r_max), ylim=c(0,  1), xlab="r", ylab="L(x)")
r_vals = seq(r_min, r_max, by = r_delta)
n = length(r_vals)

for (i in 1:n) {
	r = r_vals[i]
	y = gen_L_s()
	x = rep(r, Nseq)
	points(x, y, col="blue", cex=0.01)
}

title("Bifurcation Diagram for Logistic Map")

