jmbm

Fig_05

Oct 10th, 2022 (edited)
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.27 KB | None | 0 0
  1. pdf("fig_05.pdf", width = 5, height = 5)
  2. par(bty = "n", las = 1, mar = c(4, 4, 1, 1) + 0.2)
  3.  
  4. #---- Primero a lo bestia
  5. repetir <- 10000
  6. n <- 7
  7. trozos <- numeric(n)
  8. as <- matrix(NA, nrow = n, ncol = repetir)  # para guardar las repeticiones
  9.  
  10. for (i in 1:repetir) {
  11.   p_azar <- runif(n - 1)  # elegir los puntos de corte al azar
  12.   p_azar <- c(sort(p_azar), 1)  # ordenar los cortes, más el final
  13.   trozos[1] <- p_azar[1]  # primer trozo
  14.   for (j in 2:n) {
  15.     trozos[j] <- p_azar[j] - p_azar[j - 1]  # los demás trozos
  16.   }
  17.   trozos <- sort(trozos, decreasing = TRUE)  # ordenarlos de mayor a menor
  18.   as[, i] <- trozos
  19. }
  20.  
  21. distribucion <- rowMeans(as)  # longitudes medias
  22.  
  23. # gráfico
  24. plot(1:n, distribucion, lwd = 2, bty = "n", xlab = "Rango", ylab = "Longitud", type = "h",
  25.      ylim = c(0, 1.1 * max(distribucion)))
  26. text(1:n, distribucion, round(distribucion, 2), cex = 0.8, pos = 3)
  27. legend("topright", bty = "n", cex = 0.8, c("simulación", "teórico"), lty = c(1, 0), lwd = c(2, NA), pch = c(NA, 1), col = c("black", "red"))
  28.  
  29. #---- Ahora la bella para hacer la suma de armónicos
  30. sum_arm <- function(n) {
  31.   a <- numeric(n)
  32.   for (i in 1:n) {
  33.     for (j in 1:i) {
  34.       a[j] <- a[j] + 1/i
  35.     }
  36.   }
  37.   a/n
  38. }
  39.  
  40. points(sum_arm(n), col = "red")
  41.  
  42.  
  43. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment