Q4

Simulation to Estimate \(\pi\)

The Function

simulation <- function(long){
  c = rep(0,long)
  numberIn = 0
  for(i in 1:long){
    x = runif(2,-1,1)
    if(sqrt(x[1]*x[1] + x[2]*x[2]) <= 1){
      numberIn = numberIn + 1
    }
    prop = numberIn / i
    piHat = prop *4
    c[i] = piHat
  }
  return(c)
}

Running The Simulation

size = 10000
res = simulation(size)
ini = 1
plot(res[ini:size], type = 'l')

res[9000]
## [1] 3.132