We will compute $$ \int_0^\pi \exp(x)\cos(x) dx $$ using Gauss-Legendre quadrature.
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
from scipy.integrate import fixed_quad
import numpy as np
import matplotlib.pyplot as plt
f = lambda x: np.exp(x)*np.cos(x)
a,b = 0.0,np.pi
qe = -0.5*(1.0 + np.exp(np.pi)) # Exact integral
N = np.arange(2,10)
err = np.zeros(len(N))
for (i,n) in enumerate(N):
val = fixed_quad(f,a,b,n=n)
err[i] = np.abs(val[0]-qe)
print('%5d %24.14e' % (n,err[i]))
plt.figure()
plt.semilogy(N,err,'o-')
plt.xlabel('n')
plt.ylabel('Error');
2 2.65864149305591e-01 3 5.70741337850613e-02 4 1.56826095079055e-04 5 1.77805009098364e-05 6 1.47122865001847e-08 7 1.14247988847183e-09 8 4.15667500419659e-13 9 2.84217094304040e-14
The error plot indicates exponential convergence.