jmbm

Fig_06

Oct 10th, 2022 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.09 KB | None | 0 0
  1. pdf("fig_06.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.   trozos <- rexp(n, 0.1)
  12.   trozos <- trozos/sum(trozos)
  13.   trozos <- sort(trozos, decreasing = TRUE)  # ordenarlos de mayor a menor
  14.   as[, i] <- trozos
  15. }
  16.  
  17. distribucion <- rowMeans(as)  # longitudes medias
  18.  
  19. # gráfico
  20. plot(1:n, distribucion, lwd = 2, bty = "n", xlab = "Rango", ylab = "Longitud", type = "h",
  21.      ylim = c(0, 1.1 * max(distribucion)), col = "royalblue")
  22. text(1:n, distribucion, round(distribucion, 2), cex = 0.8, pos = 3)
  23. legend("topright", bty = "n", cex = 0.8, c("exponencial", "suma arm."), lty = c(1, 0), lwd = c(2, NA), pch = c(NA, 1), col = c("royalblue", "red"))
  24.  
  25. #---- Ahora la bella para hacer la suma de armónicos
  26. sum_arm <- function(n) {
  27.   a <- numeric(n)
  28.   for (i in 1:n) {
  29.     for (j in 1:i) {
  30.       a[j] <- a[j] + 1/i
  31.     }
  32.   }
  33.   a/n
  34. }
  35.  
  36. points(sum_arm(n), col = "red")
  37.  
  38.  
  39. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment