Takes a vector of x values and a corresponding set of postive
f(x)=y values and evaluates the area under the curve:
int{f(x)dx}
.
Usage
sintegral(x, fx, n.pts = 256)
Arguments
x
a sequence of x values.
fx
the value of the function to be integrated at x.
n.pts
the number of points to be used in the
integration.
Value
returns a list with the following elements
x
the x-values at which the integral has been evaluated
y
the cummulative integral
int
the value of the integral over the whole range
Examples
## integrate the normal density from -3 to 3
x<-seq(-3,3,length=100)
fx<-dnorm(x)
estimate<-sintegral(x,fx)$int
true.val<-diff(pnorm(c(-3,3)))
cat(paste("Absolute error :",round(abs(estimate-true.val),7),"\n"))
cat(paste("Relative percentage error :", 100*round((abs(estimate-true.val)/true.val),6),"%\n"))
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Bolstad2)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Bolstad2/sintegral.Rd_%03d_medium.png", width=480, height=480)
> ### Name: sintegral
> ### Title: Numerical integration using Simpson's Rule
> ### Aliases: sintegral
> ### Keywords: misc
>
> ### ** Examples
>
> ## integrate the normal density from -3 to 3
> x<-seq(-3,3,length=100)
> fx<-dnorm(x)
> estimate<-sintegral(x,fx)$int
> true.val<-diff(pnorm(c(-3,3)))
> cat(paste("Absolute error :",round(abs(estimate-true.val),7),"\n"))
Absolute error : 8.1e-06
> cat(paste("Relative percentage error :", 100*round((abs(estimate-true.val)/true.val),6),"%\n"))
Relative percentage error : 8e-04 %
>
>
>
>
>
> dev.off()
null device
1
>