[R] About an integral univariate problem
R. Michael Weylandt
michael.weylandt at gmail.com
Tue Oct 18 17:48:50 CEST 2011
Your original code was not directly vectorizible (and I should have
noted that): this should work:
fnc <- function(x, y) {
z <- outer(y, x, FUN = "^")
exp(-x) * colSums(z)
}
y <- c(2,3,5)
integrate(fnc, 0, 3, y)
This should work and sorry for the initial confusion.
Michael
On Tue, Oct 18, 2011 at 11:05 AM, Freddy Hernandez Barajas
<fhernanb at gmail.com> wrote:
> Thanks for the response.
>
> But there is a problem, the value of the integral is approximately 14.02 and
> the proposed procedure gives 346.85
>
> Freddy Hernández Barajas
>
>
>
> 2011/10/18 R. Michael Weylandt <michael.weylandt at gmail.com>
>>
>> How about adding an additional argument to fun?
>>
>> R> fun <- function(x, y) exp(-x)*sum(y^x)
>> R> y <- c(2,3,5)
>> R> integrate(fun, 0, 3, y)
>> [1] 346.853 with absolute error < 3.9e-12
>> Michael
>>
>> On Tue, Oct 18, 2011 at 10:01 AM, Freddy Hernandez Barajas
>> <fhernanb at gmail.com> wrote:
>> > Hello all R users
>> >
>> > I want to calculate this univariate integral:
>> > exp(-x)*sum(y^x) respect to x from 0 to 3 where y is a vector
>> > y=(2,3,5).
>> > In fact, the original y vector has a large number of elements but I
>> > propose
>> > with 3 elements.
>> >
>> > I know that I can resolve this problem doing
>> >
>> > fun <- function(x) exp(-x)*(2^x+3^x+5^x)
>> > integrate(fun,0,3)
>> >
>> > When the y vector has a large number of elements the last solution is
>> > not
>> > possible,
>> > if I use this another way I can not obtain the true answer
>> >
>> > y <- c(2,3,5)
>> > fun1 <- function(x) exp(-x)*sum(y^x)
>> > integrate(fun1,0,3)
>> >
>> > Someone could give me a help with the problem?
>> > Thanks.
>> >
>> >
>> > Freddy Hernández Barajas
>> >
>> > [[alternative HTML version deleted]]
>> >
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > 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.
>> >
>> >
>
>
More information about the R-help
mailing list