Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

integration - integrate with a vecor or list as upper limit R

I want to use integrate and calculate for different upper limits. I tried to put a vector and a list into the upper limit parameter:

ul <- as.list(seq(0.001,1,0.001))
integrand <- function(x) {1/((x+1)*sqrt(x))}
test <- integrate(integrand, lower = 0, upper = ul)$val

Error:

Error in is.finite(upper) : default method not implemented for type 'list'

I tried it with a for loop:

ul <- seq(0.001,1,0.001)
for (i in ul){
   test = NULL
   test <- rbind(test, integrate(integrand, lower = 0, upper = S[i])$val[i])
}

error: Error in if (is.finite(lower) && is.finite(upper)) { : missing value where TRUE/FALSE needed

I could change the upper limit manually and then save the result with rbind in a data frame, but this would need to much time:

test <-data.frame(test = integrate(integrand, lower = 0, upper = 0.001)$val)
test <- data.frame(rbind(test, integrate(integrand, lower = 0, upper = 0.002)$val)) 

and so on.

I guess I should use lappy to solve my problem, but I'm not familiar to using it. How could I solve my problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  sapply(ul, function(x) integrate(integrand, lower = 0 , upper = x)$value )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...