# 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))