Approximate This Integral!

N = 0 π / 4 ( cos ( x ) ) 100 d x N = \int_0^{\pi/4} (\cos(x))^{100}\,dx

What are the first three digits after the decimal point of N ? N?


The answer is 125.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

12 solutions

Ross Dempsey
Jul 22, 2013

The midpoint rule is simple to code and has a reasonable error bound. First, we code the midpoint rule in Python:

def midpoint_integral(f, a, b, n):
x = a+(b-a)/(2*n)
dx = (b-a)/n
ans = 0
while x < b:
    ans += f(x)*dx
    x += dx
return ans

We then solve for the minimum n required to achieve error less than 0.0005. The error of the midpoint rule is known to be k ( b a ) 3 24 n 2 k \frac{(b-a)^{3}}{24n^{2}} , where k is the maximum value over the interval [a, b] of f ( x ) |f^{``}(x)| . In this case, k = 100, as can be determined through basic calculus. We then solve the inequality: 100 ( π 4 ) 3 24 n 2 < 0.0005 \frac{100(\frac{\pi}{4})^{3}}{24n^{2}} < 0.0005 and arrive at n being at least 45. Running the command

int(1000*midpoint_integral(lambda x: math.cos(x) ** 100, 0, math.pi/4, 45))

gives a result of 125 \textbf{125} .

I'm pretty sure this solution is valid - it does evaluate sine and cosine, but only at 0 and pi/4. It just integrates using the reduction formula for c o s ( x ) cos(x) , which is very nice to code in Python:

import math

def integral(n): #integral of cos^n(x)
    if n > 2:
        return lambda x: (1/n)*(math.cos(x) ** (n-1))*math.sin(x) + (1-1/n)*integral(n-2)(x)
    elif n == 2:
        return lambda x: x/2+math.sin(x)*math.cos(x)/2
    else: #n == 1
        return lambda x: math.sin(x)

F = integral(100)
print(F(math.pi/4)-F(0))

Ross Dempsey - 7 years, 10 months ago

Log in to reply

Yes -- the reduction formula is a great way to go. And of course, since you already know the values of sine and cosine at 0 and pi/4, you don't actually need to "evaluate" them (in the sense that you don't need to call these functions).

John Smith Staff - 7 years, 10 months ago

Great! I've been waiting to see someone perform a careful analysis of the error of the approximation.

Now here is a more serious challenge: can you write a program that calculates this answer without ever having to compute the values of the function c o s ( x ) cos(x) ?

John Smith Staff - 7 years, 10 months ago
Josh Silverman Staff
Jul 24, 2013

0 π / 4 ( cos x ) 100 d x 0 π / 4 ( 1 x 2 2 ) 100 d x 0 π / 4 e 50 x 2 d x 0 e 50 x 2 d x = 1 2 π 50 0.125331 \displaystyle \int\limits_{0}^{\pi/4} \left(\cos x\right)^{100} dx\\ \displaystyle\approx \int\limits_{0}^{\pi/4} \left(1-\frac{x^2}{2}\right)^{100} dx \\ \displaystyle\approx \int\limits_{0}^{\pi/4} e^{-50x^2} dx \\ \displaystyle\approx \int\limits_{0}^{\infty} e^{-50x^2} dx \\ \displaystyle= \frac12 \sqrt{\frac{\pi}{50}} \approx 0.125331\ldots

Sorry. I couldn't follow how you approximated the integrals one by one. Could you just explain a bit more ?

Nishant Sharma - 7 years ago

Log in to reply

First approximation: First two terms of Maclaurin series of cos x \cos x was substituted.

Second approximation: ( 1 x 2 2 ) 100 = [ ( 1 x 2 2 ) 2 ] 50 ( 1 x 2 2 ) 1 00 = ( 1 x 2 + x 4 2 ) 50 \left(1-\dfrac{x^2}2\right)^{100}=\left[\left(1-\dfrac{x^2}2\right)^2\right]^{50}\\~\\~\\\phantom{\left(1-\dfrac{x^2}2\right)^100}=\left(1-x^2+\dfrac{x^4}2\right)^{50} And since 1 x 2 + x 4 2 1-x^2+\dfrac{x^4}2 is first three terms of Maclaurin series of e x 2 e^{-x^2} , we can substitute that. Hence we have ( e x 2 ) 50 = e 50 x 2 \left(e^{-x^2}\right)^{50} = e^{-50x^2}

Not sure about the rest of steps, but it deals with error function , and I suppose we can change upper limit to \infty since you would see that π 4 e 50 x 2 d x 0 \displaystyle\int_{\frac{\pi}4}^\infty e^{-50x^2}\;\mathrm dx\approx0 if you plot it.

Micah Wood - 5 years ago

Log in to reply

@Nishant Sharma

After you get the integrand to e 50 x 2 e^{-50x^2} , the only remaining step is to recognize that the integrand is already very close to zero at π / 4 \pi/4 .

Therefore, extending the limits of integration to \infty contributes hardly any extra area to the value of the integral.

The final step is simply the fact that we have a closed form for the integral of a Gaussian from 0 0 to \infty .

Josh Silverman Staff - 4 years, 10 months ago

We approximate the function for rectangles in the interval between the limits of integration. Since the integral is the area below the curve of the function, we gotta add the areas of the rectangles.

Code in C:

#include <stdio.h>
#include <math.h>

#define PI acos(-1)

