My code only plot the last iteration. Logisticmap plotting in R

Viewed 26

Im plotting the logisticmap equation. I made a for loop to get the values and I want to plot all of them but it only plot the last iteration. Im new in R, is my first programming language. Thank you for help. :) Here is my code

n <- readline(prompt="Introduce el numero de iteraciones: ")
r <- readline(prompt="Introduce el parametro R: ")
xo <- readline(prompt="Introduce la condicion inicial x_o: ")



n <- as.numeric(n)
r <- as.numeric(r)
xo <- as.numeric(xo)



result <- 1:n
result[1] <- xo



for(i in 1:n){
  result[i+1] = r*result[i]*(1-result[i])
  print(result[[i+1]])
  plot(i, result[i+1])
}
1 Answers

An option is to change the par settings based on the value of n and then plot on a single page

par(mfrow = c(3, 3))
for(i in 1:n){
   result[i+1] = r*result[i]*(1-result[i])
   print(result[[i+1]])
   plot(i, result[i+1])
 }

Or save all the plots in a single pdf

pdf("testing.pdf")
for(i in 1:n){
  result[i+1] = r*result[i]*(1-result[i])
  print(result[[i+1]])
  plot(i, result[i+1])
 }

dev.off()
Related