par(mfrow=c(1,2), mar=c(3,2,2,1)) # affianco i due grafici in 1 finestra
#
plot(c(-1,1),c(-1,1),type="n",xlab="", ylab="", asp=1)
abline(v=axTicks(1), h=axTicks(2), col="red",lty=3)
symbols(0, 0, circles=1, inches=FALSE, add=TRUE, fg="blue")
n <- 0; ok <- 0; xx <- NULL; yy <- NULL
for (i in 1:10000) {x <- runif(1)*2-1; y <- runif(1)*2-1; d <- x*x+y*y
if(d < 1){n <- n+1; points(x,y,type="p", pch="."); xx <- c(xx,x); yy <- c(yy,y)
          if(d < 0.25) ok <- ok+1} }
mod = lm(yy ~ xx); mod$coefficients; abline(mod,lwd=2)
mod = lm(xx ~ yy); mod$coefficients
a1 <- mod$coefficients[1]; a2 <- mod$coefficients[2]
abline(-a1/a2,1/a2,lwd=2)
#
plot(c(-1,1),c(-1,1),type="n",xlab="", ylab="", asp=1)
abline(v=axTicks(1), h=axTicks(2), col="red",lty=3)
symbols(0, 0, circles=1, inches=FALSE, add=TRUE, fg="blue")
pi2 <- atan(1)*8; ok <- 0; n <- 10000; xx <- NULL; yy <- NULL
for(i in 1:n) {r <- runif(1); a <- runif(1)*pi2
x <- r*cos(a); y <- r*sin(a); d <- x*x+y*y; xx <- c(xx,x); yy <- c(yy,y)
points(x,y,type="p", pch="."); if(d < 0.25) ok <- ok+1}
mod = lm(yy ~ xx); mod$coefficients; abline(mod,lwd=2)
mod = lm(xx ~ yy); mod$coefficients
a1 <- mod$coefficients[1]; a2 <- mod$coefficients[2]
abline(-a1/a2,1/a2,lwd=2)
# fine