trapint: Trapezoidal rule for 1D and 2D Riemman integrals

Description Usage Arguments Details Value Author(s) Examples

View source: R/trapint.R

Description

Computes the Riemman integral of a 1D or 2D function f by using the trapezoidal rule.

Usage

1
trapint(x, y = NULL, f)

Arguments

x

First vector of points where the function f is evaluated. The first and last entries of this vector must be the limits of the interval [a,b] where the function is defined.

y

Optional. Second vector of points where the function f is evaluated. The first and last entries of this vector must be the limits of the interval [c,d] where the function is defined. Required when f is 2D.

f

The integrand. A vector or matrix with values of f(x) or f(x,y).

Details

This command computes the 1D-Riemman integral

I = integral f(x) dx,

or the 2D-Riemman integral

I = integral f(x,y) dxdy,

of a real-valued function f(x) in the interval [a,b] or f(x,y) in the rectangle [a,b] x [c,d]. The argument f should be a vector or matrix. The arguments x and y should be vectors where the function f is evaluated.

Value

A real number corresponding to the final estimate of the Riemman integral.

Author(s)

Jonatan A. Gonzalez <jmonsalv@uji.es>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## Not run:
#################

# Define the interval [2, 3]
x <- seq(2, 3, length.out = 100)
# Compute the values of the function f(x) = x / (x ^ 2 - 1) on [2,3]
f <- x / (x ^ 2 - 1)
# Estimated integral of f over [2, 3]
trapint(x = x, f = f)

# 2D integral example
# Define the rectangular region R = [1, 2] x [0, 1]
x <- seq(1, 2, length.out = 100)
y <- seq(0, 1, length.out = 100)
# Compute the values of the function f(x, y) = (x ^ 2) * y on R
f <- 0 * matrix(1:length(x)*length(y), nrow=length(x), ncol=length(y))
for(i in 1:length(x)){
  for(j in 1:length(y)){
    f[i,j] <- (x[i]) ^ 2 * y[j]
  }
}
# Estimated integral of f over R
trapint(x = x, y = y, f = f)
## End(Not run)

frajaroco/scdcLISA documentation built on Nov. 28, 2021, 12:19 a.m.