JuliaMath / Cubature.jl

One- and multi-dimensional adaptive integration routines for the Julia language
Other
126 stars 21 forks source link

Suggest a simple example of change of variables. #42

Closed a2468834 closed 5 years ago

a2468834 commented 5 years ago

Although there is a link introducing how to apply change of variables method to compute integrals over infinite or semi-infinite domains, I think taking an example would be more friendly way to explain how to use it.

If f(x)=1/(x^2), we could calculate the integral of f(x) over [a, ∞] by hands in advance.

To compute an integral over a semi-infinite interval, you have to perform the change of variables x=a+t/(1-t):

Now we could turn all of them into Julia script.

a = 1 # assume that the lower bound of integral is 1.

function f(x)
    return 1/(x^2)
end

function g(a, t)
    return f(a+(t/(1-t)))*(1/(1-t)^2)
end

hquadrature(g, 0, 1)

returning (1.0, 1.1102230246251565e-14) which are the same as our calculation by hands.

a2468834 commented 5 years ago

Another example about infinite interval integral.

If f(x)=e^(-x^2), we could calculate the integral of f(x) over [-∞, ∞] by hands in advance.

To compute an integral over a infinite interval, you have to perform the change of variables x=t/(1-t²):

Now we could turn all of them into Julia script.

function f(x)
    return exp(-x^2)
end

function g(t)
    return f(t/(1-t^2))*(1+t^2)/((1-t^2)^2)
end

hquadrature(g, -1, 1)

returning (1.772453850905516, 2.95326836403382e-9) which are the same as our calculation by hands.

stevengj commented 5 years ago

Duplicate of #32