N = ∫ 0 π / 4 ( cos ( x ) ) 1 0 0 d x
What are the first three digits after the decimal point of N ?
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.
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 ) , 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))
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).
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 ) ?
0 ∫ π / 4 ( cos x ) 1 0 0 d x ≈ 0 ∫ π / 4 ( 1 − 2 x 2 ) 1 0 0 d x ≈ 0 ∫ π / 4 e − 5 0 x 2 d x ≈ 0 ∫ ∞ e − 5 0 x 2 d x = 2 1 5 0 π ≈ 0 . 1 2 5 3 3 1 …
Sorry. I couldn't follow how you approximated the integrals one by one. Could you just explain a bit more ?
Log in to reply
First approximation: First two terms of Maclaurin series of cos x was substituted.
Second approximation: ( 1 − 2 x 2 ) 1 0 0 = [ ( 1 − 2 x 2 ) 2 ] 5 0 ( 1 − 2 x 2 ) 1 0 0 = ( 1 − x 2 + 2 x 4 ) 5 0 And since 1 − x 2 + 2 x 4 is first three terms of Maclaurin series of e − x 2 , we can substitute that. Hence we have ( e − x 2 ) 5 0 = e − 5 0 x 2
Not sure about the rest of steps, but it deals with error function , and I suppose we can change upper limit to ∞ since you would see that ∫ 4 π ∞ e − 5 0 x 2 d x ≈ 0 if you plot it.
Log in to reply
After you get the integrand to e − 5 0 x 2 , the only remaining step is to recognize that the integrand is already very close to zero at π / 4 .
Therefore, extending the limits of integration to ∞ 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 to ∞ .
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;
}
With a small enough ϵ , simply calculating the sum of ϵ ⋅ cos 1 0 0 ( i ϵ ) with i from 0 to 4 ϵ π will give the answer by the definition of integration.
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 1 2 5 .
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
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.
Let f(x) = c o s ( x ) 1 0 0 and F(x) integral of f(x). Than N = F(pi/4) - F(0).
Let F(0) = c
Let e = 1 0 − 4
Knowing that f(x) = e F ( x + e ) − F ( x ) => F(x + e) = F(x) + e*f(x) we can write the series:
....
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);
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
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.
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.
Problem Loading...
Note Loading...
Set Loading...
The midpoint rule is simple to code and has a reasonable error bound. First, we code the midpoint rule in Python:
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 2 4 n 2 ( b − a ) 3 , where k is the maximum value over the interval [a, b] of ∣ f ‘ ‘ ( x ) ∣ . In this case, k = 100, as can be determined through basic calculus. We then solve the inequality: 2 4 n 2 1 0 0 ( 4 π ) 3 < 0 . 0 0 0 5 and arrive at n being at least 45. Running the command
gives a result of 125 .