# Integral for piecewise function inconsistent with the usual way

I have the following piecewise function:

f0[y_] :=(1/(E^((-2 + y)^2/2)*Sqrt[2*Pi]) + 1/(E^((2 + y)^2/2)*Sqrt[2*Pi]))/2

f1[y_] :=(1/(E^((-3 + y)^2/2)*Sqrt[2*Pi]) + 1/(E^((1 + y)^2/2)*Sqrt[2*Pi]))/2

l[y_] :=f1[y]/f0[y]

llua = 1.379804276200094;

zn =0.817051;

g00nx[y_] :=Piecewise[{{f0[y]/(llua*zn), l[y] < llua^(-1)}, {f1[y]/(zn*Sqrt[llua*l[y]]), llua^(-1) <= l[y] <= llua}, {f0[y]/zn, l[y] > llua}}, 0]

NIntegrate[g00nx[y]*Log[g00nx[y]/f0[y]], {y, -Infinity, Infinity}]

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in y near {y} = {-1.81331}. NIntegrate obtained 0.010000075084654395 and 1.3153774269495352*^-7 for the integral and error estimates. >>

0.0100001


Until here everything is correct. What I want to do is to write NIntegrate[g00nx[y]*Log[g00nx[y]/f0[y]], {y, -Infinity, Infinity}] in 3 different regions using Boole[.] function. When I do so, I can write the integral NIntegrate[g00nx[y]*Log[g00nx[y]/f0[y]], {y, -Infinity, Infinity}] as

(1/(zn*llua))*Log[(1/(zn*llua))]*NIntegrate[f0[y] Boole[l[y] < 1/llua], {y, -Infinity, Infinity}] + NIntegrate[1/zn*(l[y]*llua)^(-1/2)*f1[y]*Log[1/zn*(l[y]*llua)^(-1/2)*l[y]]*Boole[l[y] <= llua && l[y] >= 1/llua], {y, -Infinity, Infinity}] + (1/zn)*Log[(1/zn)]*NIntegrate[f0[y] Boole[l[y] > llua], {y, -Infinity, Infinity}]

0.00524078


Both results must be the same because I am doing the same thing. I checked if I did mistake several times but there is no mistake. I wonder what is really happening here and whether it is possible to write the piecewise functions in 3 regions, take the 3 integral and obtain the same result. Boole[.] is also not a must.

• "NIntegrate failed to converge" === "everything is correct"? In any case, I don't get the warning and both calculations return 0.00999966. V10.0.1, Mac OSX 10.9.5. – Michael E2 Dec 26 '14 at 14:43
• @MichaelE2 I have the version 9. the first returned 0.00999967 when I just copied and pasted. It is correct. The second returned 0.00524078. I have another version it is version 7 student edition and it also returned 0.00524078. Is it due to versions? if yes It is really surprising! – Seyhmus Güngören Dec 26 '14 at 14:48
• Things do change from version to version. In integration, which is extremely complicated, it seems algorithms are developed that are better in one area, worse in another, sometimes unintentionally. Generally they pick changes that give an overall improvement, but in some specials, it's not an improvement. – Michael E2 Dec 26 '14 at 14:53
• @MichaelE2 very interesting. I remember once I had consistent results about another problem in student version 7 but not in version 9. Then, I realized that in version 9 I should have said that the variable was a real one, not complex, extra definitions would solve the problem. However, in this case, I guess there is a serious problem. Because the calculations are absolutely wrong and the functions inside are in my opinion not complicated ones!. I must then obtain the version 10 and have a look at there. Thank you! – Seyhmus Güngören Dec 26 '14 at 14:58

On my Mac, versions 9 (9.0.1) and 10 (10.0.1) provide the same result as obtained by Michael E2. Try simplifying the intermediate results.

\$Version


"9.0 for Mac OS X x86 (64-bit) (January 24, 2013)"

f0[y_] = (1/(E^((-2 + y)^2/2)*Sqrt[2*Pi]) +
1/(E^((2 + y)^2/2)*Sqrt[2*Pi]))/2 // Simplify;

f1[y_] = (1/(E^((-3 + y)^2/2)*Sqrt[2*Pi]) +
1/(E^((1 + y)^2/2)*Sqrt[2*Pi]))/2 // Simplify;

l[y_] = f1[y]/f0[y] // Simplify;

llua = 1.379804276200094;

zn = 0.817051;

g00nx[y_] =
Piecewise[{{f0[y]/(llua*zn),
l[y] < llua^(-1)}, {f1[y]/(zn*Sqrt[llua*l[y]]),
llua^(-1) <= l[y] <= llua}, {f0[y]/zn, l[y] > llua}}, 0];

NIntegrate[g00nx[y]*Log[g00nx[y]/f0[y]], {y, -Infinity, Infinity}]


0.00999966

(1/(zn*llua))*Log[(1/(zn*llua))]*
NIntegrate[f0[y] Boole[l[y] < 1/llua], {y, -Infinity, Infinity}] +
NIntegrate[
1/zn*(l[y]*llua)^(-1/2)*f1[y]*Log[1/zn*(l[y]*llua)^(-1/2)*l[y]]*
Boole[l[y] <= llua && l[y] >= 1/llua], {y, -Infinity,
Infinity}] + (1/zn)*Log[(1/zn)]*
NIntegrate[f0[y] Boole[l[y] > llua], {y, -Infinity, Infinity}]


0.00999966

• this one is okay. did you get the same result for the second integral? – Seyhmus Güngören Dec 26 '14 at 15:39
• Yes, see edit above. – Bob Hanlon Dec 26 '14 at 15:46
• "9.0 for Microsoft Windows (32-bit) (January 24, 2013)" and i checked the details and it is 9.0.1.0. – Seyhmus Güngören Dec 26 '14 at 16:13
• It is now even more interesting. I have a laptop and when I use it at enerysave mode, I got again the wrong answer and when I turned it back to the highperformance mode I got the correct answer. – Seyhmus Güngören Jan 10 '15 at 1:34
• On my MacBook Pro running OS X 10.10.1 I get the same results as reported above for both Mma v9.0.1 (64-bit) and 10.0.2 (64-bit) both with and without the charger connected. – Bob Hanlon Jan 10 '15 at 1:49