1 Problem

It is not always possible to find the exact value of a definite integral. For definite integrals of functions \(f\), finding an antiderivative of \(f\) is often very difficult or even impossible. For example:

\[ \int_0^1 \exp(-x^2)\,dx \quad \quad \int_0^1 \frac{\sin(x)}{x}\,dx \]

Or, when the function is obtained from scientific experiments or data collection, with no explicit formula available.

Theorem 1.1 If a function \(f\) is continuous on \([a,b]\), then the definite integral \(\int_a^b f(x)\,dx\) can be defined as the limit of Riemann sums as the norm of the partition approaches zero.

We will refer to Theorem 1.1 later.

2 Method

In such cases, we need to approximate the value of the integral and control the error. A definite integral can be defined as the limit of a Riemann sum, hence:

\[ \int_a^b f(x)\,dx \approx \sum_{i=1}^n f(x^*_i) \Delta x, \quad \text{where } \Delta x = \frac{b-a}{n} \]

with \(x^*_i\) being any point in \([x_{i-1}, x_i]\).

Three common choices for \(x^*_i\):

  1. Left endpoint: \(x^*_i = x_{i-1}\)
  2. Right endpoint: \(x^*_i = x_i\)
  3. Midpoint: \(x^*_i = \frac{x_i + x_{i-1}}{2}\)

Among these, the midpoint approximation usually yields better results.

We will illustrate this using the function \(f(x) = \sqrt{x}\) over the interval \([1,5]\):

library(tidyverse)
library(cubature)
library(glue)

f <- function(x) sqrt(x)

total_area <- adaptIntegrate(f, lower = 1, upper = 5)$integral

approximate_plot <- function(method, n) {
  x_part <- seq(1, 5, length.out = n + 1)
  
  if (method == "left") {
    x_star <- x_part[-(n+1)]
  } else if (method == "right") {
    x_star <- x_part[-1]
  } else if (method == "mid") {
    x_star <- (x_part[-1] + x_part[-(n+1)]) / 2
  }
  
  y_star <- f(x_star)
  delta_x <- (5 - 1) / n
  approx_area <- sum(y_star * delta_x)
  error <- total_area - approx_area
  
  tibble(x = x_star, y = y_star) %>%
    ggplot(aes(x = x, y = y)) +
    geom_col(width = delta_x, fill = "skyblue") +
    labs(
      title = glue("{method} - Error: {round(error,7)}"),
      x = "x", y = "f(x)"
    ) +
    theme_minimal()
}

plot_left <- approximate_plot("left", 100)
plot_right <- approximate_plot("right", 100)
plot_mid <- approximate_plot("mid", 100)

library(gridExtra)
grid.arrange(plot_left, plot_right, plot_mid, nrow = 2)

3 Conclusion

For the integral \(\int_1^5 \sqrt{x}\,dx\), the midpoint approximation clearly results in a smaller error compared to the left and right endpoint approximations.

In practice, many computational systems use Simpson’s rule or more sophisticated techniques like adaptive numerical integration, which subdivides intervals more finely where the function varies rapidly to optimize accuracy with fewer computations.

4 References

[1] Stewart, James. Essential Calculus: Early Transcendentals. Brooks/Cole, a part of the Thomson Corporation, 2007.