Appendix A. Integration
Composite Numerical Integration
Although above-described methods are good enough when we want to integrate along small intervals, larger intervals would require more sampling points, where the resulting Lagrange interpolant will be a high-order polynomial. These interpolant polynomials exihibit usually an oscillatory behaviour (best known as Runge's phenomenon), being more inaccurate as we increase .
An elegant and computationally inexpensive solution to this problem is a piecewise approach, where low-order Newton-Cotes formula (like trapezoidal and Simpson's rules) are applied over subdivided intervals. This methods are already implemented in the previous scipy
trapezoidal and Simpsons implementations. An internal implementation is given in the Appendix of integration
Composite trapezoidal rule
This formula is obtained when we subdivide the integration interval within sets of two points, such that we can apply the previous Trapezoidal rule to each one.
Let be a well behaved function (), defining the interval space as , where N is the number of intervals we take, the Composite Trapezoidal rule is given by:
for some value in .
Composite Simpson's rule
Now, if we instead divide the integration interval in sets of three points, we can apply Simpson's rule to each one, obtaining:
for some value in .
Activity
Take the previous routine CompositeQuadrature and the above function and explore high-order composites quadratures. What happens when you increase the number of points?
Activity
An experiment has measured , the number of particles entering a counter, per unit time, as a function of time. Your problem is to integrate this spectrum to obtain the number of particles that entered the counter in the first second
For the problem it is assumed exponential decay so that there actually is an analytic answer.
Compare the relative error for the composite trapezoid and Simpson rules. Try different values of N. Make a logarithmic plot of N vs Error.
Adaptive Quadrature Methods
Calculating the integrate of the function within the interval , we obtain:
Using composite numerical integration is not completely adequate for this problem as the function exhibits different behaviours for differente intervals. For the interval the function varies noticeably, requiring a rather small integration interval . However, for the interval variations are not considerable and low-order composite integration is enough. This lays a pathological situation where simple composite methods are not efficient. In order to remedy this, we introduce an adaptive quadrature methods, where the integration step can vary according to the interval. The main advantage of this is a controlable precision of the result.
Simpson's adaptive quadrature
Although adaptive quadrature can be readily applied to any quadrature method, we shall cover only the Simpson's adaptive quadrature as it is more than enough for most problems.
Let's assume a function . We want to compute the integral within the interval . Using a simple Simpson's quadrature, we obtain:
where we introduce the notation:
and is simply .
Now, instead of using an unique Simpson's quadrature, we implement two, yielding:
For this expression, we reasonably assume an equal fourth-order derivative , where is the estimative for the first subtinterval (i.e. ), and for the second one (i.e. ).
As both expressions can approximate the real value of the integrate, we can equal them, obtaining:
which leads us to a simple way to estimate the error without knowing the fourth-order derivative, i.e.
If we fix a precision , such that the obtained error for the second iteration is smaller
it implies:
and
The second iteration is then times more precise than the first one.
Steps Simpson's adaptive quadrature
1. Give the function to be integrated, the inverval and set a desired precision .
2. Compute the next Simpsons's quadratures:
3. If
then the integration is ready and is given by:
within the given precision.
4. If the previous step is not fulfilled, repeat from step 2 using as new intervals and and a new precision . Repeating until step 3 is fulfilled for all the subintervals.
Activity
Activity
These integrals cannot be solved using analitical methods. Using the previous routine for adaptive quadrature, compute the integrals with a precision of for values of . Create two arrays with those values and then make a plot of vs . The resulting figure is called Euler spiral, that is a member of a family of curves called Clothoid loops.
Activity
Models of Universe
From the Friedmann equations can be found the dynamics of the Universe, i.e., the evolution of the expansion with time that depends on the content of matter and energy of the Universe. Before introducing the general expression, there are several quatities that need to be defined.
It is convenient to express the density in terms of a critical density given by
where is the Hubble constant. The critical density is the density needed in order the Universe to be flat. To obtained it, it is neccesary to make the curvature of the universe . The critical density is one value per time and the geometry of the universe depends on this value, or equally on . For a universe with it would ocurre a big crunch(closed universe) and for a there would be an open universe.
Now, it can also be defined a density parameter, , a normalized density
where is the actual density() for the component . Then, it can be found the next expression
where , and are the matter, radiation and vacuum density parameters. And is the total density including the vacuum energy.
This expression can also be written in terms of the expansion or scale factor, , rather than the redshift, , due to the expression: , and it can be simplified in several ways.
For the next universe models, plot time( units) vs the scale factor:
-Einstein-de Sitter Universe: Flat space, null vacuum energy and dominated by matter
-Radiation dominated universe: All other components are not contributing
-WMAP9 Universe
You can take the cosmological parameters from the link
http://lambda.gsfc.nasa.gov/product/map/dr5/params/lcdm_wmap9.cfm or use these ones: = 0.266, and .
Use composite simpson rule to integrate and compare it with the analitical expression in case you can get it. The superior limit in the integral corresponds to the actual redshift . What is happening to our universe?
Improper Integrals
Although the previous integration methods can be applied in almost every situation, improper integrals pose a challenger to numerical methods as they involve indeterminations and infinite intervals. Next, we shall cover some tricks to rewrite improper integrals in terms of simple ones.
Left endpoint singularity
Assuming a function such that it can be rewritten as
the integral over an interval converges only and only if .
Using Simpson's composite rule, it is possible to compute the fourth-order Taylor polynomial of the function at the point , obtaining
The integral can be then calculated as
The second term is an integral of a polynimal, which can be easily integrated using analytical methods.
The first term is no longer pathologic as there is not any indetermination, and it can be determined using composite Simpson's rule
Right endpoint singularity
This is the contrary case, where the indetermination is present in the extreme of the integration interval . For this problem is enough to make a variable substitution
With this, the right endpoint singularity becomes a left endpoint singularity and the previous method can be directly applied.
Infinite singularity
Finally, infinite singularities are those where the integration domain is infinite, i.e.
this type of integrals can be easily turned into a left endpoint singularity jus making the next variable substitution
yielding
Activity
Using the substitution it is possible to use the previous methods for impropers integrals in order to evaluate the error function. Create a routine called ErrorFunction
that, given a value of , return the respective value of the integral.