0% found this document useful (0 votes)
9 views

hw6

The document outlines exercises related to numerical integration, including proving the midpoint rule's accuracy for polynomials and implementing various numerical integration methods such as the trapezoid rule, Simpson's rule, and Gaussian quadrature. It also includes code examples for each method and tests their accuracy in approximating the value of π. The document concludes by discussing the performance of these methods and their accuracy with different functions.

Uploaded by

bradley.c.yu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

hw6

The document outlines exercises related to numerical integration, including proving the midpoint rule's accuracy for polynomials and implementing various numerical integration methods such as the trapezoid rule, Simpson's rule, and Gaussian quadrature. It also includes code examples for each method and tests their accuracy in approximating the value of π. The document concludes by discussing the performance of these methods and their accuracy with different functions.

Uploaded by

bradley.c.yu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

hw6

February 14, 2024

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 𝑥𝑛 ).

[ ]: function trapezoid(f::Function, a::Real, b::Real, n::Integer=100)


# numerical integration using the trapezoid rule
h = (b - a) / n
s = 0.5 * (f(a) + f(b))
for i = 1:n-1
s += f(a + i*h)
end
return h * s

end

[ ]: trapezoid (generic function with 2 methods)

Let us verify that $ �_0^1 x^2 dx = 1/3 $.

[ ]: 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 < ⋯ < 𝑥𝑛 = 𝑏

[ ]: function simpson(f::Function, a::Real, b::Real, n::Integer=100)


# numerical integration using Simpson's rule
h = (b - a) / n
s = f(a) + f(b)
for i = 1:n-1
s += 2 * f(a + i*h) * (i % 2 + 1)
end
return h * s / 3
end

[ ]: simpson (generic function with 2 methods)

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

[ ]: gaussian2 (generic function with 2 methods)

[ ]: # The following computation should also be exact


gaussian2(x->x^2,0,1)

[ ]: 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

[ ]: gaussian3 (generic function with 2 methods)

[ ]: # The following computation should also be exact


gaussian3(x->x^2,0,1)

[ ]: 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

Let us test our algorithms to see what we get.


[ ]: println("Exact value: ", Float64(�))
circle(x) = 2*sqrt(1-x^2)
println("Approximation by trapezoid rule:", trapezoid(circle,-1,1))
println("Approximation by Simpson rule:", simpson(circle,-1,1))
println("Approximation by Gaussian quadrature with two points:",␣
↪gaussian2(circle,-1,1))

println("Approximation by Gaussian quadrature with three points:",␣


↪gaussian3(circle,-1,1))

Exact value: 3.141592653589793


Approximation by trapezoid rule:3.1382685110984996
Approximation by Simpson rule:3.140292577830334

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))

println("Approximation by Gaussian quadrature with three points: ",␣


↪gaussian3(circle,-1,1,200))

Exact value: 3.141592653589793


Approximation by trapezoid rule: 3.141366358851326
Approximation by Simpson rule: 3.141504258208086
Approximation by Gaussian quadrature with two points: 3.141615112264936
Approximation by Gaussian quadrature with three points: 3.141606877131482
I am getting approximately four digits of accuracy with the four algorithms. This is not a very
significant difference between the four algorithms, and it is maybe less that we would have expected.
Can you explain why the accuracy is so low in this example?
Let us now compute a different function as a way to approximate 𝜋. We will use the following
identity.
1
2
𝜋=∫ 𝑑𝑥.
−1 1 + 𝑥2

[ ]: println("Exact value: ", Float64(�))


g(x) = 2/(1+x^2)
println("Approximation by trapezoid rule: ", trapezoid(g,-1,1,60))
println("Approximation by Simpson rule: ", simpson(g,-1,1,60))
println("Approximation by Gaussian quadrature with two points: ",␣
↪gaussian2(g,-1,1,30))

println("Approximation by Gaussian quadrature with three points: ",␣


↪gaussian3(g,-1,1,20))

Exact value: 3.141592653589793


Approximation by trapezoid rule: 3.141407468407329
Approximation by Simpson rule: 3.1415926535353598
Approximation by Gaussian quadrature with two points: 3.141592653628502
Approximation by Gaussian quadrature with three points: 3.141592653560033
In this case, the Simpson rule, and the two Gaussian quadrature rules, give us a much better
approximation than the trapezoid rule. The approximations are accurate to several digits even
though we used very few points.

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.

[ ]:

You might also like