47
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Commented Oct 9, 2012 at 17:12

2 Answers 2

39
$\begingroup$

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 *)
$\endgroup$
8
  • $\begingroup$ This seems to be working on my actual problem. I'm still waiting on the result. Thank you. $\endgroup$
    – jmlopez
    Commented Sep 12, 2012 at 19:36
  • $\begingroup$ Took a while but it finished. :) $\endgroup$
    – jmlopez
    Commented Sep 12, 2012 at 19:43
  • 1
    $\begingroup$ Thank you for this answer it has solved my problem perfectly (and saved me the trouble of asking a question to boot :) ) Thanks again. $\endgroup$ Commented Oct 9, 2012 at 17:14
  • 2
    $\begingroup$ 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? $\endgroup$
    – Wizzerad
    Commented Feb 3, 2015 at 22:48
  • 1
    $\begingroup$ Note that if you have defined a function without the ?NumericQ, you need to clear the function definition (easiest method is to restart Kernel) after adding the ?NumericQ, or it will not work. $\endgroup$
    – a20
    Commented Mar 2, 2020 at 15:09
21
$\begingroup$

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.

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.