Integrating "If"
- To: mathgroup at smc.vnet.net
- Subject: [mg84512] Integrating "If"
- From: Scott Hemphill <hemphill at hemphills.net>
- Date: Mon, 31 Dec 2007 21:16:58 -0500 (EST)
- Reply-to: hemphill at alumni.caltech.edu
I've come across what appears to be a bug in my (rather old) version of Mathematica, so if it's fixed in a newer version, please let me know. In[1]:= $Version Out[1]= 5.1 for Linux (October 25, 2004) This is a simplified version of the problem I am actually considering: Two points are chosen randomly from U(0,1). What is the distribution of d, the distance between them? If I formulate the problem this way, I get a correct answer: In[2]:= Integrate[If[Abs[x0 - x1] < d, 1, 0], {x0, 0, 1}, {x1, 0, 1}, Assumptions -> {d > 0}] Out[2]= Piecewise[{{3/4, d == 1/2}, {1, d >= 1}}, 2*d - d^2] I am delighted that Mathematica can integrate the "If" function, but if I use this equivalent (I think!) formulation, I get an incorrect answer: In[3]:= Integrate[If[(x0 - x1)^2 < d^2, 1, 0], {x0, 0, 1}, {x1, 0, 1}, Assumptions -> {d > 0}] Out[3]= Piecewise[{{5/8, 2*d == 1}, {1, d >= 1}, {((4 - 3*d)*d)/2, 1/2 < d < 1}}, (-1 + 6*d - 3*d^2)/2] In[4]:= Plot[%, {d, 0, 2}, PlotRange -> All] This function of d should be positive, monotonic and continuous, but this plot exhibits none of those traits. I would prefer to use the second formulation, because it is easy to generalize for sampling from the unit square, unit cube, etc. By the way, I included the "Assumptions -> {d > 0}" in my formulations so that Mathematica would be aware that d was a real variable, and reduce the number of cases it had to consider. If I remove it from In[3], then I get correct results for positive d. However, the results for negative d, which should be a mirror image of those for positive d, are screwy. Is there some way I can reformulate the integral, playing to Mathematica's strengths, so that I can get an answer I can trust? Keep in mind that I am also interested in evaluating integrals like: Integrate[If[(x0-x1)^2+(y0-y1)^2<d^2,1,0],{x0,0,1},{x1,0,1},{y0,0,1},{y1,0,1}] (This integral takes a while to evalulate, and the result isn't continuous or monotonic. I'm about half way done with evaluating it by hand--it's an interesting problem.) Thanks! Scott -- Scott Hemphill hemphill at alumni.caltech.edu "This isn't flying. This is falling, with style." -- Buzz Lightyear