I am doing numerical integration in R with the function integrate
. I am integrating a function fun1
and have discovered some nonsmooth behavior. Changing the integral's lower bound slightly produces a big jump in the calculated value of the integral; compare aaa
and ccc
below. aaa
is approximately correct, as can be verified using online numerical integral calculators. However, ccc
is far from the truth.
For illustrative purposes, I split the integration interval into two and sum the two parts. The result is very different from integrating over a single interval. This should not be the case, as algebraically the two are equal.
fun1 <- function(x, d){
min( 0, -(exp(x)-0.1*abs(exp(x)-d)-0.05*(exp(x)-d))^(-1) ) * dnorm(x=x,mean=0,sd=0.05)
}
aaa <- integrate(f=fun1, d=0.8932789, lower=-3.159041, upper=40 )$value; aaa
bbb <- integrate(f=fun1, d=0.8932789, lower=-3.159041, upper=-3.157379)$value; bbb
ccc <- integrate(f=fun1, d=0.8932789, lower=-3.157379, upper=40 )$value; ccc
aaa-(bbb+ccc) # This should be (but is not) close to zero because algebraically, aaa-(bbb+ccc)=0
Questions: Why is this happening? How do I fix this? I.e. how do I get a correct value for ccc
?