Calculus: Manually Programming Numerical Integration¶

Anthony Jimenez
25 August 2021
http://www.fa-jimenez.com/

Overview of goals in this notebook¶

This notebook will cover how one can manually program a numerical integration function that leverages rectangular panels to integrate any given function.

The main libraries that will be used are numpy (for arrays) and scipy for pre-built integration functions (for comparison purposes).

Given an $x$ domain and $n$ panels desired from the input, the given function will be integrated $F(x) = \int_{a}^{b} f(x)\, dx$.

Defining the general function $f(x)$¶

In this notebook we wil look at the domain, $x: [0, 9]$ for the following function:

$$f(x) = \text{sin}(x)$$$$F(x) = \int_{a}^{b} f(x)\, dx = -\text{cos}(x)|_{a}^{b}$$

When evaluating this integral over the aforementioned domain, we get the following result.

$$F(x) = \int_{0}^{9} \text{sin}(x)\, dx = -\text{cos}(x)|_{0}^{9} = 1.91113026188468$$

Creating main rectangular integration function¶

Provided the function, number of panels the rectangular method is computed as follows:

$$w_{\text{panel}} = \frac{b - a}{n}$$$$h = f(x)$$$$A_{\text{rectangle}} = h * w_{\text{panel}}$$

The main difference between the two rectangular methods will be which $x$ values will be used for evaluating the height of the rectangle, $h$. When implementing the left rectangular method, the height is always evaluated at the left-hand point of any given rectangle panel. As a result, the last $x$ value will not be used when computing the height of the rectangle.

In contrast the right rectangular method will skip the first $x$ value of the domain and the second value will be used to compute the height of the first rectangle.

Example 1: Low density rectangle panels (11 panels)¶

Naturally, with lower density rectangle panels, each rectangle has a larger width. This means that the height computed for the rectangle will be applied to a larger domain section and this can lead to bad estimates if the function changes quickly.

In effect, a lower density count of rectangles will lead to the function being averaged over a bigger section. Below the true function is shown with subplots also showing the interpretation of the rectangles using both left and right schemes.

Lastly, a print out of the integral result for left- and right-hand side are displayed for comparison.

Example 2: Mid density rectangle panels (30 panels)¶

With increased rectangles, we see small width panels and the height is thus averaged over a smaller section. When comparing this with the true function, we see that the shape of the sin() function is a lot more alike and we can see that the integral approximations are getting closer to the analytical answer.

Comparing the integral results¶

This shows the plot of the integral results for all values from [0,9]. We can see that whenever the derivative of the function approaches 0 the integration is doing the best because it is most like the shape of the rectangle. Whenever the function is growing or declining that is when we see the integration missing in a worse manner.