[R] integrate with vector arguments

marKo
Fri, 27 Feb 2015 09:49:26 -0000

-----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-----