Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Piecewise integration of f[x,y]

  • To: mathgroup at
  • Subject: [mg15817] Re: [mg15773] Piecewise integration of f[x,y]
  • From: Andrzej Kozlowski <andrzej at>
  • Date: Mon, 8 Feb 1999 03:25:43 -0500 (EST)
  • Sender: owner-wri-mathgroup at

First I must make a disclaimer. I am an algebraic topologist and have
never had any serious dealings with numerical computations. The only
time I have to deal with integrals is when I teach calculus courses to
undergraduates. So as a rule I avoid answering questions such as this
one, but this time I shall risk it. However, you have been warned to
apply a dose of scepticism to what I say.

This is the second question concerning integrals of piecewise functions
that has come up recently, but this time it is a double integral, which
makes it more complicated. In general it seems to me that NIntegrate
works in such cases, but sometimes has problems with knowing if its
answer is accurate enough. This seems to be such kind of case:


f[x_, y_] := Which[x >= 0. && x <= 2. && y >= 0. && y <= 1.,
   -0.5*(-2. + x)*(0. + y), True, 0];

NIntegrate[f[x,y],{x,-3,3},{y,0.2,4}] NIntegrate::slwcon: 
   Numerical integration converging too slowly; suspect
    singularity, value of the integration is 0, oscillatory
    integrand, or insufficient WorkingPrecision. If your
    integrand is oscillatory try using the option
    Method->Oscillatory in NIntegrate. .....
   Further output of NIntegrate::slwcon
     will be suppressed during this calculation. NIntegrate::ncvb: 
   NIntegrate failed to converge to prescribed accuracy after
     13 recursive bisections in y near {x, y} = 
    {0.75, 0.999939}.

I do not really feel competent to try to explain this (see the
disclaimer above) and I hope someone else will do so. But I would like
to make an observation about another possible approach, by using

Integrate in general does not like functions defined by means of
conditionals (and will thus fail if used in the above example in place
of NIntegrate) with one somewhat remarkable exception. It works with
functions defined using a single If. Thus, if we define functions 

p[x_]:=If[0<x<2 ,1, 0];

we can integrate them without problems: In[65]:=

Not only that, but we can also do things like this

h[x_]:=x^3;Integrate[p[x]*h[x],{x,-Infinity,Infinity}] Out[66]=

This suggest that we should be able to evaluate the above double
integral by using  p and q above and Fubini's theorem (which says that
you can first integrate with respect to one variable and then the
result with respect to the other). In other words we define 


h[x_,y_]:=  -0.5*(-2. + x)*(0. + y)

and then compute 

Integrate[Integrate[p[x]*q[y]*h[x,y],{x,-3,3}],{y,0.2,4}] Out[69]=

This seems to be the right answer.

On Sun, Feb 7, 1999,  <sergio at> wrote:

>   Hello everyone:
>   Could somebody point out how to integrate a piecewise
>   function like:
>f[x_, y_] := Which[x >= 0. && x <= 2. && y >= 0. && y <= 1.,
>   -0.5*(-2. + x)*(0. + y), True, 0];
>                How to do:
>        x1=-3; x2=3; y1=0.2; y2=4;
>    Integrate[f[x,y],{x,x1,x2},{y,y1,y2}];
>   Sergio
>   E-mail: sergio at *)
>-----------== Posted via Deja News, The Discussion Network ==----------
>       Search, Read, Discuss, or Start Your Own

Andrzej Kozlowski
Toyama International University

  • Prev by Date: Re: FindRoot behaving funny?
  • Next by Date: Re: a tricky limit
  • Previous by thread: Piecewise integration of f[x,y]
  • Next by thread: Re: Piecewise integration of f[x,y]