marKo <mtoncic <at> ffri.hr> writes: > > I'm a bit stuck. > I have to integrate a series of polynomial functions with vector > arguments. > > 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.
The point is not that there are "vector arguments", but that your function is vector-valued and so the generated error message below rightly says "evaluation of function gave a result of wrong length". You could integrate each dimension separately or, e.g., you use quadv() from package 'pracma' which handles vector-valued functions: > v1 <- v2 <- 1:5 > f1<-function (x) {v1*x+v2*x^2} > library(pracma) > quadv(f1, 0, 1, tol=1e-10) $Q [1] 0.8333333 1.6666667 2.5000000 3.3333333 4.1666667 $fcnt [1] 13 $estim.prec [1] 0.0000000003 quadv() employs an adaptive Simpson quadrature where the recursion is applied to all components at once. > 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: > > Error in integrate(f1, 0, 1) : > evaluation of function gave a result of wrong length > > Any help will be greatly appreciated. > Thanks, > > Marko > ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.