import integration_helpers as ih
import sympy as sp
Define $f$ as the function we want to integrate from $a$ to $b$ ($f$ has to be analytically integrable).
x = sp.Symbol('x')
f = sp.sin(x) * x**2
a = 7
b = 15
Plot function and fill out the integral.
fn = sp.lambdify(x, f, modules='numpy')
ih.plot_int(fn, a, b)
Generate a reference value for error calculations.
F_sympy = ih.int_sympy(f, a, b, x)
t_sympy = %timeit -q -o ih.int_sympy(f, a, b, x)
t_sympy = t_sympy.average * 1000
with $m_i$ as the midpoint of the $i$-th interval.
Plot function and rectangles one obtains while using the chained midpoint rule.
n_midpoint = 10
ih.plot_midpoint(fn, a, b, n_midpoint)
F_midpoint = ih.int_midpoint(fn, a, b, n_midpoint)
e_midpoint = 100 * (F_midpoint - F_sympy) / F_sympy
t_midpoint = %timeit -q -o ih.int_midpoint(fn, a, b, n_midpoint)
t_midpoint = t_midpoint.average * 1000
Plot function and trapezoids one obtains while using the chained trapezoidal rule.
n_trapez = 10
ih.plot_trapez(fn, a, b, n_trapez)
F_trapez = ih.int_trapez(fn, a, b, n_trapez)
e_trapez = 100 * (F_trapez - F_sympy) / F_sympy
t_trapez = %timeit -q -o ih.int_trapez(fn, a, b, n_trapez)
t_trapez = t_trapez.average * 1000
with $\xi_i$ as the roots of the Legendre-polynomial $$P_n(x) = \sum_{i=0}^{\left \lfloor \frac{n}{2} \right \rfloor} (-1)^k \cdot \frac{(2n-2k)!}{2^n \cdot k! \cdot (n-k)! \cdot (n-2k)!} \cdot x^{n-2k}$$ and the weights $$\omega_i = \frac{2}{(1-\xi_i^2) \cdot P_n^\prime(\xi_i)^2}$$
n_gauss = 10
F_gauss = ih.int_gauss(fn, a, b, n_gauss)
e_gauss = 100 * (F_gauss - F_sympy) / F_sympy
t_gauss = %timeit -q -o ih.int_gauss(fn, a, b, n_gauss)
t_gauss = t_gauss.average * 1000
with $x_i$ as randomly sampled values in the interval $[a, b]$.
Plot function and randomly sampled lines (just for demonstration, they will not be the same in int_mc).
n_mc = 1000
ih.plot_mc(fn, a, b, n_mc)
F_mc = ih.int_mc(fn, a, b, n_mc)
e_mc = 100 * (F_mc - F_sympy) / F_sympy
t_mc = %timeit -q -o ih.int_mc(fn, a, b, n_mc)
t_mc = t_mc.average * 1000
The documentation states that an integration technique from the Fortran library QUADPACK will be used.
F_quad = ih.int_quad(fn, a, b)
e_quad = 100 * (F_quad - F_sympy) / F_sympy
t_quad = %timeit -q -o ih.int_quad(fn, a, b)
t_quad = t_quad.average * 1000