int main(void) {
    double interval = PI/400000; /* small sub-interval */
    double next;
    double integral = 0;
    int i;
    double cosseno;

    for (next = interval; next <= PI/4.0; next += interval) {
        cosseno = cos(next);
        for (i = 0; i < 99; i++) { /* really inefficient multiplication*/
            cosseno *= cos(next);
        }

        integral += cosseno * interval;
    }

    printf("integral = %lf\n", integral);

    return 0;
}
Daniel Chiu
Jul 22, 2013

With a small enough ϵ \epsilon , simply calculating the sum of ϵ cos 100 ( i ϵ ) \epsilon\cdot\cos^{100}(i\epsilon) with i i from 0 to π 4 ϵ \dfrac{\pi}{4\epsilon} will give the answer by the definition of integration.

Chew-Seong Cheong
Jul 14, 2014

Well, this is a CS problem, so I solved it the CS way using Python. The program is quite simple.

for i in range(1,100): # Just iterate the problem 99 times

 A = 0                         # A is the area under the curve y=cos(x)**100 which is the integral N

 n = 200*i

 d = pi/4/n                # Setting the slice dx

 for j in range(n):     # Approximation of the integration

      x = d*i

      y = cos(x)**100

      A+= y*d

 print n, A

After the computation finish just inspect the results for the first three digits after the decimal, which was found to be 125 \boxed{125} .

Masbahul Islam
May 20, 2014

Python code:

import math

l=.0000001

x=0

b=float(math.pi)/4

s=0.0 while x<'b: y=l (math.cos(x)) *100

    x=x+l

    s=s+y

print s

Michael Tang
Jul 23, 2013

I used Riemann sum rectangle approximations to estimate the value of the integral.

def integralValue():
    """Approximates the value of an integral"""
    from math import cos, pi

    # We use the classic Riemann sum rectangle approximation method (left-hand intervals)
    # Using 1000 rectangles should give enough accuracy

    integralValue = 0

    for x in range(1000):
        x *= pi/4000
        # Multiplies x by the width of the rectangle to obtain the correct x-value

        rectangleArea = (cos(x) ** 100) * (pi / 4000)

        # The width of each rectangle is pi/4000 and the height is cos^100(x)

        integralValue += rectangleArea

    return integralValue

Since only three digits of this are needed, we can approximate, rather than solve, this integral. To do this, we can break the function into some number of small pieces (~10000 is easily computable and gives enough precision).

The area of each piece can be computed by calculating the value of the function at that point (the height of the rectangle) and multiplying by 1/10000th of the width of the integration domain. By approximating the Riemann sum, we can approximate a difficult integral quite simply.

Adrian Iriciuc
Jul 22, 2013

Let f(x) = c o s ( x ) 100 cos(x)^{100} and F(x) integral of f(x). Than N = F(pi/4) - F(0).

Let F(0) = c

Let e = 1 0 4 10^{-4}

Knowing that f(x) = F ( x + e ) F ( x ) e \frac{F(x + e) - F(x)}{e} => F(x + e) = F(x) + e*f(x) we can write the series:

  • F(0) = c
  • F(e) = c + f(0)*e
  • F(2e) = F(e) + f(e) e = c + f(0) e + f(e)*e

....

  • F(pi/4) = c + e*(f0 + f(e) + f(2e) +...+f(pi/4 - e))

Than N = F(pi/4) - F(0) = e (f(0) + f(e) + f(2e) +...+ f(pi/4 - e)). Here is the code:

    double N = 0;
    double e = 0.0001;

    for(double x = 0; x < Math.PI / 4; x += e) {
        N += f(x) * e;
    }

    System.out.println(N);
Bao Nguyen Le
Jul 22, 2013

Simpson's Rule

from math import *

def x(i):
    global a, h
    return a+h*i

def f(x):
    return cos(x)**100

a = 0
b = pi/4
n = 10000
h = (b-a)/(2*n)

res = f(x(0)) + f(x(2*n))
for i in range(1, 2*n):
    res += ((i%2 == 1)*2 + 2)*f(x(i))
res *= h/3

print res
Vitaly Breyev
Jul 22, 2013

Despite that this problem is solvable in literally three seconds in Mathematica, I wrote up a neat little numerical integrator that utilizes the trapezoid method. I am too lazy to comment it out, but the variable names are pretty self-explanatory. It seems to work on 100000 ordinates just fine.

import math

def trapezoid_integrator(lower_limit, upper_limit, ordinates):
    c = float(upper_limit - lower_limit) / ordinates

    integrand_values = []

    for i in range(ordinates + 1):
        integrand_val = (math.cos(i))**100
        integrand_values.append(integrand_val)

    trapezoid_sum = 0

    for i in range(len(integrand_values)):
        if (i == 0) or (i == len(integrand_values)-1):
            trapezoid_sum += integrand_values[i]
        else: trapezoid_sum += 2*integrand_values[i] 

     integral = c * trapezoid_sum
     return integral


print trapezoid_integrator(0, math.pi / 4, 100000)

Whoops, made some mistakes while pasting code here. Instead of

integrand_val = (math.cos(i))**100

there should, of course, be

integrand_val = (math.cos(i * c))**100

And instead of

integral = c * trapezoid_sum

there should be

integral = c/2 * trapezoid_sum

This way, the program outputs 0.12501848174 and everything is jolly good.

Vitaly Breyev - 7 years, 10 months ago
Terence An
Jul 22, 2013

Simple Simpson's rule solution:

import math 
sum = 0
inc = (math.pi/4)
for i in range(1000):
    cos1 = math.cos(i*inc)**100
    cos2 = math.cos((i+1) * inc)**100
    cos = (cos1 + cos2)/2
    sum += cos * inc
print(sum)

This works only if your inc is also divided by 1000.

Steven Perkins - 7 years ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...