# Numerical Integration Methods

As a follow up on our motivation, I will be introducing a few of the methods that we will be testing. Namely, this post will introduce Riemann sums, Trapezoidal sums, and the Midpoint method. I wanted to put these three together because they are very similar in computation (we do not know how similar they are in accuracy though).

### Riemann Sums

The Riemann sums method is one of the simplest methods to compute definite integrals. All this method does is sum the evaluation of the function at some point, $x_i$, multiplied by some small value, $d$. This gives us the following equation:

$$\sum_{i=1}^{n}{f(x_i)d}$$ where $n$ is the number of elements in the range of our interval.

An example of what this may look like using Python/Sage code:

 import numpy f = lambda x: x**3 # Some function f a, b = (0, 3) # Some interval d = 0.001 # some small delta value numpy.sum((f(x)*d) for x in numpy.arange(a, b, d))) 

Riemann Sums Method. Src: Wikipedia

### Trapezoidal Sums

The Trapezoidal Sums method is similar to the Riemann sums method as in it computes the sums of the function evaluated at some point, $x_i$. However, each term that is summed is the average between two points. That is, our sum looks like the following:

$$\sum_{i=1}^{n-1}{(f(x_i)+f(x_{i+1})d/2}$$ where $n$ again is the number of elements in the range of our interval.

An example of what this may look like in Python/ Sage code:

 f = lambda x: x**3 # Some function f a, b = (0, 3) # Some interval d = 0.001 # Some small delta value x = np.arange(a, b, d) np.sum((f(x[i]) + f(x[i+1]))*d/2 for i in range(0, len(x)-1)) 

### Midpoint Method

The midpoint method, like the Trapezoidal method, is very similar to the Riemann sums method, except, while using the midpoint method, we are computing the sums of the “middle” of the rectangle. That is, our summation looks as follows:

$$\sum_{i=1}^{n-1}{((f(x_i)+f(x_{i+1}))/2)d}$$ where $n$ is the number of elements in the our interval.

An example of what this method may look like in Python/Sage is:

 f = lambda x: x**3 # some function f a, b = (0, 3) # some interval d = 0.001 # some small delta value x = np.arange(a, b, d) np.sum((f((x[i] + x[i+1])/2)*d) for i in range(0, len(x)-1)) 

## 6 thoughts on “Numerical Integration Methods”

1. Samuel Coskey

I like the Riemann Sums picture (though I’m not sure I understand the rainbow in the middle ðŸ™‚

One question I had while reading the beginning of this article is: what exactly is the small number d? And what does it have to do with the choice of evaluation points $x_i$?

2. carriesmith

I am wondering why you would use one method over the other. I have used The Riemann Sum and the Midpoint Method before but never The Trapezoidal Sums. In what application would you choose to use The Trapezoidal Sums?

1. Kenny Post author

As we can begin to see from our data, we can see that the Trapezoidal method can be more accurate on certain classes of functions. Further, the machinery of the trapezoidal method and even more so the Simpsons method, is more complex and specifically designed to give us more accurate estimates of the definite integral.