[R] Integrate with vectors and varying upper limit

Pete Brecknock Peter.Brecknock at bp.com
Mon Mar 25 01:33:52 CET 2013


sunny0 wrote
> I'd like to integrate vectors 't' and 'w' for log(w)/(1-t)^2 where i can
> vary the upper limit of the integral to change with each value of 't' and
> 'w', and then put the output into another vector. 
> 
> So, something like this...
> 
> w=c(.33,.34,.56)
> t=c(.2,.5,.1)
> k-c(.3,.4,.5)
> 
> integrand <- function(t) {log(w)/(1-t)^2}
> integrate(integrand, lower = 0, upper = k)
> 
> or maybe...
> 
> integrand <- function(tt) {tt}
> integrate(integrand, lower = 0, upper = k)
> 
> ... with sapply or something similar to create the output vector. How can
> this be done?

Something like this?

w=c(.33,.34,.56) 
t=c(.2,.5,.1) 
k=c(.3,.4,.5) 

integrand <- function(t) {log(w)/(1-t)^2} 

out <- sapply(k,function(x) integrate(integrand, lower = 0, upper = x ,
subdivisions=1000))

# Output
             [,1]         [,2]        [,3]        
value        -0.4017303   -0.6249136  -0.9373696  
abs.error    2.798235e-05 9.17413e-05 9.209191e-05
subdivisions 32           208         91          
message      "OK"         "OK"        "OK"        
call         Expression   Expression  Expression  

HTH

Pete



--
View this message in context: http://r.789695.n4.nabble.com/Integrate-with-vectors-and-varying-upper-limit-tp4662338p4662341.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list