# Nested NIntegrate

Suppose that we have the given simple integral expression

$$\int_{-5}^{5} x \int_{-\infty}^{x} e^{\int_{0}^{z} -y dy} dz dx$$

Writing this out in Mathematica we obtain:

Integrate[x Integrate[Exp[Integrate[-y, {y, 0, z}]], {z, -∞, x}], {x, -5., 5}]
30.0795


### Question:

Is it possible to do a numerical integration on this expression by using NIntegrate?

A very naive attempt gives us the following errors:

NIntegrate[x NIntegrate[Exp[NIntegrate[-y, {y, 0, z}]], {z, -∞, x}], {x, -5, 5}]


NIntegrate::nlim: y = z is not a valid limit of integration

Notice that we want everything to be a numerical integration, this includes the inner integrals.

The problem is that one of the NIntegrates is an argument to the exponential function and this does not allow us to write the double integral with only one NIntegrate as mentioned in here

### Motivation

I'm trying to evaluate an expression that is too complicated for Mathematica to do symbolically and it is composed on integrals of the kind mentioned above.

• Thank you so much for asking this question, I have a problem exactly like yours where the bounds of the inner integral are dependent on the current value of the outer integral and it was blowing up in my face. This helped a lot. Thanks again. Commented Oct 9, 2012 at 17:12

You can always separate your inner integrals, convert them to functions and use in NIntegrate:

i1[z_?NumericQ] := i1[z] = NIntegrate[-y, {y, 0, z}]
i2[x_?NumericQ] := i2[x] = NIntegrate[Exp[i1[z]], {z, -∞, x}]
NIntegrate[x i2[x], {x, -5., 5}]
(* 30.0795 *)

• This seems to be working on my actual problem. I'm still waiting on the result. Thank you. Commented Sep 12, 2012 at 19:36
• Took a while but it finished. :) Commented Sep 12, 2012 at 19:43
• @R.M. Could one use N[Integrate[...]] in case some of the inner integrals have an analytic solution ? If not, N[Integrate[...]]=NIntegrate[...] anyways. Commented Sep 13, 2012 at 6:46
• Thank you for this answer it has solved my problem perfectly (and saved me the trouble of asking a question to boot :) ) Thanks again. Commented Oct 9, 2012 at 17:14
• Curiously it works only with ?NumericQ. So, it looks like NIntegrate gives numerical values only if the check ?NumericQ is inserted, which looks strange to me. Does anyone has an explanation why it is like this? Commented Feb 3, 2015 at 22:48

It is possible like so for example:

ClearAll[fn];
fn[z_?NumericQ] := Exp[NIntegrate[-y, {y, 0, z}]];

NIntegrate[x fn[z], {x, -5, 5}, {z, -∞, x}]

(*  30.0795  *)


but it takes a while to compute.

I used the ability of NIntegrate to handle non-rectangular domains, which is very powerful but not widely known and / or appreciated, it seems. Note that the answers which numericalize all dimensions separately may not pick the optimal integration grid in multi-dimensional case, in which class of cases (and this includes the case in question) this form may generally work better.