SystemException[“MemoryAllocationFailure”] in Integrate

Integrating products and ratios of piecewise linear functions throws the error Uncaught SystemException returned to top level. Can be caught with \ Catch[\[Ellipsis], _SystemException] SystemException["MemoryAllocationFailure"] after about 5 min and does not compute the integral. How to find the integral in closed form? It should almost be doable by hand because of the piecewise linear functions. NIntegrate works, although slowly.

Clear[v0, fv, fe0, fe, dfe, ne, w1, w2, h1, h2, qs, \
v0, qmax, vfoc0]
$Assumptions = h2 > h1 > 0 && w2 > w1 > 0 && v0 > 0 && qmax > 0; qs[v_, v0_] := Piecewise[{{-qmax, v < -v0}, {qmax, v > v0}, {v*qmax/v0, -v0 <= v <= v0}, {0, True}}]; fv[v_] := Piecewise[{{1/2, -1 <= v <= 1}, {0, True}}] v0 = 0.8; qmax = 2.30; h1 = 0.01; h2 = 1.; w1 = 2.0; w2 = 4.0; fe0[e_] := Piecewise[{{h1, -w2 <= e < -w1 || w1 < e <= w2}, {h2*(w1 - Abs[e])/w1 + h1*(1 - (w1 - Abs[e])/w1), -w1 <= e <= w1}, {0, True}}] ne = 2.06; fe[e_] := fe0[e]/ne; dfe[e_] := Piecewise[{{0, -w2 <= e < -w1 || w1 < e <= w2}, {(h2 - h1)/w1, -w1 <= e <= 0}, {-(h2 - h1)/w1, 0 <= e <= w1}, {0, True}}] vfoc0 = (-q* Integrate[v*fe[q + \[Epsilon] - qs[v, v0]]*fv[v], {v, -1, 1}]* Integrate[fv[v]*dfe[q + \[Epsilon] - qs[v, v0]], {v, -1, 1}] + Integrate[ fe[q + \[Epsilon] - qs[v, v0]]*fv[v], {v, -1, 1}]*(Integrate[ v*fe[q + \[Epsilon] - qs[v, v0]]*fv[v], {v, -1, 1}] + q*Integrate[ v*fv[v]*dfe[q + \[Epsilon] - qs[v, v0]], {v, -1, 1}]))/ Integrate[fe[q + \[Epsilon] - qs[v, v0]]*fv[v], {v, -1, 1}]^2; Integrate[ fe[\[Epsilon]]*vfoc0, {\[Epsilon], -w2, w2}](*Takes >5 min. Does not integrate with breakpoints -w1,0,w1. \ Does not integrate with vfoc0/.{z->q+\[Epsilon]}, \ SystemException["MemoryAllocationFailure"].*)  Same error using rationals for the parameters instead of decimals. Same with Set instead of SetDelayed. Mathematica 11.3 on Ubuntu 18.04, Dell Latitude E7470. Sometimes also Mathematica freezes with the system message "Mathematica has stopped responding. Force quit. Wait." Using PiecewiseExpand, same error. The modified code using rationals, Set and PiecewiseExpand is Clear[v0, fv, fe0, fe, dfe, dfe0, ne, w1, w2, h1, h2, qs, v0, qmax, \ vfoc0]$Assumptions = h2 > h1 > 0 && w2 > w1 > 0 && v0 > 0 && qmax > 0;
qs = Piecewise[{{-qmax, v < -v0}, {qmax,
v > v0}, {v*qmax/v0, -v0 <= v <= v0}, {0, True}}];
fv = Piecewise[{{1/2, -1 <= v <= 1}, {0, True}}];
v0 = 8/10; qmax = 23/10; h1 = 1/100; h2 = 1;
w1 = 2; w2 = 4; h1v = 1; h2v = 2;
fe0 = PiecewiseExpand[
Piecewise[{{h1, -w2 <= q + \[Epsilon] - qs < -w1 ||
w1 < q + \[Epsilon] - qs <=
w2}, {h2*(w1 - Abs[q + \[Epsilon] - qs])/w1 +
h1*(1 - (w1 - Abs[q + \[Epsilon] - qs])/w1), -w1 <=
q + \[Epsilon] - qs <= w1}, {0, True}}],
h2 > h1 > 0 && w2 > w1 > 0 && v0 > 0 && qmax > 0, Reals];
ne = 206/100;
fe = fe0/ne;
dfe0 = PiecewiseExpand[
Piecewise[{{0, -w2 <= q + \[Epsilon] - qs < -w1 ||
w1 < q + \[Epsilon] - qs <= w2}, {(h2 - h1)/w1, -w1 <=
q + \[Epsilon] - qs <= 0}, {-(h2 - h1)/w1,
0 < q + \[Epsilon] - qs <= w1}, {0, True}}],
h2 > h1 > 0 && w2 > w1 > 0 && v0 > 0 && qmax > 0, Reals];
dfe = dfe0/ne;
vfoc0 = (-q*Integrate[v*fe*fv, {v, -1, 1}]*
Integrate[fv*dfe, {v, -1, 1}] +
Integrate[
fe*fv, {v, -1, 1}]*(Integrate[v*fe*fv, {v, -1, 1}] +
q*Integrate[v*fv*dfe, {v, -1, 1}]))/
Integrate[fe*fv, {v, -1, 1}]^2;
Integrate[
PiecewiseExpand[fe*vfoc0,
h2 > h1 > 0 && w2 > w1 > 0 && v0 > 0 && qmax > 0,
Reals], {\[Epsilon], -w2, w2}]


Adding Simplify@ in front of PiecewiseExpand everywhere creates the warnings Power: Infinite expression 1/0^2 encountered and Infinity: Indeterminate expression 0ComplexInfinity encountered, same SystemException at the end.

• Does it behave differently if you use rationals instead of decimal values? – Daniel Lichtblau Nov 19 '20 at 16:35
• On Mathematica 12.1 (Linux), the code block in the question crashed the kernel after about 15 minutes with an apparent peak memory usage of around 10GB. – eyorble Nov 19 '20 at 16:47
• Have you tried perhaps heavy use of Simplify and PiecewiseExpand on your expressions? vfoc0 is a pretty heavily nested combination of Piecewise function. That may be why the system chokes following the maze of branches. You may also consider using rationals for symbolic integration, and Set (=) instead of SetDelayed to avoid unnecessary recomputations. – MarcoB Nov 19 '20 at 16:58
• @DanielLichtblau Same behaviour with rationals. Same with Set instead of SetDelayed. Will investigate Simplify and PiecewiseExpand` - the latter I have not used before. – Sander Heinsalu Nov 20 '20 at 7:49
• @MarcoB I tried adding Simplify and PiecewiseExpand (new code copied to the edited question). Same error. – Sander Heinsalu Nov 20 '20 at 10:01