# Integration ## Newton-Cotes methods A related problem to interpolation is when we need to calculate the integral of a function $$I = \int_{x_1}^{x_N} f(x) dx$$ given values of the function at a discrete set of points $x_1\dots x_N$, which we'll write as $f_i\equiv f(x_i)$. For simplicity here we'll assume that the spacing between points on the grid is constant $x_{i+1}-x_i = \Delta x$, but it's straighforward to generalize to non-uniform sampling if you need to. To estimate the value of the integral, we need to model how the function behaves in each interval $x_i>> np.polynomial.legendre.leggauss(2) (array([-0.57735027, 0.57735027]), array([1., 1.])) ``` gives the $N=2$ values. In this case, the weights are both 1, and the $x$ values are $\pm 1/\sqrt{3}$. ```{admonition} Exercise: Gaussian quadrature Using [`numpy.polynomial.legendre.leggauss`](https://numpy.org/doc/stable/reference/generated/numpy.polynomial.legendre.leggauss.html) to get the locations and weights for different choices of $N$, implement Gaussian quadratures. Check that the answer is indeed exact for polynomials of degree $2N-1$ or less. What happens for higher order polynomials? Try a function that is not a simple polynomial, e.g. $e^{-x^2}$. What is the error in the approximation and how does it scale with $N$? Hints: - to get the correct value of the integral to compare with to see how accurate your approximation is, you could use the general purpose integrator [`scipy.integrate.quad`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html) - to generate a polynomial with degree `N` and random coefficients between -10 and +10 (for example) you can use `np.polynomial.Polynomial(np.random.randint(-10,high=10,size=N+1))` ``` Gaussian quadratures can be applied more generally to integrals of the form $$\int W(x) f(x) dx$$ for some weight function $W(x)$ (in the previous example we had $W(x)=1$). We write the integral as a sum $$\int W(x) f(x) dx= \sum_{i=1}^N w_i f(x_i)$$ and find the choices of $w_i$ and $x_i$ that make the sum exact for a polynomial of degree $2N-1$ or less. One example is $W(x)=e^{-x^2}$, ie. integrals $$\int_0^\infty e^{-x^2} f(x) dx.$$ In this case, the weights and locations are given by [`numpy.polynomial.hermite.hermgauss`](https://numpy.org/doc/stable/reference/generated/numpy.polynomial.hermite.hermgauss.html). [The locations $x_i$ are the roots of the $N$th Hermite polynomial, which are the polynomials that are orthogonal under an inner product defined with weight function $W(x)$.] ```{admonition} Exercise: Gauss-Hermite Modify your code to use the Gauss-Hermite coefficients and check that you can get an exact answer for the integral of $e^{-x^2}$. Hint: If you want to use `scipy.integrate.quad` again to get the value of the integral as a comparison, note that you can give it limits of $-\infty$ to $+\infty$ using `-np.inf` and `np.inf`. ``` Other examples are - $W(x)=e^{-x}$ with integration limits $0$ to $\infty$. In this case, we need Gauss-Laguerre integration -- see [`numpy.polynomial.laguerre.laggauss`](https://numpy.org/doc/stable/reference/generated/numpy.polynomial.laguerre.laggauss.html) - $W(x)=1/\sqrt{1-x^2}$ from $x=-1$ to $1$ is Gauss-Chebyshev quadrature -- see [`numpy.polynomial.chebyshev.chebgauss`](https://numpy.org/doc/stable/reference/generated/numpy.polynomial.chebyshev.chebgauss.html) ## Integration challenge ```{admonition} Exercise: Average velocity of the Maxwell-Boltzmann distribution. Use Simpson's rule, Gaussian quadrature, and the general purpose integrator [`scipy.integrate.quad`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html) to evaluate the average velocity $\langle\left|v\right|\rangle$ for the 3D [Maxwell-Boltzmann distribution](https://en.wikipedia.org/wiki/Maxwell–Boltzmann_distribution). For each method, check the numerical error comparing to the analytic result. How many points do you need to get to $0.1$% accuracy? For Simpson's rule you can use your own implementation from above or you could try [`scipy.integrate.simpson`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.simpson.html). For Gaussian quadrature, try both Gauss-Hermite and Gauss-Laguerre. Which one is best? ``` ## Further reading - Integration methods are covered in Chapter 7 of Gezerlis. - Overview of [`scipy.integrate'](https://docs.scipy.org/doc/scipy/tutorial/integrate.html) - [QUADPACK](https://en.wikipedia.org/wiki/QUADPACK) is the Fortran 77 library that is used by `scipy.integrate.quad` under the hood.