(*Function to evaluate a Riemann sum
for a pre-defined function f[x] over
an interval [a,b] using n equal subintervals
and a randomly chosen evaluation point within
each interval. The value of the Riemann sum is S*)

RS[a_,b_,n_]:=(
S=0;
del = (b-a)/n;
For[i=0, i≤n-1,i=i+1,
S = S + f[a+(i+RandomReal[]) del] del]);

f[x_]:= Sqrt[1-x^2]

(*estimate the area of the quarter unit disk
with 50 intervals*)

RS[0,1,50]

S

(*estimate for the area of the unit disk*)

4 S

(*try more intervals*)

RS[0,1,5000]

4S

(* more intervals *)

RS[0,1,50000]

(*the value is now close to Pi, as expected*)

4S

(*example estimating ln x as a Riemann sum*)

f[x_]:= 1/x

(* estimating ln e *)

RS[1,Exp[1],100]

S

(*example illustrating Integral(e^x) = e^x *)

f[x_]:= Exp[x]

N[Exp[1],10]

RS[0,1,1000]

N[S+1,10]

RS[0,1,5000]

N[S+1,10]

(*a made-up example*)

f[x_]:= Sin[x]/(1+x^2)

Plot[f[x],{x,0,10}]

RS[0,10,5000]

S

(*density for standard normal distribution*)

f[x_] := (1/Sqrt[2 Pi])Exp[-(1/2)x^2]

f[x]

Plot[f[x],{x,-5,5}]

(*there is no antiderivative in "closed form";
we must estimate the area under the curve numerically*)

RS[-5,5,5000]

S