# Exercise 3 f <- function(x) {(cos(50*x) + sin(20*x))^2} # define function par(mfrow = c(2,1)) # create window curve(f, lwd = 2) # plot function theor <- integrate(f, 0, 1) # calculate exact value of the integral # plot horizontal line xgrid <- seq(1, 10^4, by = 1) y <- rep(theor[1], 10^4) plot(xgrid,y, type = "l", lwd = 2, col = "red") x <- f(runif(10^4)) # generate 10.000 random numbers ~ U(0,1) estint <- cumsum(x)/(1:10^4) # approximate the integral lines(estint, type = "l", lwd = 2) # plot approximation