[R] an integration question

Vito Muggeo vito.muggeo at giustizia.it
Mon Sep 13 11:35:58 CEST 2004


Dear all,
I'm stuck on a problem concerning integration..Results from the analytical
expression and numerical approximation (as returned by integrate()) do not
match.
It probably depends on some error of mine, so apologizes for this off-topic
question.

I'm interested in computing the integral of f where:
f<-function(x){exp(-3-.2*pmin(x-7.5,0))}
x<-seq(0,15,length=50)
plot(x, f(x),type="l")

Using the integrate() function, I get reasonable results
a<-sapply(x, function(xx)integrate(f,0,xx)[[1]])
plot(x, a,type="l")

Using analytical expression, the primitive of f is (or should be..)
F<-function(x){exp(-3-.2*pmin(x-7.5,0))/(-.2*I(x<7.5))}
plot(x,(F(x)-F(0)), type="l")

The problem is that for x>7.5 the denominator (-.2*I(x<7.5)) is zero and
then the primitive function F(.) goes to infinity. On the other hand
integrate() provides finite (as it should be, I believe) output. For x<7.5
everything works.

> F(10)-F(0)
[1] -Inf
..
> integrate(f,0,10)
0.9911831 with absolute error < 1.1e-14
>
> F(5)-F(0)
[1] 0.7052258
..
> integrate(f,0,5)
0.7052258 with absolute error < 7.8e-15

Hence I think there is an error in the expression of H(.), but I can not
figure out where it is..Please can anyone help me? I would like to get an
analytical expression for F.

Many thanks,

vito




More information about the R-help mailing list