Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Numerical Integration (Quadrature) using Python
Sometimes you'll encounter definite integrals that cannot be solved analytically. The scipy library for Python contains numerous functions for scientific computing and data analysis, which include a few algorithms for finding the definite integrals of functions. The quad function which determines where the integral is more difficult to calculate and expends more effort there. The examples below demonstrate a few common uses of the function.
The following lines import the functions for integrating, plotting, and vectorization.
Suppose that you want to calculate the integral The integrand must be defined as a function (called intgrd1 below). Note that the argument of the cos function is in radians. In the simplest case, the integrand only depends on the variable of integration. The arguments of the quad function are the integrand, the lower limit, and the upper limit. It returns both the result of the integration and an estimate of the error.
It is easy to separate the two outputs as shown below. This is useful if you want to use the result in a calculation.
If you want to find the integral for several different upper limits, you can define the integral as a function of the upper limit (called intgrl1 below). This function can be called for various upper limits.
If you wanted to make a graph of the integral as a function of the upper limit, You might try to make a list of upper limits, then call the intgrl1 function to get a list of results with the various upper limits. However, that will not work, because it is not a "vectorized" function which can handle a list of inputs. (Many Python functions like cos, which returns the cosine of the argument, are vectorized.) The lines below make vectorized version of the function, which has a different name (vec_intgrl1) than the original function. It returns the integral for each upper limit in the list (th_upper) and their uncertainty estimates.
To create plot of the integral vs. the upper limit, send the results to a separate list from the errors. In order to get a smooth curve, the integral is calculated for a larger list of upper limits.
It is also possible to perform an integral that depends on another parameter in addition to the variable of integration. For example, you could calculate the integral for different values of . The function intgrnd2 is defined as a function of the integration varible (this must be the first argument) and the parameter . The function intgrl2 returns the integral as a funciton of the parameter (passed along using the args argument in the call to the quad function), with the limits of the integration fixed. After a vectorized function (called vec_intgrl2) is created, the values of the integral for the each parameter in a list can be found. This makes it easy to plot the integral vs. the parameter.
Exercise
The electric field a distance z above the center of a segment of length 2L with uniform charge per length λ is vertical and its magnitude is .
- If you aren't give the values of all of the variables (L and λ in this case), it is best to work with quantities that don't have units. Find an expression (integral) for the unitless quantity EL/kλ, where k =1/4πε0.
- Plot EL/kλ vs. Z/L for values of Z/L from 0.5 to 10.0. (Have the computer solve the integral numerically!)
Additional Documentation
More information and examples are available at https://docs.scipy.org/doc/scipy/tutorial/integrate.html