-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathintegrateTrapezoid.Rd
54 lines (51 loc) · 1.97 KB
/
integrateTrapezoid.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/misc.R
\name{integrateTrapezoid}
\alias{integrateTrapezoid}
\title{Trapezoidal Integration}
\usage{
integrateTrapezoid(x, y, type = c("A", "dA", "cA"), xmin, xmax)
}
\arguments{
\item{x, y}{vectors of x and y values. In the normal case, these
vectors are both supplied, and of equal length. There are also two
special cases. First, if \code{y} is missing, then
\code{x} is taken to be \code{y}, and a new \code{x} is constructed
as \code{\link{seq_along}}\verb{(y)1. Second, if }length(x)\verb{is 1 and}length(y)\verb{exceeds 1, then}x\verb{is replaced by}x*\code{[}seq_along\verb{]}(y)`.}
\item{type}{Flag indicating the desired return value (see \dQuote{Value}).}
\item{xmin, xmax}{Optional numbers indicating the range of the integration.
These values may be used to restrict the range of integration, or to
extend it; in either case, \code{\link[=approx]{approx()}} with \code{rule=2}
is used to create new x and y vectors.}
}
\value{
If \code{type="A"} (the default), a single value is returned,
containing the estimate of the integral of \code{y=y(x)}. If
\code{type="dA"}, a numeric vector of the same length as \code{x}, of which
the first element is zero, the second element is the integral between
\code{x[1]} and \code{x[2]}, etc. If \code{type="cA"}, the result is the
cumulative sum (as in \code{\link[=cumsum]{cumsum()}}) of the values that would be
returned for \code{type="dA"}. See \dQuote{Examples}.
}
\description{
Estimate the integral of one-dimensional function using the trapezoidal
rule.
}
\section{Bugs}{
There is no handling of \code{NA} values.
}
\examples{
x <- seq(0, 1, length.out = 10) # try larger length.out to see if area approaches 2
y <- 2 * x + 3 * x^2
A <- integrateTrapezoid(x, y)
dA <- integrateTrapezoid(x, y, "dA")
cA <- integrateTrapezoid(x, y, "cA")
print(A)
print(sum(dA))
print(tail(cA, 1))
print(integrateTrapezoid(diff(x[1:2]), y))
print(integrateTrapezoid(y))
}
\author{
Dan Kelley
}