Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a multidimensional integration of the form:

  somefunc[t] = NIntegrate[ otherfunc[x, y, z, t ],
  {z, z1[t], z2[t]}, {y, y1[t, z], y2[t, z]}, {x, x1[t, z, y], x2[t, z, y]}];

This needs to be evaluated only when $z2 > z1$, $y2 > y1$ and $x2 > x1$. However mathematica still evaluates the integrand backwards even if $y2 < y1$. My question is how to tell NIntegrate to simply give 0 in such cases and move on the next integration step. I tried to modify the upper integration boundary as follows:

 y2p[t, z] = Max[y1[t, z], y2[t, z]]

But this is not accepted by Mathematica as an integration boundary. How can I fix this?

share|improve this question
Please post your complete and valid code. – Mr.Wizard Mar 6 '12 at 10:10
Are you missing a pattern from the LHS? Looks like somefunc[t] = should be somefunc[t_] :=. It also looks like most answers have copied this problem without noticing it. – Szabolcs Mar 13 '12 at 5:21

A better option than using Boole would be to use Piecewise. Using that you can define a function that returns 0 when your conditions aren't met and otherfunc otherwise.

So, define a function otherfunc2 and integrate that:

otherfunc2[x_, y_, z_, t_] := 
   {otherfunc[x, y, z, t],
    z1[t] <=z<= z2[t] && y1[t, z] <=y<= y2[t, z] && x1[t, z, y] <=x<= x2[t, z, y]},
   {0, True}
share|improve this answer

Maybe you can try

   somefunc[t] = NIntegrate[ 
   Boole[z1[t] <= z <= z2[t]
   && y1[t, z] <= y <= y2[t, z] 
   && x1[t, z, y] <= x <= x2[t, z, y]]
   otherfunc[x, y, z, t ],
   {z, z0, z1},{y, y0, y1}, {x, x0, x1}];

where the integration limits are appropriate constants.

share|improve this answer

The integral is well defined whether y1 > y2, y1 < y2, or one or both of y1 and y2 is complex (so that < and > have no meaning). That's why NIntegrate proceeds with integration in any of these cases.

Therefore if you want these cases to return a special value, such as 0, you should do it manually with something like this:

somefunc[t] := If[z1[t] < z2[t] && ..., NIntegrate[...], 0];

(Definitely use := instead of =.)

share|improve this answer

Can't you do something like

somefunc[t] = NIntegrate[ otherfunc[x, y, z, t ],
  {z, z1[t], Max[z1[t], z2[t]]}, {y, y1[t, z], Max[y1[t, z], y2[t, z]]}, 
  {x, x1[t, z, y], Max[x1[t, z, y], x2[t, z, y]]}];
share|improve this answer

Your Answer


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.