I have mathematica 14.3.
I have encountered a weird issue when numerically integrating this complicated function
f[x_] :=
UnitStep[
x - .3]*(1/(-0.3` + x)^2 (-1.1434102576408818` -
73.9413929141465` (-0.3` + x)^2.915` -
115.82390213796239` (-0.3` + x)^3.415` +
536.3471523421645` (-0.3` + x)^3.915` -
513.5287667970828` (-0.3` + x)^4.415` +
24.579421179117183` (0.3` + x)^2.915` +
24.925561694460875` (0.3` + x)^3.415` -
77.76754995587882` (0.3` + x)^3.915` +
51.33230871908811` (0.3` + x)^4.415` +
x (2.65606615372176` -
154.94956890020973` (-0.3` + x)^2.915` -
145.89473463682685` (-0.3` + x)^3.415` +
388.39637357704294` (-0.3` + x)^3.915` -
178.45013745326605` (-0.3` + x)^4.415` -
124.82340431573319` (0.3` + x)^2.915` -
114.87401830151424` (0.3` + x)^3.415` +
331.86338927447434` (0.3` + x)^3.915` -
205.70188178134828` (0.3` + x)^4.415`) +
x^2 (-167.90881183998138` (-0.3` + x)^2.915` -
145.10636075108883` (-0.3` + x)^3.415` +
412.2021927513278` (-0.3` + x)^3.915` -
266.5287241054068` (-0.3` + x)^4.415` +
165.54009634792362` (0.3` + x)^2.915` +
139.07326720934407` (0.3` + x)^3.415` -
372.5592895364412` (0.3` + x)^3.915` +
216.691879843441` (0.3` + x)^4.415`))) +
UnitStep[.3 -
x]*(1/(-0.3` + x)^2 (-1.1434102576408818` +
24.579421179117187` (0.3` - x)^2.915` +
24.925561694460878` (0.3` - x)^3.415` -
77.76754995587882` (0.3` - x)^3.915` +
51.33230871908812` (0.3` - x)^4.415` +
24.579421179117187` (0.3` + x)^2.915` +
24.925561694460878` (0.3` + x)^3.415` -
77.76754995587882` (0.3` + x)^3.915` +
51.33230871908812` (0.3` + x)^4.415` +
x (2.65606615372176` +
124.82340431573319` (0.3` - x)^2.915` +
114.87401830151425` (0.3` - x)^3.415` -
331.86338927447434` (0.3` - x)^3.915` +
205.70188178134828` (0.3` - x)^4.415` -
124.82340431573319` (0.3` + x)^2.915` -
114.87401830151425` (0.3` + x)^3.415` +
331.86338927447434` (0.3` + x)^3.915` -
205.70188178134828` (0.3` + x)^4.415`) +
x^2 (165.54009634792365` (0.3` - x)^2.915` +
139.07326720934404` (0.3` - x)^3.415` -
372.5592895364412` (0.3` - x)^3.915` +
216.691879843441` (0.3` - x)^4.415` +
165.54009634792365` (0.3` + x)^2.915` +
139.07326720934404` (0.3` + x)^3.415` -
372.5592895364412` (0.3` + x)^3.915` +
216.691879843441` (0.3` + x)^4.415`)));
The weird thing is that
NIntegrate[f[x], {x, 0, 1}]
gives
-1162.46 - 5.61143*10^-32 I
while
NIntegrate[f[x], {x, 0, .4}] + NIntegrate[f[x], {x, .4, 1}]
gives the correct answer
7.5341
Why does this happen? Is there an easy fix without splitting up the integral at some random point by trial and error?



NIntegrate[f[x], {x, 0, 1}, MinRecursion -> 1]seems to produce an accurate result $\endgroup$NIntegrate[f[x],{x,0,1},AccuracyGoal->4]gives7.5341. Btw, the point .3 is problem in your input.Limit[Rationalize[f[x]],x->3/10]givesIndeterminatesame as forf[.3]. This the point where the unit steps join. ButExclusions -> {.3}did not help. Do not know if this is why you get the original error. i.sstatic.net/Xx2PQMcg.png $\endgroup$fe = FunctionInterpolation[f[x], {x, 0, 1}]; NIntegrate[fe[x], {x, 0, 1}]gives7.53412$\endgroup$NIntegrate[](as happens in the integral over{x, 0, 0.4}), the error introduced by the "mistake" is negligible, because the "exact" integrand is actually well-behaved at the singularity. But if the badness is caught, thenNIntegate[]crumbles under the numerical instability. The magnification of round-off error by the factors1/(-0.3 + x)^2forxnear0.3ought to be considered in attempting to integratef[x]. $\endgroup$