# 2D integration over non-rectangular domain using cubature

**URL:** <https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991>\
**Category:** New to Julia\
**Created:** [March 31, 2017, 5:29pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991 "2017-03-31T17:29:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [March 31, 2017, 5:29pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/1 "2017-03-31T17:29:00Z")

</div>

Hello everyone,

I have a very basic question.  
I’m looking for the most efficient way to perform the following very classical integral:

![](https://global.discourse-cdn.com/julialang/original/3X/8/6/86f4af4e52c1f747e1e79dc52b49b6a61d9840a0.png)

For now I’m using hquadrature of the cubature package in two steps.

This works, but I’m not sure whether this is the most efficient way.  
More generally, I would like to know, how to perform multidimensional integration over non-rectangular domains.  
It seems cubature does not allow bounds to be functions. Am I correct?

Many thanks in advance  
Olivier

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 31, 2017, 5:32pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/2 "2017-03-31T17:32:10Z")

</div>

You can use a change of variables in general. However, since your problem is separable in `x` and `u`, you should take advantage of that, eg approximate the integral of `g(u)du` from `0` to `x` as some `G(x)`, similarly for `f`, and then `F(a)G(x)` is your answer.

(BTW, does the forum allow LaTeX now? or was that inserted as an image?)

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [March 31, 2017, 5:38pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/3 "2017-03-31T17:38:32Z")

</div>

Thanks for your answer,  
Ok, I will stick with two-step approach then.

BTW, yes, it’s an image. 🙂

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [April 5, 2017, 5:59pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/4 "2017-04-05T17:59:49Z")

</div>

Why is the problem separable? The integration is over the `x` variable which is the limit of integration of the second integral.

In any case, your suggestion has been accepted.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 5, 2017, 6:22pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/5 "2017-04-05T18:22:00Z")

</div>

> [@mzaffalon](#):
>
> Why is the problem separable? The integration is over the x variable which is the limit of integration of the second integral.

The way I read this notation is that the `dx` closes the scope for the `x`, so the second `x` is a free variable. I am aware that some people follow a different convention, but I find that confusing.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 5, 2017, 8:43pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/6 "2017-04-05T20:43:48Z")

</div>

It’s quite natural, once you get used to it, to think of as “\int dx” as the operator “integrate the following function with respect to x”

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 6, 2017, 12:38am UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/7 "2017-04-06T00:38:22Z")

</div>

> [@Olivier\_Merchiers](#):
>
> Ok, I will stick with two-step approach then.

I would recommend trying a change of variables to turn the integration region into a rectangle. For example, let y=u/x, so that the integral becomes

![](https://global.discourse-cdn.com/julialang/original/3X/3/5/354702f3d5277df2b3e82de4240b8beda7e6f204.jpeg)

In general, a 2d cubature code can be more efficient than nested 1d quadratures, assuming you are using adaptive quadrature/cubature. This is because the inner 1d integral, if it is performed as an independent 1d adaptive quadrature, can waste a lot of integrand evaluations trying to refine the 1d integral to high relative accuracy even if its overall contribution to the integral is small.

(There are also specialized cubature algorithms for triangular domains, but I’ve never seen an implementation of a nested/adaptive one.)

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [April 6, 2017, 7:03pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/8 "2017-04-06T19:03:39Z")

</div>

Thank you very much @stevengj for your answer.  
That was exactly the information I was looking for.

I know in matlab you can actually use a function handle to define an integration region.  
I was wondering if something similar was possible with one of the julia packages.

Thanks again

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 6, 2017, 8:38pm UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/9 "2017-04-06T20:38:40Z")

</div>

> [@Olivier\_Merchiers](#):
>
> I know in matlab you can actually use a function handle to define an integration region.

I’m not sure what you’re referring to — as far as I can tell, Matlab doesn’t include multi-dimensional integration routines.

---

<div class="post-metadata">

**Author:** ![Olivier\_Merchiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olivier_merchiers/32/4073_2.png) [@Olivier\_Merchiers](https://discourse.julialang.org/u/Olivier_Merchiers)\
**Post date:** [April 7, 2017, 9:04am UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/10 "2017-04-07T09:04:48Z")

</div>

This is what I found for Matlab R2017a

[https://fr.mathworks.com/help/matlab/ref/integral2.html#btbbuxy-3](https://fr.mathworks.com/help/matlab/ref/integral2.html#btbbuxy-3)

In older versions I used quad2d  
In both versions you could pass a function handle (anonymous function) as an integration bound.

I quickly checked the publications cited at the end of the page, but I have no idea whether matlab actually implements the algorithms descibed in these or relies on something else.

---

<div class="post-metadata">

**Author:** ![hesampour](https://avatars.discourse-cdn.com/v4/letter/h/e495f1/32.png) [@hesampour](https://discourse.julialang.org/u/hesampour)\
**Post date:** [February 17, 2019, 6:40am UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/11 "2019-02-17T06:40:33Z")

</div>

There is no way to compute non rectangular Integral in julia???  
this is the way that I do it in python:  
func = lambda x,y: x_y  
integ = integrate.dblquad(x2,0,t,lambda x :x, lambda y: t)[0]  
and this is the way that I do in matlab:  
func = @(x,y) x_y;  
integ = integral2(func,0,t,@(t1) t1,t);  
but I dont know how I can compute it in julia.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 24, 2019, 3:27am UTC](https://discourse.julialang.org/t/2d-integration-over-non-rectangular-domain-using-cubature/2991/12 "2019-02-24T03:27:59Z")

</div>

> [@hesampour](#):
>
> There is no way to compute non rectangular Integral in julia???

As I explained when you asked about this in [an HCubature.jl issue](https://github.com/stevengj/HCubature.jl/issues/16), just do a change of variables to transform to a rectangular region.

For example, to integrate f(x,y) for x=0…t and y=0…x (i.e. a triangle), just do the change of variables y=ux and integrate `f(x,u*x)*x` for x=0…t , u=0…1 (note the Jacobian factor `*x` multiplying `f`).
