Search code examples
rmathstatisticssimulationnumerical-integration

Evaluating integral using Riemann sums


How to evaluate J=P(0<=Z<=1), where Z~N(0,1) using Riemann sums?

so J is the integral from 0 to 1 of the function (1/sqrt(2*pi))*exp^((-x^2)/2)

Here is my approach to implement this in R

 m<-5000
a<-0
b<-1
w<-(b-a)/m
x<-seq(a+(w/2),b-(w/2),w)
h<-(1/sqrt(2*pi))*exp^((-x^2)/2)

# Error in exp^((-x^2)/2) : non-numeric argument to binary operator

sum(h*w)
#Error: object 'h' not found

I don't know why marks such error, I type is.numeric(x) and returns TRUE so where the problem is exactly if I am combining numerics only?


Solution

  • Remove ^ that follows exp function. Try:

    m<-5000
    a<-0
    b<-1
    w<-(b-a)/m
    x<-seq(a+(w/2),b-(w/2),w)
    h<-(1/sqrt(2*pi))*exp((-x^2)/2)
    
    sum(h*w)
    [1] 0.3413447