Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- results <- c()
- for(i in c(1:20)) {
- r <- 1
- limit <- 2^i
- points <- data.frame(
- x = runif(limit, -r, r),
- y = runif(limit, -r, r))
- points$d <- sqrt(points$x^2 + points$y^2)
- points$type <- ifelse(points$d < r, "c", "s")
- picalc <- 4 * length(points$type[points$type=="c"]) / limit
- error <- pi - picalc
- label <- paste0('Pi calc : ', round(picalc, 6), 'nError : ', round(error, 6))
- iter <- data.frame(n = limit, picalc = picalc, error = error, label = label)
- results <- rbind(results, iter)
- }
- # GGANIMATE
- library(ggplot2)
- library(gganimate)
- p <- ggplot(results, aes(x = runif(n, -1, 1), y = runif(n, -1, 1))) +
- geom_point(lwd = 2, alpha = 0.3) +
- theme_minimal() +
- geom_text(aes(x = 0, y = 0, label = label), size = 5) +
- labs(caption = 'Number of random points : {frame_time}') +
- transition_time(n)
- animate(p, nframes = nrow(results), fps = 5)
- library(ggplot2)
- library(gganimate)
- p <- ggplot(results, aes(x = n, y = error)) +
- geom_point(lwd = 2, alpha = 0.3) +
- theme_minimal() +
- geom_text(aes(x = 0, y = 0, label = label), size = 5, hjust = 0) +
- scale_x_log10(breaks = c(2^(1:4), 4^(2:10)), minor_breaks = NULL) +
- labs(caption = 'Number of random points : {frame}') +
- transition_manual(n) +
- shadow_trail(exclude_layer = 2)
- animate(p, nframes = nrow(results), fps = 5)
Add Comment
Please, Sign In to add comment