[R] integrate with vector arguments

David Winsemius
Fri, 27 Feb 2015 05:49:45 -0000

> On Feb 26, 2015, at 1:49 PM, marKo <mtoncic at ffri.hr> wrote:
>
> v1<-c(1:5)
> v2<-c(1:5)
>
> f1<-function (x) {v1*x+v2*x^2}
>
> The problem is that integrate(f1, 0, 1) does not work.
> I does not, even if a pas the arguments (v1, v2)
>
> f1<-function (x, v1, v2) {v1*x+v2*x^2}
>
> or if i try to vectorize the function
>
> f1<-Vectorize(function(x, v1, v2){v1*x+v2*x^2},
> vectorize.args=c("v1", "v2"))
>
> integrate(f1, 0, 1) gives an error:


> f1<-function (X, Y) integrate( function(x) {X*x+Y*x^2}, 0, 1)$value
> fV<-Vectorize(f1)
> outer( X=v1,Y=v2, FUN= fV)
          [,1]     [,2] [,3]     [,4]     [,5]
[1,] 0.8333333 1.166667  1.5 1.833333 2.166667
[2,] 1.3333333 1.666667  2.0 2.333333 2.666667
[3,] 1.8333333 2.166667  2.5 2.833333 3.166667
[4,] 2.3333333 2.666667  3.0 3.333333 3.666667
[5,] 2.8333333 3.166667  3.5 3.833333 4.166667

?
David Winsemius, MD
Alameda, CA, USA