-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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. 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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJU72qnAAoJEJcj4KySkkQsLAkP/R7DvO0GiZDRrtHgDna/2xj+ XJd8G/gGfe029lVjg+3i6wfKfZ9CoRH+kHEVnT0/SRYcSAeRu3/fys11sjEgVGnl a/Go167YRYfDkP/OrY4jKtlULySeiGBxNJwKmk1oCidoodk2mejWdPQ61tBj6ozF sA+Bzoi7Exh2pp88Eks4+Ynz+Toi8Ck1hItV60kP9yOMSBsIPVLw53lGXDfOshzM zLcFbHM5hyjmt/BQvyaBm3E822YEJgcDQN3nedjQgwThJuEyig2TXHAvyEZcdBWD H8Py0b5/TBdmxqJQ3EqYyBFmPxeFuhO4ZS22IhP+rqPJ51EZnfqG6DRBHHLqQ9rX ZnYJN8ryqDVMOrYHn6j3dNd/m7C/YWmrY8gjArv8WxRsX+kX+DAgbRmiw/43BXNG Y2Jco5dChWBrXQDR3FMoJWBTWjvwgPfP06hnwjrJT1uJZQLPUzhdrIxyHxbhsW0A UeiRqNiPjE9YpKrFGn9Itg1tXk35yrPrNmmj1nzIzaHejMzT8zf0X2pJAygAYyk3 +mrEgwkB31GOt2mUqqFzDxgDHASaSTPlskviIVJ9klcs7ViWYSy5ARiF4/ptbluE CTny7dVj/AoXq8dC8TxghOT1QSnPVy7ceb6fCep7LxJDWlFqTEM0LCbL7Ql78yzP +Em5gaikzPGbJ7uvVKIG =7J6P -----END PGP SIGNATURE-----
Hi, The following works.> f2function(z) { f1 <- function(t) { z*t + z*t^2 } return(f1) }> sapply(1:5,function(x)integrate(f2(x),0,1)$value)[1] 0.8333333333 1.6666666667 2.5000000000 3.3333333333 4.1666666667 -- View this message in context: http://r.789695.n4.nabble.com/integrate-with-vector-arguments-tp4703906p4703925.html Sent from the R help mailing list archive at Nabble.com.
> 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
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Gee. That simple. I knew that! Thanks a lot. Essentially, I needed only the diagonal elements. Easily solved by: diag(outer( X=v1,Y=v2, FUN= fV) I am sure that there are simpler options, but that works like a charm. Thanks a lot. Cheers, Marko On 02/27/2015 06:49 AM, David Winsemius wrote:> >> 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 >-----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJU8D2hAAoJEJcj4KySkkQsblsQAK2cpOG/qsrXP6vK9Zq3UMkc 6jNQ9PQqvT1H8WyR8rNTY1Bis7npxgNh0avRYOrsRf1D3u9frNjBAhT2v0mMW+Uk LRvHpER1EnIZhdFcmE7uOUNsaU/IquicVVWAXgVVu2/yUW8nJZ4FQ2wkKDLlTY97 dyNMj5i1r5DWBtfUbCtTiSJz1G16DXiJBsk0Zi1Z9zT71evOh2tIfztv1qSSTOLJ gjWbpefYThypPq/170+Eqv3BOigjQd1ljeyXVgGrs/vMTX6a9ZL5KDUYsR7QnhmH ZiHNUTcUpAFb3QGLNVM6ULd7GfzDKgG7rH8JwokYzi/IIsm5aTNfY7dUCGySx+dL 4tOQKe6LjFGIfqPHeZXXs1ErYrIs7L//z+n9ZfHKT0RH7zAoakrzkvFxsBx2LU7z Wziv3/eMN65WOPFpH2puaUQrandbmpDujuZS0PfGBc5/hrIpeCejm7LfHocck6MT v+J4o05+a3jMSdZdtbNXv6q5z+tXaEdThKz77VmFwMQWzFto8QFbe5dOKP9EO6Vn 9+u0gSswtE43PXkn3Egj6HmhrTkREc3lnHsh/0E32+g4UsWxVlyLvfCrm9s4zKOZ fGjIpaUPUp2AkNGAa8BhLqO6LJVaU2/wCvcYIRAbv1Uck2XK9lppKsvFEUuRz9Uk kPaGAS9OJgsUS8kMVpMf =v7j4 -----END PGP SIGNATURE-----