# A question about implementing double integral

**URL:** https://discourse.julialang.org/t/a-question-about-implementing-double-integral/111914
**Category:** Numerics
**Tags:** question, calculus
**Created:** [March 21, 2024, 2:56am UTC](https://discourse.julialang.org/t/a-question-about-implementing-double-integral/111914 "2024-03-21T02:56:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![swsw](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@swsw](https://discourse.julialang.org/u/swsw)
#### Post date: [March 21, 2024, 2:56am UTC](https://discourse.julialang.org/t/a-question-about-implementing-double-integral/111914/1 "2024-03-21T02:56:57Z")

</div>

I’d like to calculate the following double integral

Integrate[exp(-x)Integrate[exp(-y),{y,0,x}],{x,0,∞}]

![image](https://global.discourse-cdn.com/julialang/original/3X/b/b/bb419a7e99d6133404b482954fe5739a6bc55822.png)

I try the following in julia but i think i probably get it wrong.

> using QuadGK  
> inner\_function(y) = exp(-y)  
> outer\_function(x) = quadgk(y → inner\_function(y), 0, x)[1]  
> result, \_ = quadgk(outer\_function, 0, Inf)  
> println(“Integration result:”, result)

Would anyone help me with it? Thanks!

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [March 21, 2024, 10:10am UTC](https://discourse.julialang.org/t/a-question-about-implementing-double-integral/111914/2 "2024-03-21T10:10:36Z")

</div>

Hi! 🙂

First off, why do you think you got it wrong? It’s very useful to add what kind of errors or unexpected results you already found and it makes it easier for other people to help.

Second, I think the `outer_function` is missing multiplication by `exp(-x)` like so

```julia
outer_function(x) = exp(-x) * quadgk(inner_function, 0, x)[1]

```

Now this is the whole integrand you wrote down (note that you can shorten `y -> f(y)` to just `f` in general).

Other than that, it looks correct to me. After the change, I get

```julia-repl
julia> result, _ = quadgk(outer_function, 0, Inf)
(0.49999999999999994, 4.8229187783801426e-11)

```

---

<div class="post-metadata">

### Author: ![swsw](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@swsw](https://discourse.julialang.org/u/swsw)
#### Post date: [March 22, 2024, 2:48am UTC](https://discourse.julialang.org/t/a-question-about-implementing-double-integral/111914/4 "2024-03-22T02:48:50Z")

</div>

thank you! this is so nice! I didn’t realize i was almost there.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [March 22, 2024, 3:59am UTC](https://discourse.julialang.org/t/a-question-about-implementing-double-integral/111914/5 "2024-03-22T03:59:37Z")

</div>

Also note that for performance (if it matters), you generally want to set a tolerance for both integrals and use a stricter tolerance for the inner integral by a factor of ~10 to make sure that the outer integral doesn’t try to shape the inter integral.
