Numerical Integration

Approximation with four subintervals

We want to evaluate the integral (∫_0^1)cos((x^2))dx.  By the Fundamental Theorem of Calculus, we can evaluate the the integral if we have an antiderivative for f(x)=cos((x^2)).

Integrate[Cos[x^2], x]

Mathematica is telling us that there is a name for the antiderivative (a certain Fresnel function), but this function does not have an elementary formula, and, in particular, we do not know what its values are at zero and one, so the FTC will not help us.

We will approximate the integral numerically.  First we graph the integrand f(x)=cos((x^2)).

Plot[Cos[x^2], {x, -1, 2}]

We compute the left approximation (four rectangles), the right approximation, the trapezoidal approximation, and the midpoint approximation.

L4 = N[(1/4)*(Cos[0^2] + Cos[(1/4)^2] + Cos[(1/2)^2] + Cos[(3/4)^2] )]

R4 = N[(1/4)*( Cos[(1/4)^2] + Cos[(1/2)^2] + Cos[(3/4)^2] +Cos[(1)^2])]

T4=(L4+R4)/2

M4 = N[(1/4)*(Cos[(1/8)^2] + Cos[(3/8)^2] + Cos[(5/8)^2] + Cos[(7/8)^2])]

Approximation with many subintervals using the Sum command

To obtain better estimates for (∫_0^1)cos((x^2))dx, we increase the number, n, of subintervals.

We will find left, right, trapezoidal, and midpoint approximations for n=100.  In order to avoid writing long expressions, we use the Sum command.  For example, recall the general formula for the left endpoint approximation:

({{({{(Subscript[L, n])= (Underoverscript[(((b-a)/n)) ∑, (           i=0), (          n-1)])f(a+i(b-a)/n)}}) ≈ (∫_a^b)f(x)dx}})

In this case, since f(x)=cos((x^2)), a=0, and b=1,

({{({{(Subscript[L, 100])= (Underoverscript[((1/100)) ∑, (           i=0), (          99)])cos((((i/100))^2))}}) ≈ (∫_0^1)cos((x^2))dx}})

This is calculated in Mathematica using the following command:

L100 = N[(1/100)*Sum[Cos[(i/100)^2], {i, 0, 99}]]

The general formula for the right endpoint approximation is similar.  The main difference is that the index ranges from i=1 to i=n instead of i=0 to i=n-1.

({{({{(Subscript[R, n])= (Underoverscript[(((b-a)/n)) ∑, (           i=1), (          n)])f(a+i(b-a)/n)}}) ≈ (∫_a^b)f(x)dx}})

In our example,

({{({{(Subscript[R, 100])= (Underoverscript[((1/100)) ∑, (                i=1), (              100)])cos((((i/100))^2))}}) ≈ (∫_0^1)cos((x^2))dx}})

R100 = N[(1/100)*Sum[Cos[(i/100)^2], {i, 1, 100}]]

The general formula for the midpoint approximation is:

({{({{(Subscript[M, n])= (Underoverscript[(((b-a)/n)) ∑, (           i=0), (          n-1)])f(a+(2i+1)(b-a)/(2n))}}) ≈ (∫_a^b)f(x)dx}})

In our example,

({{({{(Subscript[M, 100])= (Underoverscript[((1/100)) ∑, (                i=0), (               99)])((cos((2i+1)/200))^2)}}) ≈ (∫_0^1)cos((x^2))dx}})

M100 = N[(1/100)*Sum[Cos[((2i+1)/200)^2], {i, 0, 99}]]

To find the trapezoidal approximation, simply average the left and right endpoint approximations as above.