Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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, -\[Infinity], 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, -\[Infinity], 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.

share|improve this question
    
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. –  James Matta Oct 9 '12 at 17:12
add comment

2 Answers

up vote 17 down vote accepted

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 *)
share|improve this answer
    
This seems to be working on my actual problem. I'm still waiting on the result. Thank you. –  jmlopez Sep 12 '12 at 19:36
    
Took a while but it finished. :) –  jmlopez Sep 12 '12 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. –  b.gatessucks Sep 13 '12 at 6:46
    
@b.gatessucks If the inner ones have an analytic solution that also evaluates quickly, then yes, doing so might be worthwhile. But I've come across some integrals that have a nice analytical solution, but take a long time to compute whereas NIntegrate gives it instantly. Also, N@Integrate will give the same result as NIntegrate only if the function is well behaved within the default options in NIntegrae (e.g., not highly oscillatory, requiring increased precisions/recursions, etc). So N@Integrate only applies N to the result, whereas NIntegrate will use different algorithms –  rm -rf Sep 13 '12 at 23:16
1  
Thank you for this answer it has solved my problem perfectly (and saved me the trouble of asking a question to boot :) ) Thanks again. –  James Matta Oct 9 '12 at 17:14
add comment

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, -\[Infinity], 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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