Compute the area of a function with values
y at the points
1 2 3 4
x-coordinates of points on the x-axis
y-coordinates of function values
function to be integrated.
lower and upper border of the integration domain.
maximum number of steps.
tolerance; stops when improvements are smaller.
arguments passed to the function.
(x, 0) and
(x, y) are taken as vertices of a
polygon and the area is computed using
polyarea. This approach
matches exactly the approximation for integrating the function using the
trapezoidal rule with basepoints
cumtrapz computes the cumulative integral of
y with respect
x using trapezoidal integration.
y must be
vectors of the same length, or
x must be a vector and
matrix whose first dimension is
y can be complex.
trapzfun realizes trapezoidal integration and stops when the
differencefrom one step to the next is smaller than tolerance (or the
of iterations get too big). The function will only be evaluated once
on each node.
Approximated integral of the function, discretized through the points
x, y, from
Or a matrix of the same size as
trapzfun returns a lst with components
value the value of
iter the number of iterations, and
the relative error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Calculate the area under the sine curve from 0 to pi: n <- 101 x <- seq(0, pi, len = n) y <- sin(x) trapz(x, y) #=> 1.999835504 # Use a correction term at the boundary: -h^2/12*(f'(b)-f'(a)) h <- x - x ca <- (y-y) / h cb <- (y[n]-y[n-1]) / h trapz(x, y) - h^2/12 * (cb - ca) #=> 1.999999969 # Use two complex inputs z <- exp(1i*pi*(0:100)/100) ct <- cumtrapz(z, 1/z) ct #=> 0+3.14107591i f <- function(x) x^(3/2) # trapzfun(f, 0, 1) #=> 0.4 with 11 iterations
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.