我正在做一个关于圆周率的蒙特卡罗近似的练习.
由于某种原因,当所有的变量都被定义时,曲线图是空的,你能帮我吗?
以下是代码:
set.seed(1)
M = 10^4
U = matrix(runif(2*M), ncol = 2)
sum_squares = U[,1]^2 + U[,2]^2
under_curve = sum_squares < 1
points_under_curve = U[under_curve,]
points_over_curve = U[!under_curve,]
pi_4_estimate = sum(under_curve)/M
cat("Estimation of π/4 = ", pi_4_estimate)
pi_estimate = pi_4_estimate * 4
cat("Estimation of π = ", pi_estimate)
plot(pi_estimate, type = "l", xlab = "Number of Simulations", ylab = "Estimation of Pi", main = "Convergence of Monte Carlo Estimator of Pi", xlim = c(0,M))
I tried :
check if the variables pi_4_estimate and pi_estimate are correctly calculated.
Modifying the plot parameters
restarting RStudio and running the code again.