Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
| Ambos lados da revisão anteriorRevisão anteriorPróxima revisão | Revisão anterior | ||
| disciplinas:ce718:atividades2011:pi [2011/06/10 09:25] – paulojus | disciplinas:ce718:atividades2011:pi [2011/06/21 00:56] (atual) – [section 4] fernandomayer | ||
|---|---|---|---|
| Linha 4: | Linha 4: | ||
| ==== Agulha de Buffon ==== | ==== Agulha de Buffon ==== | ||
| + | |||
| + | Diferentes abordagens para a estimativa de π através do experimento da agulha de Buffon, realizadas pelos participantes do curso: | ||
| + | |||
| + | === Éder === | ||
| + | |||
| + | <code R> | ||
| + | buffon.eder <- function(n, | ||
| + | if(a< | ||
| + | if(a> | ||
| + | theta <- runif(n, | ||
| + | dist <- runif(n, | ||
| + | inter <- sum(dist <= l/ | ||
| + | phi_est <- round((n/ | ||
| + | cat(' | ||
| + | round(pi-phi_est, | ||
| + | return(c(n, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | ## Teste de funcionamento | ||
| + | buffon.eder(1000) | ||
| + | |||
| + | ## Abordagem final | ||
| + | n <- seq(10000, | ||
| + | res <- matrix(NA, | ||
| + | con <- 1 | ||
| + | system.time( | ||
| + | for (i in n){ | ||
| + | res[con,] <- buffon.eder(i) | ||
| + | con <- con+1 | ||
| + | } | ||
| + | ) | ||
| + | |||
| + | ## Plot | ||
| + | plot(res, | ||
| + | abline(h=pi, | ||
| + | </ | ||
| + | |||
| + | === Walmes === | ||
| + | |||
| + | <code R> | ||
| + | buffon.walmes <- function(simul, | ||
| + | ## argumentos da função | ||
| + | ## simul é o escalar inteiro número de agulhas lançadas | ||
| + | ## d é o escalar espaçamento entre as linhas da grade | ||
| + | ## l é o escalar comprimento da agulha | ||
| + | ## n é o escalar inteiro número de linhas na grade | ||
| + | malha <- seq(0, 10, by=d) | ||
| + | linhas <- malha[-c(1, | ||
| + | range <- range(malha)+c(1, | ||
| + | x <- matrix(runif(2*simul, | ||
| + | a <- runif(simul, | ||
| + | y <- x+matrix(c(l*sin(a), | ||
| + | R <- sapply(seq_len(simul), | ||
| + | function(i){ | ||
| + | sum(linhas> | ||
| + | }) | ||
| + | rho <- l/d | ||
| + | ## função retorna um a lista com | ||
| + | ## R vetor de TRUE/FALSE se a linha toca ou não a grade | ||
| + | ## o valor de rho | ||
| + | ## a matrix X com as coordenadas onde a agulha toca | ||
| + | return(list(R=R, | ||
| + | } | ||
| + | |||
| + | ## Teste de funcionamento | ||
| + | buffon.walmes(1000) | ||
| + | |||
| + | ## Abordagem final | ||
| + | bf <- buffon.walmes(1000) | ||
| + | pi0 <- bf$rho/ | ||
| + | |||
| + | ## Plot | ||
| + | plot(bf$rho/ | ||
| + | abline(h=pi) | ||
| + | </ | ||
| + | |||
| + | === Fernando === | ||
| + | |||
| + | <code R> | ||
| + | buffon.fernando <- function(n, a = 1, l = 1){ | ||
| + | buffon.calc <- function(n, ...){ | ||
| + | D.random <- runif(n, 0, a/2) | ||
| + | theta.random <- runif(n, 0, pi) | ||
| + | d <- (l/2) * sin(theta.random) | ||
| + | H <- numeric(n) | ||
| + | H[D.random <= d] <- 1 | ||
| + | h <- sum(H) | ||
| + | pi.est <- (2*l*n)/ | ||
| + | return(pi.est) | ||
| + | } | ||
| + | pi.res <- sapply(n, buffon.calc) | ||
| + | saida <- data.frame(n = n, | ||
| + | pi.est = pi.res, | ||
| + | abs.error = abs(pi.res - pi)) | ||
| + | class(saida) <- c(" | ||
| + | return(saida) | ||
| + | } | ||
| + | |||
| + | ## Teste | ||
| + | system.time( | ||
| + | testef <- buffon.fernando(1: | ||
| + | ) | ||
| + | |||
| + | ## Abordagem final | ||
| + | # ja finalizada | ||
| + | source(" | ||
| + | |||
| + | ## Plot | ||
| + | plot(buffon.fernando(1: | ||
| + | </ | ||
| + | |||
| + | A função '' | ||
| + | |||
| + | <code R> | ||
| + | plot.buffon <- function(x, xlab = " | ||
| + | ylab = expression(paste(" | ||
| + | ...){ | ||
| + | plot(x$n, x$pi.est, type = " | ||
| + | main = " | ||
| + | abline(h = pi, col = " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Stuart === | ||
| + | |||
| + | Para comparação, | ||
| + | |||
| + | <code R> | ||
| + | require(CIM) | ||
| + | buf(100) | ||
| + | |||
| + | ## edita a funcao para tirar o plot | ||
| + | buffon.stuart <- function(n){ | ||
| + | ttt <- NULL | ||
| + | ttt[1] <- 0 | ||
| + | x <- runif(n) | ||
| + | th <- runif(n, 0, pi) | ||
| + | st <- sin(th) | ||
| + | for (i in 1:n) { | ||
| + | if (st[i] > x[i]) | ||
| + | ttt[i + 1] <- ttt[i] + 1 | ||
| + | else ttt[i + 1] <- ttt[i] | ||
| + | } | ||
| + | #ttt | ||
| + | if (ttt[n + 1] > 0) | ||
| + | 2 * (0:n)[ttt > 0]/ttt[ttt > 0] | ||
| + | else print(" | ||
| + | } | ||
| + | |||
| + | ## Teste | ||
| + | buffon.stuart(1000) | ||
| + | |||
| + | ## Plot | ||
| + | plot(buffon.stuart(1000), | ||
| + | abline(h = pi) | ||
| + | </ | ||
| + | |||
| ==== Comparação dos algorítmos ==== | ==== Comparação dos algorítmos ==== | ||
| + | |||
| + | === Executando e armazenando tempos e resultados === | ||
| + | |||
| + | <code R> | ||
| + | ## Define um n comum | ||
| + | n1 <- 1:1e+4 | ||
| + | n2 <- seq(0, 1e+6, 1000)[-1] | ||
| + | |||
| + | ## | ||
| + | ## Tempo Eder com n1 | ||
| + | res.eder.n1 <- matrix(NA, ncol=2, nrow=length(n1)) | ||
| + | con <- 1 | ||
| + | eder.n1 <- system.time( | ||
| + | for (i in n1){ | ||
| + | | ||
| + | con <- con+1 | ||
| + | } | ||
| + | ) | ||
| + | |||
| + | ## Tempo Walmes com n1 | ||
| + | walmes.n1 <- system.time( | ||
| + | bf <- buffon.walmes(max(n1)) | ||
| + | ) | ||
| + | res.walmes.n1 <- bf$rho/ | ||
| + | |||
| + | |||
| + | ## Tempo Fernando com n1 | ||
| + | fernando.n1 <- system.time( | ||
| + | | ||
| + | ) | ||
| + | |||
| + | ## Tempo Stuart com n1 | ||
| + | stuart.n1 <- system.time( | ||
| + | | ||
| + | ) | ||
| + | ## | ||
| + | |||
| + | ## Tempo Eder com n2 | ||
| + | res.eder.n2 <- matrix(NA, ncol=2, nrow=length(n2)) | ||
| + | con <- 1 | ||
| + | eder.n2 <- system.time( | ||
| + | for (i in n2){ | ||
| + | | ||
| + | con <- con+1 | ||
| + | } | ||
| + | ) | ||
| + | |||
| + | ## Tempo Walmes com n2 | ||
| + | walmes.n2 <- system.time( | ||
| + | bf <- buffon.walmes(max(n2)) | ||
| + | ) | ||
| + | res.walmes.n2 <- bf$rho/ | ||
| + | |||
| + | |||
| + | ## Tempo Fernando com n2 | ||
| + | fernando.n2 <- system.time( | ||
| + | | ||
| + | ) | ||
| + | |||
| + | ## Tempo Stuart com n2 | ||
| + | stuart.n2 <- system.time( | ||
| + | | ||
| + | ) | ||
| + | #### Parei em 3261 seg. ~ 55 min. | ||
| + | </ | ||
| + | |||
| + | === Comparando performances === | ||
| + | |||
| + | <code R> | ||
| + | ## Usando n1 = 1:1e4 | ||
| + | ## | ||
| + | |||
| + | ## Comparacao de tempos de execucao | ||
| + | tempo.n1 <- c(eder.n1[3], | ||
| + | names(tempo.n1) <- c(" | ||
| + | tempo.n1 <- sort(tempo.n1) | ||
| + | ## barchart | ||
| + | require(lattice) | ||
| + | barchart(tempo.n1, | ||
| + | panel = function(...){ | ||
| + | | ||
| + | | ||
| + | labels = do.call(as.character, | ||
| + | list(round(tempo.n1, | ||
| + | }) | ||
| + | |||
| + | ## Cria um data.frame com todos os resultados | ||
| + | res.n1 <- data.frame(n = n1, | ||
| + | eder = res.eder.n1[, | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | ## modifica o data.frame para o lattice | ||
| + | require(reshape) | ||
| + | res.n1 <- melt(res.n1, | ||
| + | |||
| + | ## Comparacao grafica | ||
| + | xyplot(value ~ n | variable, data = res.n1, type = " | ||
| + | xlab = " | ||
| + | ylab = expression(paste(" | ||
| + | panel = function(...){ | ||
| + | | ||
| + | | ||
| + | }, scales = list(relation = " | ||
| + | |||
| + | ## Usando n2 = seq(0, 1e+6, 1000)[-1] | ||
| + | ## | ||
| + | |||
| + | ## Comparacao de tempos de execucao | ||
| + | tempo.n2 <- c(eder.n2[3], | ||
| + | names(tempo.n2) <- c(" | ||
| + | tempo.n2 <- sort(tempo.n2) | ||
| + | ## barchart | ||
| + | barchart(tempo.n2, | ||
| + | panel = function(...){ | ||
| + | | ||
| + | | ||
| + | labels = do.call(as.character, | ||
| + | list(round(tempo.n2, | ||
| + | }) | ||
| + | |||
| + | ## Cria um data.frame com todos os resultados | ||
| + | ## Stuart nao entra pq nao terminou a execução | ||
| + | ## Walmes fica de fora pq vai ate 1e6 | ||
| + | res.n2 <- data.frame(n = n2, | ||
| + | eder = res.eder.n2[, | ||
| + | | ||
| + | |||
| + | ## modifica o data.frame para o lattice | ||
| + | res.n2 <- melt(res.n2, | ||
| + | |||
| + | ## Comparacao grafica | ||
| + | xyplot(value ~ n | variable, data = res.n2, type = " | ||
| + | xlab = " | ||
| + | ylab = expression(paste(" | ||
| + | panel = function(...){ | ||
| + | | ||
| + | | ||
| + | })#, scales = list(relation = " | ||
| + | |||
| + | ## Plot separado do Walmes | ||
| + | xyplot(res.walmes.n2 ~ 1:max(n2), type = " | ||
| + | xlab = " | ||
| + | ylab = expression(paste(" | ||
| + | panel = function(...){ | ||
| + | | ||
| + | | ||
| + | }) | ||
| + | </ | ||
| + | |||