n<-1000
x<-runif(n)
y<-runif(n)
plot(x,y, col= 'blue')
y1<-sqrt(1/4-(x-1/2)^2)+1/2
y2<--sqrt(1/4-(x-1/2)^2)+1/2
plot(x,y,col="blue",lwd=3, main="Estimación del área del circulo", xlab="", ylab="", las=1, col.axis="red")
points(x,y1,col="green",lwd=3)
points(x,y2,col="green",lwd=3)
y1_y<-y1-y
y_y2<-y-y2
punto<-1:n
datos<-data.frame(punto,x,y,y1,y2,y1_y,y_y2)
head(datos,10)
## punto x y y1 y2 y1_y y_y2
## 1 1 0.3839739 0.27628982 0.9863517 0.013648333 0.71006185 0.262641484
## 2 2 0.1478767 0.75388237 0.8549777 0.145022266 0.10109537 0.608860099
## 3 3 0.8638219 0.16558228 0.8429776 0.157022373 0.67739535 0.008559907
## 4 4 0.5382614 0.45409030 0.9985339 0.001466081 0.54444361 0.452624222
## 5 5 0.3746437 0.88475104 0.9840308 0.015969219 0.09927974 0.868781818
## 6 6 0.2242101 0.07549092 0.9170611 0.082938945 0.84157013 -0.007448020
## 7 7 0.8932384 0.97201998 0.8088099 0.191190135 -0.16321011 0.780829843
## 8 8 0.4197840 0.21404768 0.9935234 0.006476555 0.77947577 0.207571124
## 9 9 0.8583532 0.42659839 0.8486875 0.151312466 0.42208915 0.275285920
## 10 10 0.8223845 0.44559262 0.8821887 0.117811257 0.43659613 0.327781362
datos2<-datos[datos$y1_y >=0 & datos$y>=0.5,]
datos3<-datos[datos$y_y2 >=0 & datos$y<0.5,]
plot(x,y,col="blue",lwd=3, main="Estimación del área del circulo", xlab="", ylab="", las=1, col.axis="red")
points(x,y1,col="green",lwd=3)
points(x,y2,col="green",lwd=3)
points(datos2$x,datos2$y,col="green",lwd=3)
points(datos3$x,datos3$y,col="green",lwd=3)
(nrow(datos2)+nrow(datos3))/n
## [1] 0.824
la siguiente función permite estimar el área del circulo variando la cantidad de puntos.
estimacion_area<-function(m){
x<-runif(m)
y<-runif(m)
y1<-sqrt(1/4-(x-1/2)^2)+1/2
y2<--sqrt(1/4-(x-1/2)^2)+1/2
y1_y<-y1-y
y_y2<-y-y2
datos<-data.frame(punto,x,y,y1,y2,y1_y,y_y2)
datos2<-datos[datos$y1_y >=0 & datos$y>=0.5,]
datos3<-datos[datos$y_y2 >=0 & datos$y<0.5,]
estimacion<-(nrow(datos2)+nrow(datos3))/m
plot(x,y,col="blue",lwd=3, main=c("El área estimada es:",as.character(estimacion), 'cantidad de puntos utilizados:', as.character(m)), xlab="", ylab="", las=1, col.axis="red")
points(x,y1,col="green",lwd=3)
points(x,y2,col="green",lwd=3)
points(datos2$x,datos2$y,col="green",lwd=3)
points(datos3$x,datos3$y,col="green",lwd=3)
}
estimacion_area(2000)