Não foi possível enviar o arquivo. Será algum problema com as permissões?
Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
Ambos lados da revisão anterior Revisão anterior Próxima revisão | Revisão anterior Próxima revisão Ambos lados da revisão seguinte | ||
pessoais:eder [2011/06/05 21:13] eder [section 5] |
pessoais:eder [2011/06/06 20:50] eder [section 5] |
||
---|---|---|---|
Linha 24: | Linha 24: | ||
<code R> | <code R> | ||
###-----------------------------------------------------------------### | ###-----------------------------------------------------------------### | ||
- | ###buf | + | ### Agulha de buffon |
- | buf <- function(n){ | + | buffon <- function(n,l=1,a=1){ |
- | ttt <- NULL | + | if(a<l){cat('Erro: a < l, deve ser a > l\n')} |
- | ttt[1] <- 0 | + | if(a>=l){ |
- | x <- runif(n) | + | theta <- runif(n,0,pi) |
- | th <- runif(n,0,pi) | + | dist <- runif(n,0,a/2) |
- | st <- sin(th) | + | inter <- sum(dist <= l/2*sin(theta)) |
- | for ( i in 1:n){ | + | phi_est <- round((n/inter)*(2*l/a),12) |
- | if(st[i]>x[i]){ | + | cat('Número Simulação',n,'phi_estimado',phi_est,'Erro',round(pi-phi_est,12),'\n') |
- | ttt[i+1] <- ttt[i]+1 | + | return(c(n,phi_est)) |
- | } | + | }} |
- | else { | + | |
- | ttt[i+1] <- ttt[i] | + | n <- seq(10000,1000000,by=20000) |
- | }} | + | res <- matrix(NA,ncol=2,nrow=length(n)) |
- | if (ttt[n+1]>0){ | + | con <- 1 |
- | plot((0:n)[ttt>0],2*(0:n)[ttt>0]/ttt[ttt>0],type='l',xlab='numero simulação',ylab='pi') | + | for (i in n){ |
- | } | + | res[con,] <- buffon(i) |
- | else{print('no sucesso')} | + | con <- con+1 |
- | abline(pi,0) | + | } |
- | } | + | |
- | + | plot(res,type='l',ylab=expression(pi),xlab='Simulações') | |
- | buf(100000) | + | abline(h=pi,col='red') |
###-----------------------------------------------------------------### | ###-----------------------------------------------------------------### | ||
### MOnte carlo | ### MOnte carlo |