Re: Piecewise integration of f[x,y]

*To*: mathgroup at smc.vnet.net*Subject*: [mg15817] Re: [mg15773] Piecewise integration of f[x,y]*From*: Andrzej Kozlowski <andrzej at tuins.ac.jp>*Date*: Mon, 8 Feb 1999 03:25:43 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

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: In[62]:= f[x_, y_] := Which[x >= 0. && x <= 2. && y >= 0. && y <= 1., -0.5*(-2. + x)*(0. + y), True, 0]; In[63]:= 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. ..... General::stop: 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}. Out[63]= 0.479956 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. 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 In[64]:= p[x_]:=If[0<x<2 ,1, 0]; q[y_]:=If[0<y<1,1,0]; we can integrate them without problems: In[65]:= Integrate[p[x],{x,-3,3}] Out[65]= 2 Not only that, but we can also do things like this In[66]:= h[x_]:=x^3;Integrate[p[x]*h[x],{x,-Infinity,Infinity}] Out[66]= 4 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 In[67]:= h[x_,y_]:= -0.5*(-2. + x)*(0. + y) and then compute In[69]:= Integrate[Integrate[p[x]*q[y]*h[x,y],{x,-3,3}],{y,0.2,4}] Out[69]= 0.48 This seems to be the right answer. On Sun, Feb 7, 1999, <sergio at scisun.sci.ccny.cuny.edu> 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 scisun.sci.ccny.cuny.edu *) > >-----------== Posted via Deja News, The Discussion Network ==---------- >http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own > Andrzej Kozlowski Toyama International University JAPAN http://sigma.tuins.ac.jp/ http://eri2.tuins.ac.jp/