An oscillatory integrandΒΆ

This function gets increasingly oscillatory near the right endpoint.

using FundamentalsNumericalComputation
f = x -> (x+1)^2*cos((2*x+1)/(x-4.3))
plot(f,0,4,label="", xlabel="x", ylabel="f(x)")

Accordingly, the trapezoid rule is more accurate on the left half of the interval than on the right half.

n = @. 50*2^(0:3)
Tleft = []; Tright = [];
for (i,n) = enumerate(n)
    Tleft = [ Tleft; FNC.trapezoid(f,0,2,n)[1] ] 
    Tright = [ Tright; FNC.trapezoid(f,2,4,n)[1] ] 
end
left_val,err = quadgk(f,0,2,atol=1e-14,rtol=1e-14);
right_val,err = quadgk(f,2,4,atol=1e-14,rtol=1e-14);
table = (n=n, left=Tleft.-left_val, right=Tright.-right_val)
pretty_table(table,["n" "left error" "right error"],backend=:html)
n left error right error
50 -0.0024910644116187264 0.504229291780792
100 -0.000622714716272732 0.0960043735770828
200 -0.0001556754648674996 0.02254727275248758
400 -3.8918665289155996e-5 0.005554222496781058

Both the picture and the numbers suggest that more nodes should be used on the right half of the interval than on the left half.