Reputation: 21
I want to calculate double integral with HCubature, but one of the variables' limit depends on the other. For example:
And I've tried primitively:
using HCubature
F(x, y) = x+y
hcubature(r->F(r[1], r[2]), [0,0], [r[2], 1])
and of course it doesn't work and gives an error:
ERROR: LoadError: UndefVarError: r not defined
How can I calculate such integral? Maybe using another package? If so, which package and how to do this?
Upvotes: 1
Views: 212
Reputation: 1058
Use a change of variables: see the links here
I'm the author of the HCubature.jl package, and this is the way to handle non-rectangular domains in such methods. In this example, you make the explicit change of variables x=uy (dx = y du) then integrate u from 0 to 1:
julia> using HCubature
julia> hcubature(r -> F(r[1]*r[2], r[2]) * r[2], (0,0), (1,1))
(0.5, 1.6653345369377348e-16)
Upvotes: -1
Reputation: 18227
This is an iterated integral. hcubature
can be used to calculate the integrals from the inside out, like so:
julia> F(x, y) = x+y
F (generic function with 1 method)
julia> hcubature(r->hcubature(s->F(s[1],r[1]), [0], [r[1]])[1], [0], [1])
(0.4999999999999999, 1.1102230246251565e-16)
So the answer is 0.5
. In case the semantics is unclear, the integration is over a triangle (0,0)-(1,0)-(1,1). The function ranges from 0 to 2 on this triangle and so the answer is reasonable.
Upvotes: 1