hw6
hw6
1 Integration
Solve the following exercises.
1. Prove that the midpoint rule is exact for polynomials of degree ≤ 1.
2. Compute the coefficients for the closed Newton-Cotes rule with four points.
And also do exercises 13.7 and 13.20 from the book.
Let us now implement numerical integration. Ideally, we would like to write something of the form
function integrate(f::Function, a::Real, b::Real) ... end
that would return an approximate value for
𝑏
∫ 𝑓(𝑥) 𝑑𝑥
𝑎
We will write different versions of numerical quadrature. Let us start with the trapezoid rule using
𝑛 + 1 points (from 𝑥0 to 𝑥𝑛 ).
end
[ ]: trapezoid(x->x^2,0,1)
[ ]: 0.33335000000000004
1
We now implement the Simpson rule. The parameter 𝑛 still means that we should be evaluating
the function at equispaced points 𝑥0 = 𝑎 < 𝑥1 < 𝑥2 < ⋯ < 𝑥𝑛 = 𝑏
The following computation should give you an exact value up to round-off errors. Why?
[ ]: simpson(x->x^2,0,1)
[ ]: 0.3333333333333334
The following function implement Gaussian curvature with two intermediate points. Note that
unline the previous cases, we end up evaluating the function 𝑓 in more than 𝑛 + 1 points.
[ ]: function gaussian2(f::Function, a::Real, b::Real, n::Integer=100)
sum = 0.
h = (b-a)/n
root = sqrt(1/3)
for i in 0:n-1
x0 = h*i + a
x1 = h*(i+1) + a
y1 = (x0+x1)/2 + root*(x1-x0)/2
y2 = (x0+x1)/2 - root*(x1-x0)/2
sum += (f(y1)+f(y2))*h/2
end
return sum
end
[ ]: 0.3333333333333333
You should now implement gaussian3 that uses three intermediate points instead of two. You can
use Wikipedia to find the roots of the orthogonal polynomial and the corresponding weights.
2
[ ]: function gaussian3(f::Function, a::Real, b::Real, n::Integer=100)
# Gaussian quadrature with 3 points
sum = 0.
h = (b-a)/n
root = sqrt(3/5)
for i in 0:n-1
x0 = h*i + a
x1 = h*(i+1) + a
y1 = (x0+x1)/2 + root*(x1-x0)/2
y2 = (x0+x1)/2
y3 = (x0+x1)/2 - root*(x1-x0)/2
sum += (5*f(y1)+8*f(y2)+5*f(y3))*h/9
end
return sum/2
end
[ ]: 0.3333333333333333
1.1 Computing 𝜋
There are several smart ways to compute the number 𝜋 with good accuracy. We are going to do
something that is not so smart. Let us approximate 𝜋 by computing the area of a circle directly.
1 √
𝜋 = 2∫ 1 − 𝑥2 𝑑𝑥.
−1
3
Approximation by Gaussian quadrature with two points:3.141709421681971
Approximation by Gaussian quadrature with three points:3.1416328913577876
The number above are aguably unfair. The trapezoid and simpson rule (if you implement them
like I did) evaluate the function in 𝑛 + 1 points. The gaussian2 evaluates the function in 2𝑛 points,
and the gaussian3 evaluates the function in 3𝑛 point. To make the competition fair, let us adapt
the value of 𝑛 accordingly.
[ ]: println("Exact value: ", Float64(�))
circle(x) = 2*sqrt(1-x^2)
println("Approximation by trapezoid rule: ", trapezoid(circle,-1,1,600))
println("Approximation by Simpson rule: ", simpson(circle,-1,1,600))
println("Approximation by Gaussian quadrature with two points: ",␣
↪gaussian2(circle,-1,1,300))
4
The function gaussian3 has a higher degree of accuracy in theory than simpson or gaussian2.
However, to really apreciate its benefit, we would have to be integrating with a larger number of
points. In practice, we get a nearly exact solution with any of these three methods if we use more
than 150 points. So, the benefit of gaussian3 is harder to observe.
[ ]: