[R] integrate with vector arguments

marKo mtoncic at ffri.hr
Fri Feb 27 14:49:07 CET 2015


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yes. That's it.
Thanks, a lot, really.

Marko



On 02/27/2015 02:46 PM, David Winsemius wrote:
> 
>> On Feb 27, 2015, at 4:49 AM, marKo <mtoncic at ffri.hr> wrote:
>> 
> 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.
> 
>> If I had understood that you wanted only the values with v1 and
>> v2 paired, then I would have suggested using apply:
> 
> mapply(fV, v1, v2)
>> [1] 0.8333333 1.6666667 2.5000000 3.3333333 4.1666667
> 
>> — David.
> 
> 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
>>>> 
> 
> 
> David Winsemius, MD Alameda, CA, USA
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBAgAGBQJU8HXQAAoJEJcj4KySkkQsDMUP/i2vCLuf21yrMIFotBehOcF8
XpN3gRWAvRAe9Thd4mpIzSj+5woQLJH/Rp8bF2ClXh0HaO3j62fn+RGJeKBEDS0g
gxqkPFapm0ug6qEIYHvguHVLTMuAEWgzU+fpTY955jAwOYvrkHfWJ9XGqfik7R1o
jBfbquq/4kyUqur1iBsHp4RZXkXEUfuk5lLPv03Tl7B6TOift0M/yJf5PAzi0VvY
2jhDpAvSJ6JSjwx+R+xnDkXhkSNMQAoiZuabuwVbD9dqA7T5dxreieAbu+YcUo4E
FF96A6G7gfIgneCdX3i/2i632MjTfDTe1eDB5YYATf5GnmzYB1K0ZH6Egr6sB4/7
/0wvObJHWJfc5qZQpqJyk8mEy54Igm/jko75yaQ7EIHWbDEcm30AyC0b0/7qTels
w2Tie3T4KvnjfSaPSz65AW7ip8bWYy3QpgNTzpQEn1zNjWSJ3lQYOOvWwFhr67YN
axft3nHRpthOUWk5lZgLJ5Nmx0ILB/NxYIh/nj2uUypsYYvvMNqKTg+QRNEICm51
bace0rfvPTl4UaCCrtFtydN7awv8/weRCMqUtSZccxZ9Hvkli6inIWH0WDK9/xg3
HxPgv8BEF9wJtfHa8HWtDEG7pqUT5byhd3UVtxSkH+RYZl9iGu/Vat/a3Ikj+6Lb
i9lIjaTylqlAy//m4S/Q
=EH2y
-----END PGP SIGNATURE-----



More information about the R-help mailing list