Compute the surface area of F(x) = (x^4) * exp(-x) when x belongs to (1,5) through the geometric approach of monte carlo simulation, using R

Viewed 17

I wrote this code to handle the problem:

res1=0

curve((x^4)*exp(-x),xlim=c(1,5),ylim=c(1,5))

x=runif(1000,1,5)

y=runif(1000,1,5)

points(x,y,col=ifelse(y<=(x^4)*exp(-x),"red","green"))

Practically we are looking for the ratio of y under the graph

for (i in 1:100000) {
  x=runif(100000,1,5)
 y=runif(100000,1,5) 

 res1<-c(res1,mean(y<=mean((x^4)*exp(-x))))}

 4 * 4 * mean(res1) #Reminder = mean_of_numbers_of_y_under_the_graph * width * height

If we check the outcome with the built-in function integrate we will have a big divergence. Thus I wonder where a mistake could be, because if I use as function the f(x)=sqrt(1-x^2) where x belongs to (0,1) this code (properly changed) is fine.

Thanks in advance for the time!

0 Answers
Related