Guest User

Untitled

a guest
Oct 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. results <- c()
  2. for(i in c(1:20)) {
  3. r <- 1
  4. limit <- 2^i
  5. points <- data.frame(
  6. x = runif(limit, -r, r),
  7. y = runif(limit, -r, r))
  8. points$d <- sqrt(points$x^2 + points$y^2)
  9. points$type <- ifelse(points$d < r, "c", "s")
  10. picalc <- 4 * length(points$type[points$type=="c"]) / limit
  11. error <- pi - picalc
  12. label <- paste0('Pi calc : ', round(picalc, 6), 'nError : ', round(error, 6))
  13. iter <- data.frame(n = limit, picalc = picalc, error = error, label = label)
  14. results <- rbind(results, iter)
  15. }
  16.  
  17. # GGANIMATE
  18. library(ggplot2)
  19. library(gganimate)
  20. p <- ggplot(results, aes(x = runif(n, -1, 1), y = runif(n, -1, 1))) +
  21. geom_point(lwd = 2, alpha = 0.3) +
  22. theme_minimal() +
  23. geom_text(aes(x = 0, y = 0, label = label), size = 5) +
  24. labs(caption = 'Number of random points : {frame_time}') +
  25. transition_time(n)
  26. animate(p, nframes = nrow(results), fps = 5)
  27.  
  28. library(ggplot2)
  29. library(gganimate)
  30. p <- ggplot(results, aes(x = n, y = error)) +
  31. geom_point(lwd = 2, alpha = 0.3) +
  32. theme_minimal() +
  33. geom_text(aes(x = 0, y = 0, label = label), size = 5, hjust = 0) +
  34. scale_x_log10(breaks = c(2^(1:4), 4^(2:10)), minor_breaks = NULL) +
  35. labs(caption = 'Number of random points : {frame}') +
  36. transition_manual(n) +
  37. shadow_trail(exclude_layer = 2)
  38. animate(p, nframes = nrow(results), fps = 5)
Add Comment
Please, Sign In to add comment