Path: blob/master/material/numerical-calculus-integration.ipynb
934 views
Throughout this section and the next ones, we shall cover the topic of numerical calculus. Calculus has been identified since ancient times as a powerful toolkit for analysing and handling geometrical problems. Since differential calculus was developed by Newton and Leibniz (in its actual notation), many different applications have been found, at the point that most of the current science is founded on it (e.g. differential and integral equations). Due to the ever increasing complexity of analytical expressions used in physics and astronomy, their usage becomes more and more impractical, and numerical approaches are more than necessary when one wants to go deeper. This issue has been identified since long ago and many numerical techniques have been developed. We shall cover only the most basic schemes, but also providing a basis for more formal approaches.
From 7 (Chapter 18)
We can only find anti-derivatives for a very small number of functions, such as those functions that are popular as problems in mathematics textbooks. Outside of math classes, these functions are rare
Bibliograpy
Thomas J. Sargent John Stachurski, Python Programming for Quantitative Economics GitHub
Numerical Integration
Integration is the second fundamental concept of calculus (along with differentiation). Numerical approaches are generally more useful here than in differentiation as the antiderivative procedure (analytically) is often much more complex, or even not possible. In this section we will cover some basic schemes, including numerical quadratures.
Geometrically, integration can be understood as the area below a funtion within a given interval. Formally, given a function such that , the antiderivative is defined as
valid for all in . However, a more useful expression is a definite integral, where the antiderivative is evaluated within some interval, i.e.
This procedure can be formally thought as a generalization of discrete weighted summation. This idea will be exploited below and will lead us to some first approximations to integration.
See pag. 66 of PDF
Numpy abstractions
The programming paradigm with Numpy are the abstractions, where the algorithms are written in terms of operations with arrays, avoiding the use of programming loops. Low level operations between arrays are implemented in C/Fortran within Numpy. Therefore, a code with abstractions is automatically optimized.
Consider for example the numerical integration with the trapezoidal method
Wehere for each internval between and , the average height of the function is used. Therefore
To implement the correspponding abstractions in Numpy it is convenient to define the following vectors The numerical approximation of the integral with the trapezoidal method is then just the dot product between the two previous vectors
We next present the code with different levels of abstractions. The faster is the first one with the direct implementation of the dot product.
Example:
Implementation in scipy
Parameters:
See integrate.quad?
for further details
Usage example
Note that:
Even so, the full tuple output with result and numerical error is obtained from
Unix time in seconds
Force output to have only the result by asking for the entry 0 of the output tuple, note the [0]
at the end:
Avoid singularities in custom solutions
Sinintegrate:
→ s*i*n*x
En un texto científico las operaciones matemáticas se deben diferenciar del texto normal, por ejemplo, yo puedo decir abc de abcedario, pero si digo eso quiere decir a*b*c
. Por eso hay que usar modo matématico incluso para referirse a la variable que es diferenta a la letra a
When you write LaTeX
in matplotlib
, you must be sure that the backslash, '', is properly interpreted as the key for LaTeX
commands. This is accomplished by writting an r
before the string declaration. Note the different behaviour of the backslash in the next two cells of code. In the first one \t
is intepreted as the keyboard key <TAB>
and \b
as an empty space, while in the second, with the prefix r
, it is just a pair of LaTeX
commands
LaTeX
La + TeX
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, , by using:
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: , and .
Use composite Simpson's rule to integrate and compare it with the analytical expression in case you can get it. The superior limit in the integral corresponds to the actual redshift . What is happening to our universe?
Activity
Using the Simpson's adaptive quadrature determine the value of the next integral with a precision of float32.
Activity
Fresnel integrals are commonly used in the study of light difraction at a rectangular aperture, they are given by:
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.
Improper Integrals
Improper integral can be processed with the quad
function of scipy.integrate
, or other integration functions, by using the infinity implementation of numpy
, (which is the same of the math
module, see below)
Positive infinity ():
np.inf
and several aliasesNegagite infinity ():
np.NINF
For details of the implementation and the aliases see here
In Python the can be defined as
It is already implemented in numpy
There are also function to check for infinity numbers:
So it is possible to evaluate improper integrals like
or
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. See for example here
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 polynomimal, 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
Error function is a special and non-elementary function that is widely used in probability, statistics and diffussion processes. It is defined through the integral:
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.