This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

\[x_{n+1} = 4rx_n(1-x_n)\]

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(dplyr, warn.conflicts = FALSE)
library(ggplot2)

r <- 0.2 # Constante
x <- 0.6 # CondiĂ§Ă£o inicial 
data <- data_frame(n = 0, fn = x) # 
maxIterations <- 30 # ...
f <- function(x, r){
  return(4*r*x*(1-x))
}

for(i in 2:(maxIterations+1)){
  x <- f(x,r)
  data[i,"fn"] <- x
  data[i,"n"] <- i
}

p <- ggplot(data, aes(x=n, y=fn))
p + geom_line()  + 
  geom_point(size=2) + 
  labs(x="Iteration (n)", y = bquote(f[n]), title = paste("ParĂ¢metros: r =", r, "and x[0] =",data[1,"fn"]))

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

burnout <- 500 # Jogo fora os primeiros 1000
nIteration <- 100
data <- data_frame(r = double() , x = double()) # 
seqIterations <- 1:nIteration 
for (r in seq(from = .7, to = 1, by = .001)){
  x <- .5
  for(i in 1:burnout) x <- f(x,r)
  for(j in seqIterations){
    x <- f(x,r)
    data[j,"r"] <- r
    data[j,"x"] <- x
  }
  seqIterations <- seqIterations + nIteration
}
  
q <- ggplot(data, aes(x = r, y = x))
q + geom_point(size = 1, alpha = .05, color = "blue")

library(compiler)
library(microbenchmark)
f <- function(x, r){
  return(4*r*x*(1-x))
}
logisticMap <-  function(r, burnOut = 1000, burnIn = 100){
  data <- data_frame(r = double() , x = double()) # 
  x <- .5
  for(i in 1:burnOut) x <- f(x,r)
  for(j in 1:burnIn){
    x <- f(x,r)
    data[j,"r"] <- r
    data[j,"x"] <- x
    }
  return(data)
}
logisticMapComp <- cmpfun(logisticMap)  

microbenchmark(
  LogistMap=logisticMap(r = .9),
  LogistMapComp=logisticMapComp(r = .9),
  times=100L)
## Unit: milliseconds
##           expr      min        lq     mean   median       uq      max
##      LogistMap 9.960785 10.104885 10.84721 10.36303 11.76221 13.29309
##  LogistMapComp 9.613380  9.727575 10.57755 10.04251 11.46385 13.72368
##  neval
##    100
##    100
library(foreach)
library(doParallel)
## Loading required package: iterators
## Loading required package: parallel
library(compiler)
library(microbenchmark)
f <- function(x, r){
  return(4*r*x*(1-x))
}



logisticMap <-  function(r, burnOut = 1000, burnIn = 100){
  data <- data.frame(r = double() , x = double()) # 
  x <- .5
  for(i in 1:burnOut) x <- f(x,r)
  for(j in 1:burnIn){
    x <- f(x,r)
    data[j,"r"] <- r
    data[j,"x"] <- x
  }
  return(data)
}
logisticMapComp <- cmpfun(logisticMap)  


# Setup parallel backend to use 4 processors
cl<-makeCluster(4)
registerDoParallel(cl)


microbenchmark(
  Parallel={
    resutParallel <- 
      foreach(r = seq(from = .7, to = 1, by = .01), .combine = 'rbind') %dopar% {
        logisticMapComp(r, burnOut = 1000, burnIn = 1000)
      }
  },
  Serial={
    resutSerial <- data.frame(r = double() , x = double())  
    for (r in seq(from = .7, to = 1, by = .01)) {
      resutSerial <- rbind(resutSerial,logisticMapComp(r, burnOut = 1000, burnIn = 1000))
    }
  },
  times=1L)
## Unit: seconds
##      expr      min       lq     mean   median       uq      max neval
##  Parallel 2.137274 2.137274 2.137274 2.137274 2.137274 2.137274     1
##    Serial 3.364310 3.364310 3.364310 3.364310 3.364310 3.364310     1
stopCluster(cl)
cl<-makeCluster(4)
registerDoParallel(cl)
resutParallel <- 
  foreach(r = seq(from = .7, to = 1, by = .001), .combine = 'rbind') %dopar% {
    logisticMapComp(r, burnOut = 1000, burnIn = 1000)
  }
q <- ggplot(resutParallel, aes(x = r, y = x))
q + geom_point(size = .75, alpha = .05, color = "blue")

stopCluster(cl)