37
$\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$
  • $\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$ – James Matta Oct 9 '12 at 17:12
28
$\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$
  • $\begingroup$ This seems to be working on my actual problem. I'm still waiting on the result. Thank you. $\endgroup$ – jmlopez Sep 12 '12 at 19:36
  • $\begingroup$ Took a while but it finished. :) $\endgroup$ – jmlopez Sep 12 '12 at 19:43
  • $\begingroup$ @R.M. Could one use N[Integrate[...]] in case some of the inner integrals have an analytic solution ? If not, N[Integrate[...]]=NIntegrate[...] anyways. $\endgroup$ – b.gates.you.know.what Sep 13 '12 at 6:46
  • 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$ – James Matta Oct 9 '12 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 Feb 3 '15 at 22:48
18
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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