[R] integration function

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Fri Feb 4 13:59:21 CET 2005


The syntax you have you used is not correct. integrate() needs as 
first argument a function! see ?integrate for more info.

a possible solution could be:

x <- seq(-3, 3, 0.1)
y <- exp(x)
######
integral <- function(z, a, b, step.){
    cc <- numeric(n <- (b-a)/step.)
    f <- function(x) exp(x)
    for(i in 1:n) cc[i] <- integrate(f, lower=a+(i-1)*step., 
upper=a+i*step.)$val
    cc
}
integral(y, -3, 3, 0.1)

However, since exp has known integral, you do not need to integrate:

exp(x[-1])-exp(x[seq(1, length(x)-1)]))


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Christoph Scherber" <Christoph.Scherber at uni-jena.de>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, February 04, 2005 1:28 PM
Subject: [R] integration function


> Dear R users,
>
> I have tried to write a function which gives the step-wise integral 
> for an exponential function (moving from -3 to 3 in steps of 0.1, 
> where the output for every step shall be the integral under the 
> curve of y against x.
>
> However, something seems to be wrong with this function; can anyone 
> please help me?
>
> x<-seq(-3,3,0.1)
> y<-exp(x)
>
>
> integral<-function(z,a,b,step){
> for(i in (1:((b-a)/step))){
>    c<-0
>    c[i]<-integrate(z,lower=a+(i-1)*step,upper=a+i*step)
>    print(c$integral)
> }}
>
> integral(y,-3,3,0.1)
>
> Best regards
> Christoph
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list